2013-06-05 68 views
0

我使用imread(..,...- GRAYSCAle)將圖像(png)文件直接從3個通道放置到1個通道,我可以在灰度上看到圖像,但像素值爲0,而不是1。不勝感激!爲什麼在imread載入圖像後像素值爲0?

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); 
    } 
} 

回答

1

OpenCV的負載形象CV_8UCX(在你的情況,這將是CV_8UC1)型。因此,要打印所有的像素值,你應該叫at()與模板參數unsigned charuchar

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. 記住翻轉i和j在你的代碼。
  2. 灰度值的範圍是0(黑色)-255(白色),所以如果像素是純白色的,您可能會看到255而不是1。
+0

當我切換到uchar後,什麼也不打印。 –

相關問題