透视变换 OMR Sheet - OpenCV Python

Perspective Transform OMR Sheet - OpenCV Python

我正在尝试在 python 中使用 OpenCV 进行透视变换。我想对齐图像并找到左上角右上角和左下角和右下角轮廓的坐标。到目前为止,这是我的代码,我可以在其中识别所有轮廓。

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(3,3),0)
edges = cv2.Canny(blur,50,100)
contours, hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #find contours
cv2.drawContours(img,contours,-1,(0,255,0),2)
cv2.imshow('Contours',img)
cv2.waitKey(0)

这是我得到的图像。

如果你能帮我解决这个问题,我将不胜感激!

Original Image
演示输入:
Demo Inputs 演示输入 1:
Demo Input 1
演示输入 2:
Demo Input 2
演示输入 3:
Demo Input 3
演示输入 4:
Demo Input 4
期望的输出:
Desired Output

这是 Python/OpenCV 中的一种方法。

  • 读取输入
  • 阅读模板(针对其维度)
  • 将输入转换为灰度和阈值
  • 填充阈值(应用形态时保留角)
  • 应用形态关闭
  • 删除填充
  • 获取最大外轮廓
  • 获取其周长并近似为4个角作为变形的输入角
  • 从模板的维度获取变形的输出角
  • 获取透视变换矩阵
  • 扭曲输入以匹配模板
  • 保存结果

输入:

模板:

import cv2
import numpy as np

# read image
img = cv2.imread("omr_test.jpg")
hh, ww = img.shape[:2]

# read template
template = cv2.imread("omr_template.jpg")
ht, wd = template.shape[:2]

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# do otsu threshold on gray image
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# pad thresh with black to preserve corners when apply morphology
pad = cv2.copyMakeBorder(thresh, 20, 20, 20, 20, borderType=cv2.BORDER_CONSTANT, value=0)

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
morph = cv2.morphologyEx(pad, cv2.MORPH_CLOSE, kernel)

# remove padding
morph = morph[20:hh+20, 20:ww+20]

# get largest external contour
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# get perimeter and approximate a polygon
peri = cv2.arcLength(big_contour, True)
corners = cv2.approxPolyDP(big_contour, 0.04 * peri, True)

# draw polygon on input image from detected corners
polygon = img.copy()
cv2.polylines(polygon, [corners], True, (0,255,0), 2, cv2.LINE_AA)
# Alternate: cv2.drawContours(page,[corners],0,(0,0,255),1)

# print the number of found corners and the corner coordinates
# They seem to be listed counter-clockwise from the top most corner
print(len(corners))
print(corners)

# reformat input corners to x,y list
icorners = []
for corner in corners:
    pt = [ corner[0][0],corner[0][1] ]
    icorners.append(pt)
icorners = np.float32(icorners)

# get corresponding output corners form width and height
ocorners = [ [0,0], [0,ht], [wd,ht], [wd,0] ]
ocorners = np.float32(ocorners)

# get perspective tranformation matrix
M = cv2.getPerspectiveTransform(icorners, ocorners)

# do perspective 
warped = cv2.warpPerspective(img, M, (wd, ht))

# write results
cv2.imwrite("omr_test_thresh.jpg", thresh)
cv2.imwrite("omr_test_morph.jpg", morph)
cv2.imwrite("omr_test_polygon.jpg", polygon)
cv2.imwrite("omr_test_warped.jpg", warped)

# display it
cv2.imshow("thresh", thresh)
cv2.imshow("pad", pad)
cv2.imshow("morph", morph)
cv2.imshow("polygon", polygon)
cv2.imshow("warped", warped)
cv2.waitKey(0)

阈值图像:

形态图像:

多边形图像:

扭曲的输入:

这里有一个修正,解决输入图像在Python/OpenCV中是顺时针旋转还是逆时针旋转。

  • 读取输入
  • 阅读模板(尺寸)
  • 将输入转换为灰度和阈值
  • 填充阈值(应用形态学时保​​留角)
  • 应用形态关闭
  • 删除填充
  • 获取最大外轮廓
  • 获取其周长并近似为4个角作为变形的输入角
  • 按 Y 对角进行排序并测试 X 的前两个排序角。如果 diff=X2-X1 为负,则输出角需要以稍微不同的顺序列出。
  • 根据模板的尺寸和排序角的差异获取变形的输出角。
  • 获取透视变换矩阵
  • 扭曲输入以匹配模板
  • 保存结果

输入 1(顺时针旋转):

:

输入 2(逆时针旋转):

import cv2
import numpy as np

# read image
#img = cv2.imread("omr_test.jpg")
img = cv2.imread("omr_test2.jpg")
hh, ww = img.shape[:2]

# read template
template = cv2.imread("omr_template.jpg")
ht, wd = template.shape[:2]

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# do otsu threshold on gray image
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]

# pad thresh with black to preserve corners when apply morphology
pad = cv2.copyMakeBorder(thresh, 20, 20, 20, 20, borderType=cv2.BORDER_CONSTANT, value=0)

# apply morphology
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (15,15))
morph = cv2.morphologyEx(pad, cv2.MORPH_CLOSE, kernel)

# remove padding
morph = morph[20:hh+20, 20:ww+20]

# get largest external contour
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)

# get perimeter and approximate a polygon
peri = cv2.arcLength(big_contour, True)
corners = cv2.approxPolyDP(big_contour, 0.04 * peri, True)

# draw polygon on input image from detected corners
polygon = img.copy()
cv2.polylines(polygon, [corners], True, (0,255,0), 2, cv2.LINE_AA)

# print the number of found corners and the corner coordinates
# They seem to be listed counter-clockwise from the top most corner
print(len(corners))
print(corners)

# reformat input corners to x,y list
sortcorners = []
for corner in corners:
    pt = [ corner[0][0],corner[0][1] ]
    sortcorners.append(pt)
icorners = np.float32(sortcorners)

# sort corners on y
def takeSecond(elem):
    return elem[1]
sortcorners.sort(key=takeSecond)

# check if second corner x is left or right of first corner x
x1 = sortcorners[0][0]
x2 = sortcorners[1][0]
diff = x2 - x1
print(x1, x2)

# get corresponding output corners from width and height
if diff >= 0:
    ocorners = [ [0,0], [0,ht], [wd,ht], [wd,0] ]
else:
    ocorners = [ [wd,0], [0,0], [0,ht], [wd,ht]]
ocorners = np.float32(ocorners)

# get perspective tranformation matrix
M = cv2.getPerspectiveTransform(icorners, ocorners)

# do perspective 
warped = cv2.warpPerspective(img, M, (wd, ht))

# write results
cv2.imwrite("omr_test2_thresh.jpg", thresh)
cv2.imwrite("omr_test2_morph.jpg", morph)
cv2.imwrite("omr_test2_polygon.jpg", polygon)
cv2.imwrite("omr_test2_warped.jpg", warped)

# display it
cv2.imshow("thresh", thresh)
cv2.imshow("pad", pad)
cv2.imshow("morph", morph)
cv2.imshow("polygon", polygon)
cv2.imshow("warped", warped)
cv2.waitKey(0)

第一次输入的结果(顺时针旋转):

第二个输入的结果(逆时针旋转):