opencv python 角點檢測與透視變換

圖片下載地址:
鏈接: https://pan.baidu.com/s/1peIDDBffc84XYsYkrdfuwg 提取碼: ibgk

import cv2
import numpy as np


def get_contour(img):
    """獲取連通域
    :param img: 輸入圖片
    :return: 最大連通域
    """
    ret, img_bin = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)

    contours, hierarchy = cv2.findContours(img_bin, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    areas = []
    for i in range(len(contours)):
        area = cv2.contourArea(contours[i])
        print("輪廓 %d 的面積是:%d" % (i, area))

        areas.append(area)
    index = np.argmax(areas)

    return img_bin, contours[index]


def resize_show_image(img_name, image):
    """
        縮放顯示圖片
    :param img_name:  顯示名稱
    :param image:  圖片名稱
    """
    cv2.namedWindow(img_name, 0)
    cv2.resizeWindow(img_name, 1075, 900)
    cv2.imshow(img_name, image)


def get_cornerHarris(img_src):
    """
        獲取圖像角點
    :param img_src: 處理圖像
    :return: 角點圖像
    """
    img_corner = np.zeros(img_src.shape, np.uint8)
    img_gray = img_src.copy()
    img_gray = np.float32(img_gray)

    img_dist = cv2.cornerHarris(img_gray, 5, 5, 0.04)
    img_dist = cv2.dilate(img_dist, None)

    img_corner[img_dist > 0.01 * img_dist.max()] = [255]

    return img_corner


def get_warpPerspective(points, image_src):
    """
    執行透視變換
    :param points: 輸入的四個角點
    :param image_src: 輸入的圖片
    :return: 變換後的圖片
    """
    src_point = np.float32([
        [points[2][0], points[2][1]],
        [points[3][0], points[3][1]],
        [points[1][0], points[1][1]],
        [points[0][0], points[0][1]]])
    width = 1920
    height = 1080
    dst_point = np.float32([[0, 0], [width - 1, 0],
                            [0, height - 1], [width - 1, height - 1]])

    perspective_matrix = cv2.getPerspectiveTransform(src_point, dst_point)

    img_dst = cv2.warpPerspective(image_src, perspective_matrix, (width, height))
    return img_dst


def main():
    # 讀取圖片
    img_white = cv2.imread("./image/image_white.jpg", cv2.IMREAD_GRAYSCALE)
    img_book = cv2.imread("./image/sin0.bmp", cv2.IMREAD_GRAYSCALE)

    resize_show_image("img_book", img_book)

    # 最大的輪廓
    img_bin, contour = get_contour(img_white)

    # 處理區域mask
    mask = np.zeros(img_white.shape, np.uint8)
    mask = cv2.drawContours(mask, [contour], 0, (255, 255, 255), -1)
    # resize_show_image("img_mask", mask)

    # 獲取區域角點圖片
    img_corner = get_cornerHarris(img_white)

    # 膨脹和 mask與角點操作
    kernel = np.ones((5, 5), np.uint8)
    img_mask = cv2.dilate(mask, kernel)
    img_corner = cv2.bitwise_and(img_mask, img_corner)
    resize_show_image("image_corner", img_corner)

    # 獲取四個角點的中心座標
    contours, hierarchy = cv2.findContours(img_corner, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)

    points = []
    for i in range(len(contours)):
        center, radius = cv2.minEnclosingCircle(contours[i])
        points.append(center)
    print(points)

    # 透視變換
    img_dst = get_warpPerspective(points, img_book)
    resize_show_image("image_dst", img_dst)

    cv2.waitKey()
    cv2.destroyAllWindows()


if __name__ == '__main__':
    main()


在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章