[英]Get coordinates of white pixels (OpenCV)
在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());
有一个内置函数来做cv :: findNonZero
返回非零像素的位置列表。
给定二进制矩阵(可能从诸如cv::threshold()
, cv::compare()
, >
, ==
等操作返回)返回所有非零索引作为cv::Mat
或std::vector<cv::Point>
例如:
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];
你可以使用这种方法来获得白色像素..希望它能帮助你。
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.