2014-01-22 165 views
1

我剛剛意識到網絡上沒有任何東西,經過大量搜索如何訪問OpenCv中像素的亮度值。灰度圖像。在openCV中訪問某個像素的亮度值(灰度圖像)

大多數網上搜索是關於如何訪問彩色圖像的BGR值,像這樣的:Accessing certain pixel RGB value in openCV

image.at <>基本上是3個頻道,即BGR,出於好奇,是有另一種OpenCV的訪問灰度圖像的某個像素值的類似方法?

+0

[灰度圖像的OpenCV中存取的像素值(的可能重複http://stackoverflow.com/questions/17919399/accessing-pixel-value-of-gray-scale- image-in-opencv) –

回答

6

您可以使用image.at<uchar>(j,i)來訪問灰度圖像的像素值。

3

cv::Mat::at<>()功能適用於所有類型的圖像,無論是單通道圖像還是多通道圖像。返回值的類型取決於提供給函數的模板參數。

灰度圖像的值可以這樣訪問:

//For 8-bit grayscale image. 
unsigned char value = image.at<unsigned char>(row, column); 

確保返回取決於圖像類型(8U,16U,32F等)正確的數據類型。

+1

好吧,除非它應該是'image.at (y,x)',因爲OpenCV使用了行,列。 –

+0

@RogerRowland ...是的,這是正確的。 – sgarizvi

+0

然後編輯你的答案,所以我可以upvote以良知;-) –

3
  • 對於IplImage* image,可以使用

    uchar intensity = CV_IMAGE_ELEM(image, uchar, y, x); 
    
  • 對於Mat image,可以使用

    uchar intensity = image.at<uchar>(y, x); 
    
-2

在(Y,X)] ++;

for(int i = 0; i < 256; i++) 
    cout<<histogram[i]<<" "; 

// draw the histograms 
int hist_w = 512; int hist_h = 400; 
int bin_w = cvRound((double) hist_w/256); 

Mat histImage(hist_h, hist_w, CV_8UC1, Scalar(255, 255, 255)); 

// find the maximum intensity element from histogram 
int max = histogram[0]; 
for(int i = 1; i < 256; i++){ 
    if(max < histogram[i]){ 
     max = histogram[i]; 
    } 
} 

// normalize the histogram between 0 and histImage.rows 

for(int i = 0; i < 255; i++){ 
    histogram[i] = ((double)histogram[i]/max)*histImage.rows; 
} 


// draw the intensity line for histogram 
for(int i = 0; i < 255; i++) 
{ 
    line(histImage, Point(bin_w*(i), hist_h), 
          Point(bin_w*(i), hist_h - histogram[i]), 
     Scalar(0,0,0), 1, 8, 0); 
} 

// display histogram 
namedWindow("Intensity Histogram", CV_WINDOW_AUTOSIZE); 
imshow("Intensity Histogram", histImage); 

namedWindow("Image", CV_WINDOW_AUTOSIZE); 
imshow("Image", image); 
waitKey(); 
return 0; 

}

+3

這個問題與直方圖無關。 –