Extract boxes from sudoku in opencv(从opencv中的数独中提取框)
问题描述
我已经使用 opencv 将数独图像转换为数独网格
现在我想从图像中提取每个框,最好的方法是什么?
据我所知,我正在尝试找到线的交点以找到每个框的角
这里的 findVerticalLines()
和 findHorizontalLines()
无法正确识别水平线和垂直线
假设最小盒子尺寸为 20*20
寻找水平线
寻找垂直线
合并并添加膨胀层以缩小小间隙
应用连通分量分析
可视化连接组件图像
如您所见,我们检测到一些文本也是框,我们可以通过简单的过滤条件轻松删除它们,这里我过滤的区域应至少为 1000 像素条件.
在检测到的盒子上绘制矩形.
最终输出图片
此答案基于我使用 OpenCV 在图像中查找复选框/表格的解决方案.您可以在我的 博客中找到详细说明走向数据科学.希望这将使您更接近解决方案.
编码愉快 :)
-- 编辑 1
进行连接组件可视化的代码
i have converted sudoku image into sudoku grid using opencv
now i want to extract each box from image what is best way to do this?
as per my knowledge i am trying to find intersection points of lines to find corner of each box
here
findVerticalLines()
andfindHorizontalLines()
are not able to dictect horizontal and vertical lines properly- original image
- masked image
- Canny edge dictation
- hough line transform
- horizontal lines
- Vertical Lines
解决方案One way to solve is to do a morphological operation to find vertical and horizontal lines from the canny edge image, then do a connected component analysis to find the boxes. I have done a sample version below. You can finetune it further to make it better. I started with the masked image as input.
Performing canny edge detection and adding a dilation layer
Now, the dilated binary image looks like this.
assuming minimum box size would be 20*20
finding horizontal lines
finding vertical lines
merging and adding a dilation layer to close small gaps
applying connected component analysis
visualising Connected component image
AS you can see, we have detected some text also as boxes, we can easily remove them with simple filter conditions, Here I'm filtering with the area should be minimum 1000 pixels condition.
drawing rectangles on the detected boxes.
final output image
This answer is based on my solution to find checkboxes/tables in an image using OpenCV. You can find a detailed explanation in my blog at Towards Data Science. Hope this will take you closer to a solution.
Happy coding :)
-- edit 1
code to do connected component visualisation
这篇关于从opencv中的数独中提取框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!