-2

我正在使用以下代码读取图像并在屏幕上显示像素值

#include<iostream>

#include<opencv2/opencv.hpp>

using namespace std;
using namespace cv; 


int main()
{

Mat img = imread("C:\\cat.jpg", CV_LOAD_IMAGE_COLOR); //open and read the image

      if (img.empty())
     {
          cout << "Image cannot be loaded..!!" << endl;
          return -1;
     }

      cvtColor(img, img, CV_BGR2GRAY); //change the color image to grayscale image

 int rows = img.rows;
 int cols =img.cols;

 cv::Size s = img.size();
rows = s.height;
cols = s.width;

// this loop is to get all the pixel values
uchar val;
for(int r=0;r<rows;r++)
{
for(int c=0;c<cols;c++)
{
val = img.at<uchar>(r,c);
cout << (int)val <<"  ";
}
cout<<endl;
}
// to get a single value of pixelu need a variable 
val = img.at<uchar>(40,50);

    cout << (int)val << endl;

 imshow("showImg",img);
     cvWaitKey(0);

    return 1;


      waitKey(0); //wait for key press

      destroyAllWindows(); //destroy all open windows
return 0;
} 

// 它适用于较大的图像,但是当我使用 8x8 .jpg 或 .tiff 图像时,它显示 //图像而不是像素值并给出错误

//“opencvconfig.exe 中 0x75c7c41f (KernelBase.dll) 处的未处理异常:Microsoft C++ //异常:cv::Exception at memory location 0x0030f354..”

//这个错误显示可以任何1请帮助,我想尽快帮助

4

1 回答 1

0

删除这些行

// to get a single value of pixelu need a variable 
val = img.at<uchar>(40,50);

cout << (int)val << endl;

(40,50) 不是有效位置,访问它是异常的最可能原因

于 2013-11-02T12:06:03.250 回答