[英]Accessing certain pixel's intensity value(grayscale image) in openCV
我剛剛意識到,在大量搜索OpenCv中如何訪問像素的強度值后,網上沒有任何內容。 灰度圖像。
大多數在線搜索都是關於如何訪問彩色圖像的BGR值,如下所示: 在openCV中訪問某些像素RGB值
image.at <>基本上是3個通道,即BGR,出於好奇,是否有另一種類似的方法來自OpenCV訪問灰度圖像的某個像素值?
您可以使用image.at<uchar>(j,i)
來訪問灰度圖像的像素值。
cv::Mat::at<>()
函數適用於每種類型的圖像,無論是單通道圖像還是多通道圖像。 返回的值類型取決於提供給函數的模板參數。
可以像這樣訪問灰度圖像的值:
//For 8-bit grayscale image.
unsigned char value = image.at<unsigned char>(row, column);
確保根據圖像類型(8u,16u,32f等)返回正確的數據類型。
對於IplImage* image
,您可以使用
uchar intensity = CV_IMAGE_ELEM(image, uchar, y, x);
對於Mat image
,您可以使用
uchar intensity = image.at<uchar>(y, x);
在(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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.