OpenCV:检测区域中的黑到白渐变
我上传了一个示例图片以便更好地理解: http://www.imagebanana.com/view /kaja46ko/test.jpg
在图像中,您可以看到一些扫描线和标记(其中有圆圈的白色矩形)。我希望 OpenCV 沿着指定区域(在示例中通过扫描线概述)移动,该区域应约为 5x5。如果该区域包含从黑到白的渐变,我希望 OpenCV 保存该区域的位置,以便稍后使用它。
最终结果将是区分标记和通过黑白线分隔的其他矩形。
这样的事情可能吗?我用谷歌搜索了很多,但只找到了边缘检测器,但这不是我想要的,我真的只需要检测黑白渐变。
提前致谢。
I uploaded an example image for better understanding: http://www.imagebanana.com/view/kaja46ko/test.jpg
In the image you can see some scanlines and a marker (the white retangle with the circle in it). I want OpenCV to go along a specified area (in the example outlined trough the scanlines) that should be around 5x5. If that area contains a gradient from black to white, I want OpenCV to save the position of that area, so that I can work with it later.
The final result would be to differentiate between the marker and the other retangles separated trough black and white lines.
Is something like that possible? I googled a lot but I only found edge detectors but that's not what I want, I really need the detection of the black to white gradient only.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通过计算直方图来过滤掉一些区域是个好主意。
您可以使用 cvCalcHist 来完成该任务,然后您可以建立一些阈值来确定黑白像素百分比是否对应于渐变的百分比。这不会解决任务,但会帮助您降低复杂性。
然后,您可以腐蚀图像以合并所有白色区域。应用阈值后,可以找到连接的组件(使用 cvFindContours),将黑色区域或白色区域中的图像分开。然后,您可以通过查找同时包含一块白色区域和黑色区域的 5x5 区域来检测渐变。
希望有帮助。
it would be a good idea to filter out some of the areas by calculating their histogram.
You can use cvCalcHist for the task, then you can establish some threshold to determine if the black-white pixels percentage corresponds to that of a gradient. This will not solve the task but it will help you in reducing complexity.
Then, you can erode the image to merge all the white areas. After applying threshold, it would be possible to find connected components (using cvFindContours) that will separate images in black zones or white zones. You can then detect gradients by finding 5x5 areas that contain both a piece of a white zone and black zone simultaneously.
hope it helps.
感谢您的回答 dnul,但这并没有真正帮助我解决这个问题。我想用直方图来解决这个问题,但这并不完全是我想要的。
我通过创建一个 40x40 矩阵解决了这个问题,该矩阵包含 5x5 矩阵,其中包含所有 3 个通道中的原始像素数据。我对每个 40 像素区域进行了迭代,并在 5 像素区域的每个边界内进行了迭代。我检查了每个像素并将比某个阈值暗的像素保存到存储中。
迭代之后,我大致了解了它们有多少黑色像素,因此我检查了每个像素的所有 3 个通道中是否有白色像素的邻居。然后我标记了每个像素并将它们保存到另一个存储中。
然后我使用 ransac 算法从这些点构造线。它为每个标记边缘构建大约 5-20 条线。然后,我查看了彼此相交的线,并保存了以直角相交的线的位置。
我从中得到的 4 个点是标记的边缘。
如果你想重现这一点,你必须事先过滤图像并应用阈值,以便更容易区分黑色和白色像素。
示例图片,在找到点之后和构造线之前保存:
http://www.imagebanana.com/view/i6gfe6qi/9.jpg
Thanks for your answerer dnul, but it didn't really help me work this out. I though about a histogram to approach the problem but it's not quite what I want.
I solved this problem by creating a 40x40 matrix which holds 5x5 matrix's containing the raw pixel data in all 3 channels. I iterated trough each 40px-area and inside iterated trough each border of 5px-area. I checked each pixel and saved the ones which are darker then a certain threshold a storage.
After the iteration I had a rough idea of how many black pixels their are, so I checked each one of them for neighbors with white-pixels in all 3 channels. I then marked each of those pixels and saved them to another storage.
I then used the ransac algorithm to construct lines out of these points. It constructs about 5-20 lines per marker edge. I then looked at the lines which meet each other and saved the position of those that meet in a square angle.
The 4 points I get from that are the edges of the marker.
If you want to reproduce this you would have to filter the image in beforehand and apply a threshold to make it easier to distinguish between black and white pixels.
A sample picture, save after finding the points and before constructing the lines:
http://www.imagebanana.com/view/i6gfe6qi/9.jpg
您所描述的是边缘检测。这正是 Canny 边缘检测器的工作原理。它会根据您传入的阈值查找亮像素附近的暗像素(还有自适应 canny,它会为您计算出阈值),并将它们设置为全黑或全白(也称为“标记”它们) )。
参见这里:
http://docs.opencv.org/doc/tutorials/imgproc /imgtrans/canny_detector/canny_detector.html
What you are describing is edge detection. This is exactly how, say, the Canny edge detector works. It looks for dark pixels near light pixels, and based on a threshold that you pass in (There is also the adaptive canny, which figures out the threshold for you), and sets them to all black or all white (aka 'marks' them).
See here:
http://docs.opencv.org/doc/tutorials/imgproc/imgtrans/canny_detector/canny_detector.html