1、(1) 在空间域图像处理中,对于一个图像我们往往需要对其逐个像素的进行处理,对每个像素的处理使用相同的算法(或者是图像中的某个矩形部分)。即,对于图像f(x,y),其中0xM,0yN,图像为M*N大小,使用算法algo,则f(x,y) = algo(f(x,y)。事先实现一个算法框架,然后再以函数指针或函数对象(functor,即实现operator()的对象)传入算法,可以减轻编程的工作量。 如下代码便是一例:#ifndef PROCESSALGO_H#define PROCESSALGO_H#include Gdiplus.hnamespace nsimgtk template bool
2、ProcessPixelsOneByOne(Gdiplus:Bitmap* const p_bitmap, Processor processor, unsigned int x, unsigned int y, unsigned int width, unsigned int height) if (p_bitmap = NULL) return false; if (width + x p_bitmap-GetWidth() | (height + y p_bitmap-GetHeight()BitmapData bitmapData;Rect rect(x, y, width,heigh
3、t); if (p_bitmap-LockBits(&rect, Gdiplus:ImageLockModeWrite, pixelFormat, &bitmapData) != Gdiplus:Ok) pixelType *pixels = (pixelType*)bitmapData.Scan0; for (unsigned int row=0; rowheight; +row) for (unsigned int col=0; colstring/ 8阶灰度图的灰度反转算法 class NegativeGray8 public: void operator()(unsigned char
4、 *const p_value) *p_value = 0xff; ;/ 8阶灰度图的Gamma校正算法 class GammaCorrectGray8 private: unsigned char d_s256; GammaCorrectGray8:GammaCorrectGray8(double c, double gamma); void operator()(unsigned char*const p_value) *p_value = d_s*p_value; / 8阶灰度图的饱和度拉伸算法 class ContrastStretchingGray8 ContrastStretchi
5、ngGray8:ContrastStretchingGray8(double a1, double b1, unsigned int x1, double a2, double b2, unsigned int x2, double a3, double b3); / 8阶灰度图的位平面分割,构造函数指定位平面号 class BitPlaneSliceGray8 BitPlaneSliceGray8(unsigned char bitPlaneNum); void operator()(unsigned char* const p_value)/ 上述类中各构造函数的实现代码,应该分在另一个文
6、件中,此处为说明方便,一并列出#include SpatialDomain/spatialDomain.hGammaCorrectGray8(double c, double gamma) double temp; for (unsigned int i=0; i 255 | x2 255 | x1 x1) d_si = i; else double tmp;x1; tmp = ceil(a1*double(i)+b1); d_si = (unsigned char)tmp; for (unsigned int i=x1;x2; tmp = ceil(a2*double(i)+b2); for
7、 (unsigned int i=x2; tmp = ceil(a3*double(i)+b3); BitPlaneSliceGray8:BitPlaneSliceGray8(unsigned char bitPlaneNum) unsigned char bitMaskArray8 = 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 unsigned char tmp = i; tmp &= bitMaskArraybitPlaneNum; tmp = (tmp bitPlaneNum) * 255; d_si = tmp;(2) 直方图在GDI
8、+1.0中没有获得支持,我们必须自行实现。直方图相关的处理在数字图像处理中占有重要地位,可以通过它获取图像灰度级的统计信息,且可以通过直方图进行一些重要的图像增强技术,如直方图均衡化,直方图规定化,基本全局门限等。下面是获取8阶图像直方图的算法实现: bool GetHistogramNormalizeGray8(Gdiplus:Bitmap * const p_bitmap, float *histogramArray) if (p_bitmap = NULL | histogramArray = NULL)Rect rect(0, 0, p_bitmap-GetWidth(), p_bit
9、map-GetHeight();ImageLockModeRead, PixelFormat8bppIndexed, & unsigned char *pixels = (unsigned char*)bitmapData.Scan0; unsigned int histogram256; for (int i=0; histogrami = 0;GetWidth();GetHeight(); +histogrampixelscol+row*bitmapData.Stride; const unsigned int totalPixels = p_bitmap-GetWidth() * p_b
10、itmap- histogramArrayi = float(histogrami) / float(totalPixels);在获取直方图后(即上面算法的第二个参数),再将其作为参数传入下面的对象的构造函数,然后以该对象为仿函数传入ProcessPixelsOneByOne即可实现8阶图像直方图均衡化: / 8阶灰度图的直方图均衡化 class HistogramEqualizationGray8 HistogramEqualizationGray8(const float *const histogramArray);#endif/ HistogramEqualizationGray8:HistogramEqualizationGray8(const float *const histogramArray) if (histogramArray != NULL) float sum = 0.0;
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1