|
// 阈值
CImage img;
img.Load();
IplImage* pImg = img->GetImage();
int threshold = 120;
RgbImage bits(pImg);
for (int y = 0; y < pImg->height; y++)
{
for (int x= 0; x < pImg->width; x++)
{
RgbPixel& pixel = bits[y][x]; // !!!使用引用
if (pixel.r < threshold)
{
pixel.r = 0;
pixel.g = 0;
pixel.b = 0;
}
}
}
img.Show( "Image" );
附模板定义:
template<class T>
class ImageBit
{
private:
IplImage* imgp;
public:
ImageBit( IplImage* img = 0 ){ imgp = img; }
~ImageBit() {imgp = 0;}
void operator=(IplImage* img){imgp = img;}
inline T* operator[](const int row)
{
return (T*)(imgp->imageData + row * imgp->widthStep);
}
};
typedef struct RgbPixel
{
unsigned char b, g, r;
}RgbPixel;
typedef struct RgbPixelFloat
{
float b, g, r;
}RgbPixelFloat;
typedef ImageBit<RgbPixel> RgbImage;
typedef ImageBit<RgbPixelFloat> RgbImageFloat;
typedef ImageBit<unsigned char> BwImage;
typedef ImageBit<float> BwImageFloat; |
|