Skip to content

Instantly share code, notes, and snippets.

@jungilhan
Created October 22, 2013 16:24
Show Gist options
  • Save jungilhan/7103694 to your computer and use it in GitHub Desktop.
Save jungilhan/7103694 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
img = cv2.imread('sudoku.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (7, 7), 0)
thresh = cv2.adaptiveThreshold(gray, 255, 1, 1, 7, 2)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
biggest = None
max_area = 0
for i in contours:
area = cv2.contourArea(i)
if area > 100:
peri = cv2.arcLength(i, True)
approx = cv2.approxPolyDP(i, 0.02 * peri, True)
if area > max_area and len(approx) == 4:
cv2.drawContours(img,[i], 0, (0, 255, 0), 2)
biggest = approx
max_area = area
cv2.imshow('img', img)
cv2.imshow('tresh', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment