Cycle through pixels with opencv

How would I be able to cycle through an image using opencv as if it were a 2d array to get the rgb values of each pixel? Also, would a mat be preferable over an iplimage for this operation?


如果您使用C ++,请使用opencv的C ++接口,然后您可以通过http://docs.opencv.org/2.4/doc/tutorials/core/how_to_scan_images/how_to_scan_images.html#the-efficient-way访问成员或使用例如,cv :: Mat :: at()。


cv::Mat is preferred over IplImage because it simplifies your code

cv::Mat img = cv::imread("lenna.png");
for(int i=0; i<img.rows; i++)
    for(int j=0; j<img.cols; j++) 
        // You can now access the pixel value with cv::Vec3b
        std::cout << img.at<cv::Vec3b>(i,j)[0] << " " << img.at<cv::Vec3b>(i,j)[1] << " " << img.at<cv::Vec3b>(i,j)[2] << std::endl;

This assumes that you need to use the RGB values together. If you don't, you can uses cv::split to get each channel separately. See etarion's answer for the link with example.

Also, in my cases, you simply need the image in gray-scale. Then, you can load the image in grayscale and access it as an array of uchar.

cv::Mat img = cv::imread("lenna.png",0);
for(int i=0; i<img.rows; i++)
    for(int j=0; j<img.cols; j++)
        std::cout << img.at<uchar>(i,j) << std::endl;

UPDATE : Using split to get the 3 channels

cv::Mat img = cv::imread("lenna.png");
std::vector<cv::Mat> three_channels = cv::split(img);

// Now I can access each channel separately
for(int i=0; i<img.rows; i++)
    for(int j=0; j<img.cols; j++)
        std::cout << three_channels[0].at<uchar>(i,j) << " " << three_channels[1].at<uchar>(i,j) << " " << three_channels[2].at<uchar>(i,j) << std::endl;

// Similarly for the other two channels

UPDATE: Thanks to entarion for spotting the error I introduced when copying and pasting from the cv::Vec3b example.


Since OpenCV 3.0, there are official and fastest way to run function all over the pixel in cv::Mat.

void cv::Mat::forEach (const Functor& operation)

If you use this function, operation is runs on multi core automatically.

Disclosure : I'm contributor of this feature.

链接地址: http://www.djcxy.com/p/89810.html

上一篇: 访问OpenCV中灰度图像的像素值

下一篇: 使用opencv循环显示像素