Using Mat::at(i,j) in opencv for a 2-D Mat object

后端 未结 3 876
说谎
说谎 2020-12-10 07:39

I am using Ubuntu 12.04 and OpenCV 2

I have written the following code :

IplImage* img =0;
img = cvLoadImage(\"nature.jpg\");
if(img != 0)
{
    Mat         


        
3条回答
  •  旧时难觅i
    2020-12-10 08:08

    You have to specify the correct type for cv::Mat::at(i,j). You are accessing the pixel as int, while it should be a vector of uchar. Your code should look something like this:

    IplImage* img = 0;
    img = cvLoadImage("nature.jpg");
    if(img != 0)
    {
      Mat Img_mat(img);
      std::vector BGR;
      split(Img_mat, BGR);
    
      Vec3b data = BGR[0].at(i,j);
      // data[0] -> blue
      // data[1] -> green
      // data[2] -> red
    }
    

提交回复
热议问题