【问题标题】:openCV: Warp Perspective for various size of imagesopenCV:各种尺寸图像的翘曲透视图
【发布时间】:2020-09-10 22:03:13
【问题描述】:

我正在学习计算机视觉并尝试为 OCR 扭曲单张纸图片的透视图。示例图片为

我成功地对图像进行了二值化并检测了轮廓。然而,我很难根据轮廓包裹透视图。

def display_cv_image(image,  format='.png'):
    """
    Display image from 2d array
    """

    decoded_bytes = cv2.imencode(format, image)[1].tobytes()
    display(Image(data=decoded_bytes))
    
def get_contour(img,original, thresh):
    contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    areas = []
    for cnt in contours:
        area = cv2.contourArea(cnt)
        if area > 10000:
            epsilon = 0.1*cv2.arcLength(cnt,True)
            approx = cv2.approxPolyDP(cnt,epsilon,True)
            areas.append(approx)

    cv2.drawContours(original,areas,-1,(0,255,0),3)
    display_cv_image(original)

    return areas[0]

def perspective(original, target):
    dst = []

    pts1 = np.float32(target)
    pts2 = np.float32([[1000,2000],[1000,0],[0,0],[0,2000]])

    M = cv2.getPerspectiveTransform(pts1,pts2)
    dst = cv2.warpPerspective(original,M,(1000,2000))

    display_cv_image(dst)

# Driver codes
original = cv2.imread('image.jpg')
thresh, grey = binarize(original)
target = get_contour(grey,original, thresh)
perspective(original, target)

问题是pts2perspective 函数中。我正在尝试变量的多个值,但它们都不起作用。我想反向计算地图矩阵,并可能使函数适应各种大小的图像。

【问题讨论】:

    标签: python opencv cv2


    【解决方案1】:

    关于四点透视变换的很好的描述可以参考Adrian的教程:https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/

    imutils 模块中有一个函数four_point_transform

    就上图而言,以下是可以用于OCR输入的sn-p进行变形和二值化的代码。

    import cv2
    import numpy as np
    from imutils.perspective import four_point_transform
    import imutils
    
    original = cv2.imread('image.jpg')
    
    blurred = cv2.GaussianBlur(original, (3, 3), 0)
    blurred_float = blurred.astype(np.float32) / 255.0
    edgeDetector = cv2.ximgproc.createStructuredEdgeDetection('model.yml')
    edged = edgeDetector.detectEdges(blurred_float)
    edged = (255 * edged).astype("uint8")
    edged = cv2.threshold(edged, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]
    
    cnts = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
    cnts = cnts[0] if len(cnts) == 2 else cnts[1]
    cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
    
    for c in cnts:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:
            screenCnt = approx
            break
    
    if len(screenCnt) == 4:
        warped = four_point_transform(original, screenCnt.reshape(4, 2))
    
    warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
    T = cv2.ximgproc.niBlackThreshold(warped, maxValue=255, type=cv2.THRESH_BINARY_INV, blockSize=81, k=0.1, binarizationMethod=cv2.ximgproc.BINARIZATION_WOLF)
    warped = (warped > T).astype("uint8") * 255
    
    cv2.imshow("Original", imutils.resize(original, height = 650))
    cv2.imshow("Edged", imutils.resize(edged, height = 650))
    cv2.imshow("Warped", imutils.resize(warped, height = 650))
    cv2.waitKey(0)
    

    以下是原始、边缘和最终扭曲的二值化输出:

    请注意StructuredEdgeDetection 用于更好的边缘检测。您可以从以下链接下载model.yml 文件:https://cdn.rawgit.com/opencv/opencv_extra/3.3.0/testdata/cv/ximgproc/model.yml.gz

    另请注意,Wolf & Julion 二值化技术用于更好的输出。

    StructuredEdgeDetectionniBlackThreshold需要通过pip安装opencv-contrib-python包。

    【讨论】:

      猜你喜欢
      • 2013-07-01
      • 1970-01-01
      • 2019-09-16
      • 2011-05-31
      • 1970-01-01
      • 2017-04-17
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多