在openCV中访问某些像素的强度值(灰度图像)

时间:2014-01-22 15:29:06

标签: c++ opencv image-processing

我刚刚意识到在搜索了如何在OpenCv中访问像素的强度值后,网上没有任何内容。灰度图像。

大多数在线搜索都是关于如何访问彩色图像的BGR值,如下所示:Accessing certain pixel RGB value in openCV

image.at<>基本上对于3个通道,即BGR,出于好奇,OpenCV是否有另一种类似的方法来访问灰度图像的某个像素值?

4 个答案:

答案 0 :(得分:6)

您可以使用image.at<uchar>(j,i)访问灰度图像的像素值。

答案 1 :(得分:3)

cv::Mat::at<>()功能适用于每种类型的图像,无论是单通道图像还是多通道图像。返回的值类型取决于提供给函数的模板参数。

可以像这样访问灰度图像的值:

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

确保根据图像类型(8u,16u,32f等)返回正确的数据类型。

答案 2 :(得分:3)

  • 对于IplImage* image,您可以使用

    uchar intensity = CV_IMAGE_ELEM(image, uchar, y, x);
    
  • 对于Mat image,您可以使用

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

答案 3 :(得分:-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;

}

相关问题