c++ and opencv get and set pixel color to Mat

前端 未结 3 538
遇见更好的自我
遇见更好的自我 2020-12-08 02:18

I\'m trying to set a new color value to some pixel into a cv::Mat image my code is below:

    Mat image = img;
    for(int y=0;y

        
3条回答
  •  星月不相逢
    2020-12-08 02:47

    I would not use .at for performance reasons.

    Define a struct:

    //#pragma pack(push, 2) //not useful (see comments below)
    struct RGB {
        uchar blue;
        uchar green;
        uchar red;  };
    

    And then use it like this on your cv::Mat image:

    RGB& rgb = image.ptr(y)[x];
    

    image.ptr(y) gives you a pointer to the scanline y. And iterate through the pixels with loops of x and y

提交回复
热议问题