Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a trivial problem but I don't know how to solve it. I just wanna do a simple "foreach" of a Mat to view rgb values. I have next code:

for(int i=0; i<mat.rows; i++)
{
    for(int j=0; j<mat.cols; j++)
    {
        int value_rgb = mat.at<uchar>(i,j);
        cout << "(" << i << "," << j << ") : " << value_rgb <<endl;
    }
}

The mat is 200 rows x 200 cols. When I print on console the results, just in the final the programs fails with next error:

**OpenCV Error: Assertion failed (dims <= 2 && data && (unsigned)i0 <(unsigned)size.p[0] && (unsigned)(i1*DataType<_Tp>::channels) < (unsigned)(size.p[1]*channels()) && ((((sizeof(size_t)<<28)|0x8442211) >> ((DataType<_Tp>::depth) & ((1 << 3) - 1))*4) & 1 5) == elemSize1()) in unknown function, file c:opencvuildincludeopencv2coremat.hpp, line 537**

Anyone can help me? Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
184 views
Welcome To Ask or Share your Answers For Others

1 Answer

The below piece of code will help you in accessing the rgb pixel values.You have to access three channels to view RGB values.

for(int i = 0; i < i<mat.rows; i++)
    {
        for(int j = 0; j < mat.cols; j++)
        {
            int b = mat.at<cv::Vec3b>(i,j)[0];
            int g = mat.at<cv::Vec3b>(i,j)[1];
            int r = mat.at<cv::Vec3b>(i,j)[2];
            cout << r << " " << g << " " << b << value_rgb <<endl ;
        }
    }

To read pixel value from a grayscale image

#include <opencvcv.h>
#include <highguihighgui.hpp>
using namespace std;
using namespace cv;
    int main()
{
cv::Mat img = cv::imread("5.jpg",0);

for(int j=0;j<img.rows;j++) 
{
  for (int i=0;i<img.cols;i++)
  {
    int a;
    a=img.at<uchar>(j,i);
     cout<<a<<endl; 
  }
}

cv::imshow("After",img);
waitKey(0);
}

Updated This code reads all the grayscale values from an image and results in frequent occurring vales (Number of times the value as occurred). i.e

Number of times pixel value '0' as appeared,

Number of times pixel value '1' as appeared, ... & so on till 256.

#include <opencvcv.h>
#include <highguihighgui.hpp>
using namespace std;
using namespace cv;
    int main()
{
cv::Mat img = cv::imread("5.jpg",0);

//for(int j=0;j<img.rows;j++) 
//{
//  for (int i=0;i<img.cols;i++)
//  {
//    int a;
//  a=img.at<uchar>(j,i);
//     cout<<a<<endl; 
//  }
//}
vector<int> values_rgb;
for(int i=0; i<20; i++)
{
    for(int j=0; j<20; j++)
    {
        int value_rgb = img.at<uchar>(i,j);
        values_rgb.push_back(value_rgb);
        //cout << "(" << i << "," << j << ") : " << value_rgb <<endl;
    }
}
// Sorting of values in ascending order 
vector<int> counter_rg_values;
for(int l=0; l<256; l++)

{
    for(int k=0; k<values_rgb.size(); k++)
    {
        if(values_rgb.at(k) == l)
        {
            counter_rg_values.push_back(l);
        }
    }
}
//for(int m=0;m<counter_rg_values.size();m++)
//cout<<m<<" "<< counter_rg_values[m] <<endl;
int m=0;
for(int n=0;n<256;n++)
{
    int c=0;
    for(int q=0;q<counter_rg_values.size();q++)
    {
        if(n==counter_rg_values[q])
        {
            //int c;
        c++;
        m++;
        }
    }

    cout<<n<<"= "<< c<<endl;
}
cout<<"Total number of elements "<< m<<endl;

cv::imshow("After",img);
waitKey(0);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...