Sou*_*jit 6 opencv image-processing iplimage
我很熟悉OpenCV 1.1中使用的IPL图像格式.但是我使用的是最新的2.4版本,并希望切换到OpenCV的C++界面.这是我访问图像中像素的方法:
int step = img->widthStep;
int height = img->height;
int width = img->width;
unsigned char* data = (unsigned char*) img->imageData;
for (int i=0; i<height; i++)
{
for (int j=0; j<step; j+=3) // 3 is the number of channels.
{
if (data[i*step + j] > 200) // For blue
data[i*step + j] = 255;
if (data[i*step + j + 1] > 200) // For green
data[i*step + j + 1] = 255;
if (data[i*step + j + 2] > 200) // For red
data[i*step + j + 2] = 255;
}
}
Run Code Online (Sandbox Code Playgroud)
我需要帮助来转换这个精确的代码块与Mat结构.我在这里和那里找到了几个函数,但是如果我得到整个上面几行的精确转换将会非常有用.
// Mat mat; // a bgr, CV_8UC3 mat
for (int i=0; i<mat.rows; i++)
{
// get a new pointer per row. this replaces fumbling with widthstep, etc.
// also a pointer to a Vec3b pixel, so no need for channel offset, either
Vec3b *pix = mat.ptr<Vec3b>(i);
for (int j=0; j<mat.cols; j++)
{
Vec3b & p = pix[j];
if ( p[0] > 200 ) p[0] = 255;
if ( p[1] > 200 ) p[1] = 255;
if ( p[2] > 200 ) p[2] = 255;
}
}
Run Code Online (Sandbox Code Playgroud)