ruimin tan
ruimin tan

Reputation: 1

why the pixel value is 0 after load the image by imread?

I laod the image (png) file directly from 3 channel to 1 channel using imread(.., ...-GRAYSCAle), I can see the image on grayscale but the pixel value is 0, not 1. any help is appreciated!

cv::Mat image=cv::imread(filename1, CV_LOAD_IMAGE_GRAYSCALE);
if (!image.data){
    std::cout<<"Problem laoding image";
}

cv::namedWindow("Window1");
cv::imshow("Window1",image);

for (i=0;i<720;i++){
    for (j=0;j<720;j++){

        std::cout<<image.at<int>(j,i)<<std::endl;
        //printf("%d \t", vPixel);
    }
}

Upvotes: 0

Views: 182

Answers (1)

cxyzs7
cxyzs7

Reputation: 1227

OpenCV loads image as CV_8UCX (in your case, it would be CV_8UC1) type. So to print all pixel values, you should call at() with template argument unsigned char or uchar:

for (int i = 0;i < image.rows; i++) {
  for (int j = 0; j < image.cols; j++) {
    std::cout<<image.at<uchar>(i, j)<<std::endl;
  }
}
  1. Remember to flip i and j in your code.
  2. The range for grayscale values is 0(black)-255(white), so you may see 255 not 1 if the pixel is pure white.

Upvotes: 1

Related Questions