【问题标题】:Accessing certain pixel's intensity value(grayscale image) in openCV在openCV中访问某些像素的强度值(灰度图像)
【发布时间】:2014-01-22 15:29:05
【问题描述】:

在对如何在 OpenCv 中访问像素的强度值进行了大量搜索之后,我才意识到网络上什么都没有。一张灰度图。

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

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

【问题讨论】:

标签: c++ opencv image-processing


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    • 对于IplImage* image,可以使用

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

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

    【讨论】:

      【解决方案3】:

      cv::Mat::at&lt;&gt;() 函数适用于各种类型的图像,无论是单通道图像还是多通道图像。返回值的类型仅取决于提供给函数的模板参数。

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

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

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

      【讨论】:

      • 好的,除了应该是image.at&lt;unsigned char&gt;(y, x),因为OpenCV使用行列。
      • 然后编辑你的答案,这样我就可以问心无愧地投票了 ;-)
      【解决方案4】:

      at(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;
      

      }

      【讨论】:

      • 这个问题与直方图无关。
      猜你喜欢
      • 2013-07-28
      • 1970-01-01
      • 1970-01-01
      • 2021-02-07
      • 2015-10-15
      • 2012-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多