|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import cv2 |
| 3 | +import numpy as np |
| 4 | +import plotCVImg |
| 5 | + |
| 6 | +IMAGE_WIDTH = 40 |
| 7 | +IMAGE_HEIGHT = 40 |
| 8 | +SUDOKU_SIZE = 9 |
| 9 | +N_MIN_ACTIVE_PIXELS = 30 |
| 10 | +SIZE_PUZZLE = IMAGE_WIDTH * SUDOKU_SIZE |
| 11 | +DEBUG = 0 |
| 12 | + |
| 13 | + |
| 14 | +def correct(img_original): |
| 15 | + # 灰度化 |
| 16 | + img_gray = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY) |
| 17 | + if DEBUG: |
| 18 | + plotCVImg.plotImg(img_gray, "gray") |
| 19 | + |
| 20 | + # 中值滤波 |
| 21 | + img_blur = cv2.medianBlur(img_gray, 1) |
| 22 | + if DEBUG: |
| 23 | + plotCVImg.plotImg(img_blur, "median Blur") |
| 24 | + |
| 25 | + # 高斯滤波 |
| 26 | + img_blur = cv2.GaussianBlur(img_blur, (3, 3), 0) |
| 27 | + if DEBUG: |
| 28 | + plotCVImg.plotImg(img_blur, "Gaussian Blur") |
| 29 | + |
| 30 | + # 将每个像素除以闭操作后的像素,可以调整图像亮度 |
| 31 | + kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (11, 11)) |
| 32 | + close = cv2.morphologyEx(img_blur, cv2.MORPH_CLOSE, kernel) |
| 33 | + div = np.float32(img_blur) / close |
| 34 | + img_brightness_adjust = np.uint8(cv2.normalize(div, div, 0, 255, cv2.NORM_MINMAX)) |
| 35 | + if DEBUG: |
| 36 | + plotCVImg.plotImg(img_brightness_adjust, "brightness adjust") |
| 37 | + |
| 38 | + # 自适应阈值二值化,注意其返回值只有一个 |
| 39 | + img_thresh = cv2.adaptiveThreshold(img_brightness_adjust, 255, |
| 40 | + cv2.ADAPTIVE_THRESH_GAUSSIAN_C, |
| 41 | + cv2.THRESH_BINARY_INV, 11, 7) |
| 42 | + if DEBUG: |
| 43 | + img_thresh = cv2.medianBlur(img_thresh, 3) |
| 44 | + plotCVImg.plotImg(img_thresh, "adaptive Threshold") |
| 45 | + |
| 46 | + # 寻找轮廓 |
| 47 | + binary, contours, hierarchy = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 48 | + if DEBUG: |
| 49 | + img_contours = img_original.copy() |
| 50 | + cv2.drawContours(img_contours, contours, -1, (0, 0, 255), 2) |
| 51 | + plotCVImg.plotImg(img_contours, "contours") |
| 52 | + |
| 53 | + # 找到最大轮廓 |
| 54 | + max_area = 0 |
| 55 | + biggest_contour = None |
| 56 | + for cnt in contours: |
| 57 | + area = cv2.contourArea(cnt) |
| 58 | + if area > max_area: |
| 59 | + max_area = area |
| 60 | + biggest_contour = cnt |
| 61 | + |
| 62 | + # mask操作 |
| 63 | + mask = np.zeros(img_brightness_adjust.shape, np.uint8) |
| 64 | + cv2.drawContours(mask, [biggest_contour], 0, 255, cv2.FILLED) |
| 65 | + cv2.drawContours(mask, [biggest_contour], 0, 0, 2) |
| 66 | + image_with_mask = cv2.bitwise_and(img_brightness_adjust, mask) |
| 67 | + if DEBUG: |
| 68 | + plotCVImg.plotImg(image_with_mask, "image_with_mask") |
| 69 | + |
| 70 | + # 角点检测 |
| 71 | + dst = cv2.cornerHarris(image_with_mask, 2, 3, 0.04) |
| 72 | + if DEBUG: |
| 73 | + plotCVImg.plotImg(dst, "image_cornerHarris") |
| 74 | + |
| 75 | + # x方向Sobel算子,膨胀操作连接断线,边缘检测找出竖线 |
| 76 | + dx = cv2.Sobel(image_with_mask, cv2.CV_16S, 1, 0) |
| 77 | + dx = cv2.convertScaleAbs(dx) |
| 78 | + cv2.normalize(dx, dx, 0, 255, cv2.NORM_MINMAX) |
| 79 | + ret, close = cv2.threshold(dx, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) |
| 80 | + kernelx = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 10)) |
| 81 | + close = cv2.morphologyEx(close, cv2.MORPH_DILATE, kernelx, iterations=1) |
| 82 | + |
| 83 | + binary, contour, hierarchy = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 84 | + for cnt in contour: |
| 85 | + x, y, w, h = cv2.boundingRect(cnt) |
| 86 | + if h / w > 5: |
| 87 | + cv2.drawContours(close, [cnt], 0, 255, -1) |
| 88 | + else: |
| 89 | + cv2.drawContours(close, [cnt], 0, 0, -1) |
| 90 | + |
| 91 | + close = cv2.morphologyEx(close, cv2.MORPH_CLOSE, None, iterations=2) |
| 92 | + closex = close.copy() |
| 93 | + |
| 94 | + # Y方向,找出横线 |
| 95 | + dy = cv2.Sobel(image_with_mask, cv2.CV_16S, 0, 2) |
| 96 | + dy = cv2.convertScaleAbs(dy) |
| 97 | + cv2.normalize(dy, dy, 0, 255, cv2.NORM_MINMAX) |
| 98 | + retVal, close = cv2.threshold(dy, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) |
| 99 | + kernely = cv2.getStructuringElement(cv2.MORPH_RECT, (10, 2)) |
| 100 | + close = cv2.morphologyEx(close, cv2.MORPH_DILATE, kernely) |
| 101 | + |
| 102 | + binary, contour, hierarchy = cv2.findContours(close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 103 | + for cnt in contour: |
| 104 | + x, y, w, h = cv2.boundingRect(cnt) |
| 105 | + if w / h > 5: |
| 106 | + cv2.drawContours(close, [cnt], 0, 255, -1) |
| 107 | + else: |
| 108 | + cv2.drawContours(close, [cnt], 0, 0, -1) |
| 109 | + |
| 110 | + close = cv2.morphologyEx(close, cv2.MORPH_DILATE, None, iterations=2) |
| 111 | + closey = close.copy() |
| 112 | + |
| 113 | + # 求x,y交点 |
| 114 | + res = cv2.bitwise_and(closex, closey) |
| 115 | + if DEBUG: |
| 116 | + plotCVImg.plotImg(res, "dots") |
| 117 | + |
| 118 | + # 查找轮廓,求每个轮廓的质心centroids |
| 119 | + img_dots = cv2.cvtColor(img_brightness_adjust, cv2.COLOR_GRAY2BGR) |
| 120 | + binary, contour, hierarchy = cv2.findContours(res, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE) |
| 121 | + centroids = [] |
| 122 | + for cnt in contour: |
| 123 | + if cv2.contourArea(cnt) > 20: |
| 124 | + mom = cv2.moments(cnt) |
| 125 | + (x, y) = int(mom['m10'] / mom['m00']), int(mom['m01'] / mom['m00']) |
| 126 | + cv2.circle(img_dots, (x, y), 4, (0, 255, 0), -1) |
| 127 | + centroids.append((x, y)) |
| 128 | + centroids = np.array(centroids, dtype=np.float32) |
| 129 | + c = centroids.reshape((100, 2)) |
| 130 | + c2 = c[np.argsort(c[:, 1])] |
| 131 | + |
| 132 | + b = np.vstack([c2[i * 10:(i + 1) * 10][np.argsort(c2[i * 10:(i + 1) * 10, 0])] for i in range(10)]) |
| 133 | + bm = b.reshape((10, 10, 2)) |
| 134 | + |
| 135 | + res2 = cv2.cvtColor(img_brightness_adjust, cv2.COLOR_GRAY2BGR) |
| 136 | + output = np.zeros((450, 450, 3), np.uint8) |
| 137 | + for i, j in enumerate(b): |
| 138 | + ri = i // 10 |
| 139 | + ci = i % 10 |
| 140 | + if ci != 9 and ri != 9: |
| 141 | + src = bm[ri:ri + 2, ci:ci + 2, :].reshape((4, 2)) |
| 142 | + dst = np.array([[ci * 50, ri * 50], [(ci + 1) * 50 - 1, ri * 50], [ci * 50, (ri + 1) * 50 - 1], |
| 143 | + [(ci + 1) * 50 - 1, (ri + 1) * 50 - 1]], np.float32) |
| 144 | + retval = cv2.getPerspectiveTransform(src, dst) |
| 145 | + warp = cv2.warpPerspective(res2, retval, (450, 450)) |
| 146 | + output[ri * 50:(ri + 1) * 50 - 1, ci * 50:(ci + 1) * 50 - 1] = warp[ri * 50:(ri + 1) * 50 - 1, |
| 147 | + ci * 50:(ci + 1) * 50 - 1].copy() |
| 148 | + img_correct = cv2.cvtColor(output, cv2.COLOR_BGR2GRAY) |
| 149 | + img_puzzle = cv2.adaptiveThreshold(img_correct, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 5, 7) |
| 150 | + img_puzzle = cv2.resize(img_puzzle, (SIZE_PUZZLE, SIZE_PUZZLE), interpolation=cv2.INTER_LINEAR) |
| 151 | + return img_puzzle |
| 152 | + |
| 153 | + |
| 154 | +def correct2(img_original): |
| 155 | + |
| 156 | + if DEBUG: |
| 157 | + plotCVImg.plotImg(img_original, "original") |
| 158 | + |
| 159 | + # gray image |
| 160 | + img_gray = cv2.cvtColor(img_original, cv2.COLOR_BGR2GRAY) |
| 161 | + if DEBUG: |
| 162 | + plotCVImg.plotImg(img_gray, "gray") |
| 163 | + |
| 164 | + # median Blur |
| 165 | + img_Blur = cv2.medianBlur(img_gray, 5) |
| 166 | + if DEBUG: |
| 167 | + plotCVImg.plotImg(img_Blur, "median Blur") |
| 168 | + |
| 169 | + # Gaussian Blur |
| 170 | + img_Blur = cv2.GaussianBlur(img_gray, (3, 3), 0) |
| 171 | + if DEBUG: |
| 172 | + plotCVImg.plotImg(img_Blur, "GaussianBlur") |
| 173 | + |
| 174 | + # adaptive threshold |
| 175 | + img_thresh = cv2.adaptiveThreshold(img_Blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 11, 2) |
| 176 | + if DEBUG: |
| 177 | + plotCVImg.plotImg(img_thresh, "adaptiveThreshold") |
| 178 | + |
| 179 | + # find the contours RETR_EXTERNAL |
| 180 | + binary, contours, hierarchy = cv2.findContours(img_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) |
| 181 | + if DEBUG: |
| 182 | + img_contours = img_original.copy() |
| 183 | + cv2.drawContours(img_contours, contours, -1, (0, 0, 255), 2) |
| 184 | + plotCVImg.plotImg(img_contours, "contours") |
| 185 | + |
| 186 | + # find the biggest contours |
| 187 | + size_rectangle_max = 0 |
| 188 | + index_biggest = 0 |
| 189 | + for i in range(len(contours)): |
| 190 | + size_rectangle = cv2.contourArea(contours[i]) |
| 191 | + # store the index of the biggest |
| 192 | + if size_rectangle > size_rectangle_max: |
| 193 | + size_rectangle_max = size_rectangle |
| 194 | + index_biggest = i |
| 195 | + |
| 196 | + # 多边形拟合 |
| 197 | + epsilon = 0.1 * cv2.arcLength(contours[index_biggest], True) |
| 198 | + biggest_rectangle = cv2.approxPolyDP(contours[index_biggest], epsilon, True) |
| 199 | + |
| 200 | + if DEBUG: |
| 201 | + # copy the original image to show the border |
| 202 | + img_border = img_original.copy() |
| 203 | + # 画出数独方格的边界 |
| 204 | + for x in range(len(biggest_rectangle)): |
| 205 | + cv2.line(img_border, |
| 206 | + (biggest_rectangle[(x % 4)][0][0], biggest_rectangle[(x % 4)][0][1]), |
| 207 | + (biggest_rectangle[((x + 1) % 4)][0][0], biggest_rectangle[((x + 1) % 4)][0][1]), |
| 208 | + (255, 0, 0), 2) |
| 209 | + plotCVImg.plotImg(img_border, "border") |
| 210 | + |
| 211 | + # sort the corners to remap the image |
| 212 | + def sortCornerPoints(rcCorners): |
| 213 | + point = rcCorners.reshape((4, 2)) |
| 214 | + mean = rcCorners.sum() / 8 |
| 215 | + cornerPoint = np.zeros((4, 2), dtype=np.float32) |
| 216 | + for i in range(len(point)): |
| 217 | + if point[i][0] < mean: |
| 218 | + if point[i][1] < mean: |
| 219 | + cornerPoint[0] = point[i] |
| 220 | + else: |
| 221 | + cornerPoint[2] = point[i] |
| 222 | + else: |
| 223 | + if point[i][1] < mean: |
| 224 | + cornerPoint[1] = point[i] |
| 225 | + else: |
| 226 | + cornerPoint[3] = point[i] |
| 227 | + return cornerPoint |
| 228 | + |
| 229 | + # 透视变换 |
| 230 | + cornerPoints = sortCornerPoints(biggest_rectangle) |
| 231 | + puzzlePoints = np.float32([[0, 0], [SIZE_PUZZLE, 0], [0, SIZE_PUZZLE], [SIZE_PUZZLE, SIZE_PUZZLE]]) |
| 232 | + PerspectiveMatrix = cv2.getPerspectiveTransform(cornerPoints, puzzlePoints) |
| 233 | + img_puzzle = cv2.warpPerspective(img_thresh, PerspectiveMatrix, (SIZE_PUZZLE, SIZE_PUZZLE)) |
| 234 | + if DEBUG: |
| 235 | + plotCVImg.plotImg(img_puzzle, "puzzle") |
| 236 | + |
| 237 | + return img_puzzle |
0 commit comments