opencv - 从 IPLImage 到 Mat

标签 opencv image-processing iplimage

我非常熟悉 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/

相关文章:

python - 如何使用opencv去除原始图像中的噪声

c++ - 使用 C++ 进行 OpenCv 相机校准(不支持的格式或格式组合错误)

python - 检测同一 View 的两个不同拍摄图像之间的差异

OpenCV IplImage 数据 float

opencv - 能够将Mat对象用作IplImage对象,反之亦然的最佳方法是什么?

python - 如何将 x 和 y 像素的颜色更改为蓝色?

c++ - cvCalcOpticalFlowHS() opencv 抛出异常

python - 使用单应性降低不透明度的图像对齐

python-3.x - 从一张图片在python中进行线检测

c - 关于 OpenCV 中的内存管理