如何清除数独方块中的凸面缺陷? [英] How to remove convexity defects in a Sudoku square?

查看:1218
本文介绍了如何清除数独方块中的凸面缺陷?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个有趣的项目:使用OpenCV从输入图像解决数独(如Google Goggles等)。我已经完成了任务,但是最后我发现了一个小问题,我来到这里。



我使用OpenCV 2.3.1的Python API编程。



以下是我所做的:


  1. 阅读图片

  2. 查找轮廓

  3. 选择具有最大面积的一个(也有点等价于方形)。

  4. 查找角点。



    例如



    注意绿线正确地与数独的真正边界重合,因此数独可以正确弯曲。 / p>



  5. >


  6. 执行OCR(为此,我使用了我在中提供的方法) OpenCV-Python中的简单数字识别OCR




问题:



查看此图片



此图片上的第4步显示的结果如下:





绘制的红线是原始轮廓,是数独边界的真实轮廓。



绘制的绿线是近似的轮廓,将是变形图像的轮廓。



当然,绿线和红线在顶部有区别数独边缘。

$ b

如何在Sudoku的正确边界上扭曲图像,即红线OR如何消除红线和绿线之间的差异?在OpenCV中有这样的方法吗?

解决方案

我有一个解决方案,但是你必须翻译OpenCV自身。它是用Mathematica写的。



