【问题标题】:Get coordinates of white pixels (OpenCV)获取白色像素的坐标(OpenCV)
【发布时间】:2016-04-30 22:36:43
【问题描述】:

在 OpenCV (C++) 中,我有一个黑白图像,其中一些形状看起来充满了白色 (255)。知道了这一点,我怎样才能得到这些对象在图像中的坐标点?我有兴趣获取所有白色像素坐标。

还有比这更干净的方法吗?

std::vector<int> coordinates_white; // will temporaly store the coordinates where "white" is found 
for (int i = 0; i<img_size.height; i++) {
    for (int j = 0; j<img_size.width; j++) {
        if (img_tmp.at<int>(i,j)>250) {
            coordinates_white.push_back(i);
            coordinates_white.push_back(j);
        }
    }
}
// copy the coordinates into a matrix where each row represents a *(x,y)* pair
cv::Mat coordinates = cv::Mat(coordinates_white.size()/2,2,CV_32S,&coordinates_white.front());

【问题讨论】:

    标签: c++ opencv matrix threshold


    【解决方案1】:

    您可以使用此方法获取白色像素.. 希望对您有所帮助。

     for(int i = 0 ;i <image.rows() ; i++){// image:the binary image
                    for(int j = 0; j< image.cols() ; j++){
                        double[] returned = image.get(i,j); 
                        int value = (int) returned[0]; 
                        if(value==255){
                        System.out.println("x: " +i + "\ty: "+j);//(x,y) coordinates
                        }
                    }
    
                }
    

    【讨论】:

      【解决方案2】:

      有一个内置函数可以做到这一点cv::findNonZero

      返回非零像素的位置列表。

      给定一个二进制矩阵(可能从cv::threshold()cv::compare()&gt;== 等操作返回)将所有非零索引返回为cv::Matstd::vector&lt;cv::Point&gt;

      例如:

      cv::Mat binaryImage; // input, binary image
      cv::Mat locations;   // output, locations of non-zero pixels
      cv::findNonZero(binaryImage, locations);
      // access pixel coordinates
      Point pnt = locations.at<Point>(i);
      

      cv::Mat binaryImage; // input, binary image
      vector<Point> locations;   // output, locations of non-zero pixels
      cv::findNonZero(binaryImage, locations);
      // access pixel coordinates
      Point pnt = locations[i];
      

      【讨论】:

        猜你喜欢
        • 2012-01-16
        • 2017-07-19
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 1970-01-01
        • 2018-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多