-1

原图

在精明的图片之后

透视变换后的图片

你好,

我在做一个小的OCR POC。看原图,我只关心铭牌里面的内容。在识别字符之前,我需要对这张图片做透视变换以增加正确性。看第二张图片,我已经很精明地得到了矩形的轮廓。

我想得到矩形的 4 个角的坐标(用红色标记),这样我就可以导出矩阵并进行透视变换。最后一张图片是我想要的输出。

我是opencv的新手......有人可以给我一些关于如何获得4个角坐标的想法吗?我用谷歌搜索并学习了一些术语,例如霍夫变换?检测线然后计算拐角位置的好方法吗?

如果有人可以向我展示一些python代码来做到这一点,那就太好了,在此先感谢。

/* below is my currunt code
# coding:utf8
import cv2
import numpy as np
import sys

if __name__ == '__main__':
imagePath = sys.argv[1]
img = cv2.imread(imagePath)  

img = cv2.GaussianBlur(img,(3,3),0)  
canny = cv2.Canny(img, 50, 150)

#element2 = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 4))
#dilation = cv2.dilate(canny, element2, iterations = 1)
cv2.imwrite("canny.jpg", dilation)
cv2.waitKey(0)  
cv2.destroyAllWindows()
4

1 回答 1

1

我改编了pyimagesearch的代码以在 python 3.5 和 opencv 3.3 中工作

import os

import cv2
import imutils
import numpy as np
import pytesseract
from PIL import Image

def order_points(pts):
    # initialzie a list of coordinates that will be ordered
    # such that the first entry in the list is the top-left,
    # the second entry is the top-right, the third is the
    # bottom-right, and the fourth is the bottom-left
    rect = np.zeros((4, 2), dtype="float32")

    # the top-left point will have the smallest sum, whereas
    # the bottom-right point will have the largest sum
    s = pts.sum(axis=1)
    rect[0] = pts[np.argmin(s)]
    rect[2] = pts[np.argmax(s)]

    # now, compute the difference between the points, the
    # top-right point will have the smallest difference,
    # whereas the bottom-left will have the largest difference
    diff = np.diff(pts, axis=1)
    rect[1] = pts[np.argmin(diff)]
    rect[3] = pts[np.argmax(diff)]

    # return the ordered coordinates
    return rect


def four_point_transform(image, pts):
    # obtain a consistent order of the points and unpack them
    # individually
    rect = order_points(pts)
    (tl, tr, br, bl) = rect

    # compute the width of the new image, which will be the
    # maximum distance between bottom-right and bottom-left
    # x-coordiates or the top-right and top-left x-coordinates
    widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    maxWidth = max(int(widthA), int(widthB))

    # compute the height of the new image, which will be the
    # maximum distance between the top-right and bottom-right
    # y-coordinates or the top-left and bottom-left y-coordinates
    heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    maxHeight = max(int(heightA), int(heightB))

    # now that we have the dimensions of the new image, construct
    # the set of destination points to obtain a "birds eye view",
    # (i.e. top-down view) of the image, again specifying points
    # in the top-left, top-right, bottom-right, and bottom-left
    # order
    dst = np.array([
        [0, 0],
        [maxWidth - 1, 0],
        [maxWidth - 1, maxHeight - 1],
        [0, maxHeight - 1]], dtype="float32")

    # compute the perspective transform matrix and then apply it
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))

    # return the warped image
    return warped

def image_process(image_path):
    # Open image
    image = cv2.imread(image_path)
    ratio = image.shape[0] / 500.0
    orig = image.copy()
    image = imutils.resize(image, height=500)
     # Canny edge detect
    edged = cv2.Canny(image, 75, 200)
     # Find the countours
    img, cnts, hierarchy = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
     # Find the contours that are the largest (not sure if applies to this project) and has four components (is a rectangle)
    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
    warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)
    return warped


def main():
    image_path = None # You're going to need to change this
    image = image_process(image_path)
    cv2.imshow('image', image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()
于 2017-11-21T08:36:30.550 回答