图像技术应用实战—全能扫描王(python)

from imutils.perspective import four_point_transform
import imutils
import cv2
import numpy as np
from matplotlib import pyplot as plt
import math

def Get_Outline(input_dir):
    image = cv2.imread(input_dir)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    blurred = cv2.GaussianBlur(gray, (5,5),0)
    edged = cv2.Canny(blurred,75,200)
    return image,gray,edged

def Get_cnt(edged):
    cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    cnts = imutils.grab_contours(cnts)
    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:
            docCnt = approx
            break   
    return docCnt
def calculate_distance(point1,point2):
    d_x = point1[0]-point2[0]
    d_y = point1[1]-point2[1]
    distance = math.sqrt(d_x**2+d_y**2)
    return distance

if __name__=="__main__":
    input_dir = "C:\\Users\\Lijian\\Desktop\\4.jpg"
    image,gray,edged = Get_Outline(input_dir)
    docCnt = Get_cnt(edged)
    print(docCnt.reshape(4,2))
    # result_img = four_point_transform(image, docCnt.reshape(4,2)) # 对原始图像进行四点透视变换
    # 改变变换的模式 公交卡的比例是16:9
    pts1 = np.float32(docCnt.reshape(4,2))
    # 加入一个判断,对不同宽高采用不同的系数
    p = docCnt.reshape(4,2)
    
    if calculate_distance(p[0],p[1])<calculate_distance(p[0],p[3]):
        pts2 = np.float32([[0,0],[0,180],[320,180],[320,0]])
        M = cv2.getPerspectiveTransform(pts1,pts2)
        dst = cv2.warpPerspective(image,M,(320,180))
    else:
        pts2 = np.float32([[0,0],[0,320],[180,320],[180,0]])
        M = cv2.getPerspectiveTransform(pts1,pts2)
        dst = cv2.warpPerspective(image,M,(180,320))
    #cv2.imwrite('0.png',dst)
    #print(result_img.shape)
    # -------画点----------
    point_size = 2
    point_color = (0, 255, 0) # BGR
    thickness = 2 # 可以为 0 、4、8
    for point in docCnt.reshape(4,2):
        cv2.circle(image, tuple(point), point_size, point_color, thickness)
    # # --------------
    cv2.imshow("original", image)
    cv2.imshow("gray", gray)
    cv2.imshow("edged", edged)
    cv2.imshow("result_img", dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows() 

# 备注,(1)顺序“左上,右上,右下,左下” 
# (2)变换后图片的大小变成点之间的距离,即缩小了

结果

原图

灰度图

canny边缘检测

校正后图像

 

  • 2
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

岁月蹉跎的一杯酒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值