第一步是调整图像的亮度,将每个像素与结束操作的结果分开:

  src = ColorConvert [Import [http://davemark.com/images/sudoku.jpg],Grayscale]; 
white = Closing [src,DiskMatrix [5]];
srcAdjusted = Image [ImageData [src] / ImageData [white]]



下一步是找到数独面积,所以我可以忽略(掩蔽掉)背景。为此,我使用连通分量分析,并选择具有最大凸区域的组件:

  components = 
ComponentMeasurements [
ColorNegate @ Binarize [srcAdjusted],{ConvexArea,Mask}] [[All,
2]];
maximumComponent = Image [SortBy [components,First] [[ - 1,2]]]



填写此image,我得到数独网格的掩码:

  mask = FillingTransform [largestComponent] 



现在,我可以使用二阶导数滤波器在两个单独的图像中找到垂直和水平线:

  lY = ImageMultiply [MorphologicalBinarize [GaussianFilter [srcAdjusted,3,{2,0}],{0.02,0.05}], 
lX = ImageMultiply [MorphologicalBinarize [GaussianFilter [srcAdjusted,3,{0,2}],{0.02,0.05}],mask];



我再次使用连接分量分析从这些图像中提取网格线。网格线比数字长得多,因此我可以使用卡尺长度来仅选择网格线连接的组件。按位置排序,我得到图像中每个垂直/水平网格线的2x10掩模图像:

  verticalGridLineMasks = 
SortBy [ComponentMeasurements [
lX,{CaliperLength,Centroid,Mask},#> 100&] [[All,
2]],#[[2,1]]&] [[All,3]];
horizo​​ntalGridLineMasks =
SortBy [ComponentMeasurements [
lY,{CaliperLength,Centroid,Mask},#> 100&] [[All,
2]],#[[2,2]]&] [[All,3]];



接下来,我取每对垂直/水平网格线,扩大它们,计算逐像素交集,结果的中心。这些点是网格线交点:

  centerOfGravity [l_]:= 
ComponentMeasurements [Image [l] centos] [[1,2]]
gridCenters =
表[centerOfGravity [
ImageData [Dilation [Image [h],DiskMatrix [2]]] *
ImageData [ Dilation [Image [v],DiskMatrix [2]]],{h,
horizo​​ntalGridLineMasks},{v,verticalGridLineMasks}];



最后一步是通过这些点为X / Y映射定义两个插值函数,并使用以下函数变换图像:

  fnX = ListInterpolation [gridCenters [[All,All,1] 
fnY = ListInterpolation [gridCenters [[All,All,2]]];
transformed =
ImageTransformation [
srcAdjusted,{fnX @@ Reverse [#],fnY @@ Reverse [#]}&,{9 * 50,9 * 50},
PlotRange - > {{1,10},{1,10}},DataRange - > Full]



所有操作都是基本的图像处理功能,因此在OpenCV中也应该可以。基于样条的图像转换可能更难,但我不认为你真的需要它。可能使用现在对每个单元格使用的透视变换将给出足够好的结果。


I was doing a fun project: Solving a Sudoku from an input image using OpenCV (as in Google goggles etc). And I have completed the task, but at the end I found a little problem for which I came here.

I did the programming using Python API of OpenCV 2.3.1.

Below is what I did :

  1. Read the image
  2. Find the contours
  3. Select the one with maximum area, ( and also somewhat equivalent to square).
  4. Find the corner points.

    e.g. given below:

    (Notice here that the green line correctly coincides with the true boundary of the Sudoku, so the Sudoku can be correctly warped. Check next image)

  5. warp the image to a perfect square

    eg image:

  6. Perform OCR ( for which I used the method I have given in Simple Digit Recognition OCR in OpenCV-Python )

And the method worked well.

Problem:

Check out this image.

Performing the step 4 on this image gives the result below:

The red line drawn is the original contour which is the true outline of sudoku boundary.

The green line drawn is approximated contour which will be the outline of warped image.

Which of course, there is difference between green line and red line at the top edge of sudoku. So while warping, I am not getting the original boundary of the Sudoku.

My Question :

How can I warp the image on the correct boundary of the Sudoku, i.e. the red line OR how can I remove the difference between red line and green line? Is there any method for this in OpenCV?

解决方案

I have a solution that works, but you'll have to translate it to OpenCV yourself. It's written in Mathematica.

The first step is to adjust the brightness in the image, by dividing each pixel with the result of a closing operation:

src = ColorConvert[Import["http://davemark.com/images/sudoku.jpg"], "Grayscale"];
white = Closing[src, DiskMatrix[5]];
srcAdjusted = Image[ImageData[src]/ImageData[white]]

The next step is to find the sudoku area, so I can ignore (mask out) the background. For that, I use connected component analysis, and select the component that's got the largest convex area:

components = 
  ComponentMeasurements[
    ColorNegate@Binarize[srcAdjusted], {"ConvexArea", "Mask"}][[All, 
    2]];
largestComponent = Image[SortBy[components, First][[-1, 2]]]

By filling this image, I get a mask for the sudoku grid:

mask = FillingTransform[largestComponent]

Now, I can use a 2nd order derivative filter to find the vertical and horizontal lines in two separate images:

lY = ImageMultiply[MorphologicalBinarize[GaussianFilter[srcAdjusted, 3, {2, 0}], {0.02, 0.05}], mask];
lX = ImageMultiply[MorphologicalBinarize[GaussianFilter[srcAdjusted, 3, {0, 2}], {0.02, 0.05}], mask];

I use connected component analysis again to extract the grid lines from these images. The grid lines are much longer than the digits, so I can use caliper length to select only the grid lines-connected components. Sorting them by position, I get 2x10 mask images for each of the vertical/horizontal grid lines in the image:

verticalGridLineMasks = 
  SortBy[ComponentMeasurements[
      lX, {"CaliperLength", "Centroid", "Mask"}, # > 100 &][[All, 
      2]], #[[2, 1]] &][[All, 3]];
horizontalGridLineMasks = 
  SortBy[ComponentMeasurements[
      lY, {"CaliperLength", "Centroid", "Mask"}, # > 100 &][[All, 
      2]], #[[2, 2]] &][[All, 3]];

Next I take each pair of vertical/horizontal grid lines, dilate them, calculate the pixel-by-pixel intersection, and calculate the center of the result. These points are the grid line intersections:

centerOfGravity[l_] := 
 ComponentMeasurements[Image[l], "Centroid"][[1, 2]]
gridCenters = 
  Table[centerOfGravity[
    ImageData[Dilation[Image[h], DiskMatrix[2]]]*
     ImageData[Dilation[Image[v], DiskMatrix[2]]]], {h, 
    horizontalGridLineMasks}, {v, verticalGridLineMasks}];

The last step is to define two interpolation functions for X/Y mapping through these points, and transform the image using these functions:

fnX = ListInterpolation[gridCenters[[All, All, 1]]];
fnY = ListInterpolation[gridCenters[[All, All, 2]]];
transformed = 
 ImageTransformation[
  srcAdjusted, {fnX @@ Reverse[#], fnY @@ Reverse[#]} &, {9*50, 9*50},
   PlotRange -> {{1, 10}, {1, 10}}, DataRange -> Full]

All of the operations are basic image processing function, so this should be possible in OpenCV, too. The spline-based image transformation might be harder, but I don't think you really need it. Probably using the perspective transformation you use now on each individual cell will give good enough results.

这篇关于如何清除数独方块中的凸面缺陷?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