【发布时间】:2017-02-08 08:29:30
【问题描述】:
我在位图扩展中有以下二进制图像:
现在我想:
- 获取顶部和底部白色像素(蓝色标记)的坐标
- 画一条与这两个坐标相交的线(黄线)(如下图所示)
我试过at函数image.at<uchar>(i,j)但没有成功。如果有人可以帮助我,我将不胜感激。提前致谢!
Mat image = imread("Before.bmp");
int i=1;
imshow("Before", image);
vector<Point> locations; // output, locations of non-zero pixels
cv::findNonZero(image, locations);
Point pnt = locations[i]; /Assertion error
for (int i = 0; i < image.rows; i++) {
for (int j = 0; j < image.cols; j++) {
if (image.at<uchar>(i,j) == 255 ) {
cout << i << ", " << j << endl; //give wrong coordinate
}
}
}
//imshow("black", image);
//imwrite("blackie.bmp", image);
waitKey(0);
return(0);
【问题讨论】:
-
尝试切换
i和j,比如image.at<uchar>(j,i) -
嗨@JeruLuke,感谢您的快速回复。不幸的是,这也没有奏效。我当前的代码使用 image.at
(i,j) 或 (j,i) 没有输出正确的白色像素坐标。你知道是什么原因造成的吗? -
cv::findNonZero怎么样?您可能只在第一行和最后一行运行它。 -
欢迎来到 Stack Overflow!请edit您的问题显示what you have tried so far。您应该包含您遇到问题的代码的minimal reproducible example,然后我们可以尝试帮助解决具体问题。您还应该阅读How to Ask。
-
@DanMašek 它输出错误:在 cv::findNonZero 中断言失败 (src.type() == CV_8UC1)。会不会是因为图片是位图扩展的?
标签: c++ opencv bitmap coordinate