Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7804915
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T02:03:54+00:00 2026-06-02T02:03:54+00:00

I was doing a fun project: Solving a Sudoku from an input image using

  • 0

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:

    enter image description here

    (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:

    enter image description here

  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:

enter image description here

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T02:03:55+00:00Added an answer on June 2, 2026 at 2:03 am

    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]]
    

    enter image description here

    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]]]
    

    enter image description here

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

    mask = FillingTransform[largestComponent]
    

    enter image description here

    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];
    

    enter image description here

    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 2×10 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]];
    

    enter image description here

    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}];
    

    enter image description here

    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]
    

    enter image description here

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a personal, just for fun, project that is using screen scraping to
I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
i was in the process of doing a fun project gathering music info/playable links
I'm doing just for fun an unread messages checker application in Delphi. I'm using
I am using EF4.1, have upgraded my project, generated POCO classes to use DbContext
I'm doing a small project for fun in C++ (in Ubuntu 11.04) and the
I'm doing a fun project in which I'm trying to redo some basic data
I'm .net newbie starting one project for fun. I'm using list box filled with
I am beginning some experimentation in writing a kernel and having fun doing it.
I'm currently doing some last-measure optimizations, mostly for fun and learning, and discovered something

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.