我非常熟悉 OpenCV 1.1 中使用的 IPL 图像格式。但是我使用的是最新的 2.4 版本,想切换到 OpenCV 的 C++ 接口(interface)。这是我访问图像中像素的方法:
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;
}
}
我需要帮助来转换这个具有 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;
}
}
关于opencv - 从 IPLImage 到 Mat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16424678/