对于较小的图像,无法读取像素值,但可以读取较大的图像

时间:2013-11-02 06:07:56

标签: c++ image opencv

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

#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 ::内存位置0x0030f354的异常...”

//显示此错误任何1请帮助,我想尽快得到帮助

1 个答案:

答案 0 :(得分:0)

删除这些行

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

cout << (int)val << endl;

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