Opencv之三通道图像的三种指针遍历

int main(int argc,char **argv)
{
    string image_path = "D:/vs2019Proj/conapp/timg.jpg";
    Mat src_image = imread(image_path);
    if(!src_image.data)
    {
        cout<<"could not load image..."<(row);
        //每一次跳跃三列,也就是跳跃一个像素,三列也就是B,G,R
        for (int col = 0; col < width*3; col+=3)
        {
            ptr[col+0] = 255 - ptr[col+0];
            ptr[col+1] = 255 - ptr[col+1];
            ptr[col+2] = 255 - ptr[col+2];
        }
    }
    //第二种指针遍历
    for (int row = 0; row < height; row++)
    {
        Vec3b* ptr = src_image.ptr(row);
        for (int col = 0; col < width; col++)
        {
            ptr[col][0] = 255 - ptr[col][0];
            ptr[col][1] = 255 - ptr[col][1];
            ptr[col][2] = 255 - ptr[col][2];    
        }
    }
    //第三种指针遍历
    int imagerows = src_image.rows;
    int imagecols = src_image.cols;
    if(src_image.isContinuous())//判断内存是否连续
    {
        imagecols = imagecols*imagerows;
        imagerows = 1;    
    }
    for (int row = 0;row < imagerows; row++)
    {
        uchar *imagedata = src_image.ptr(row);
        for(int col = 0;col < imagecols;col++)
        {
            imagedata[0] = 255 - imagedata[0];
            imagedata[1] = 255 - imagedata[1];
            imagedata[2] = 255 - imagedata[2];
            imagedata+=3;//指针偏移3
        }
    }
}

 

你可能感兴趣的:(opencv)