Fail assertion opencv mat.inl.hpp line 930

非 Y 不嫁゛ 提交于 2019-12-13 08:29:55

问题


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:\opencv\build\include\opencv2\core\mat.hpp, line 537**

Anyone can help me? Thanks.


回答1:


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 <opencv\cv.h>
#include <highgui\highgui.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 <opencv\cv.h>
#include <highgui\highgui.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);
}


来源:https://stackoverflow.com/questions/32902029/fail-assertion-opencv-mat-inl-hpp-line-930

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!