关于图像直方图均衡化的原理和步骤先不作讨论,我就看看代码吧。
private Bitmap picequalization(Bitmap basemap, int width, int height) { Bitmap retmap = new Bitmap(basemap, width, height); int size = width * height; int[] gray = new int[256]; double[] graydense = new double[256]; for(int i=0;i<width;++i) for (int j = 0; j < height; ++j) { Color pixel = basemap.GetPixel(i,j); gray[Convert.ToInt16(pixel.R)] += 1; } for (int i = 0; i < 256; i++) { graydense[i] = (gray[i]*1.0) / size; } for (int i = 1; i < 256; i++) { graydense[i] += graydense[i-1]; } for (int i = 0; i < width; ++i) for (int j = 0; j < height; ++j) { Color pixel = basemap.GetPixel(i, j); int gg = Convert.ToInt16(pixel.R); int g = 0; if(gg==0) g=0; else g =Convert.ToInt16( graydense[Convert.ToInt16(pixel.R)] * Convert.ToInt16(pixel.R)); pixel = Color.FromArgb(g,g,g); retmap.SetPixel(i, j, pixel); //gray[Convert.ToInt16(pixel.R)] += 1; } return retmap; }
private Bitmap rgb2gray(Bitmap basemap,int width,int height) { Bitmap retmap = new Bitmap(basemap, width, height); for (int i = 0; i < width; ++i) for (int j = 0; j < height; ++j) { Color pixel = basemap.GetPixel(i, j); pixel = getgray(Convert.ToByte(pixel.R), Convert.ToByte(pixel.G), Convert.ToByte(pixel.B)); retmap.SetPixel(i, j, pixel); } return retmap; } private Color getgray(int r, int g, int b) { int gray = 0; gray = (r * 30 + g * 59 + b * 11 + 50) / 100; Color retcolor = Color.FromArgb(gray,gray,gray); return retcolor; } /// <summary> /// 获取直方图数据 /// </summary> /// <param name="rule"></param> /// <param name="width"></param> /// <param name="height"></param> /// <returns></returns> private double[] getsquare(Bitmap rule, int width, int height) { //double[] rule = new double[256]; int sizerule = width * height; int[] grayrule = new int[256]; double[] grayruledense = new double[256]; for (int i = 0; i < width; ++i) for (int j = 0; j < height; ++j) { Color pixel = rule.GetPixel(i, j); grayrule[Convert.ToInt16(pixel.R)] += 1; } for (int i = 0; i < 256; i++) { grayruledense[i] = (grayrule[i] * 1.0) / sizerule; } return grayruledense; } private double[] getrule(Bitmap rule, int rulewidth, int heightrule) { //double[] rule = new double[256]; int sizerule = rulewidth * heightrule; int[] grayrule = new int[256]; double[] grayruledense = new double[256]; for (int i = 0; i < rulewidth; ++i) for (int j = 0; j < heightrule; ++j) { Color pixel = rule.GetPixel(i, j); grayrule[Convert.ToInt16(pixel.R)] += 1; } for (int i = 0; i < 256; i++) { grayruledense[i] = (grayrule[i] * 1.0) / sizerule; } return grayruledense; } private double[] getdense(double[] zhifang) { for (int i = 1; i < 256; i++) { zhifang[i] += zhifang[i - 1]; } return zhifang; }