python写透视挂_如何用Python openCV 用透视变换的方法对图像进行矫正

.需要矫正的图片1

需要矫正的图

矫正后的结果:

矫正后的图

需要矫正的图片2

矫正前

矫正后

# import the necessary packages

from imutils.perspectiveimport four_point_transform

from imutilsimport contours

import numpyas np

import argparse

import imutils

import cv2

# construct the argument parse and parse the arguments

# ap = argparse.ArgumentParser()

# ap.add_argument("-i", "--image", required=True,

#    help="path to the input image")

# args = vars(ap.parse_args())

# define the answer key which maps the question number

# to the correct answer

ANSWER_KEY = {0:1,1:4,2:0,3:3,4:1}

# load the image, convert it to grayscale, blur it

# slightly, then find edges

image = cv2.imread("changeRect.png")

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

blurred = cv2.GaussianBlur(gray, (5,5),0)

edged = cv2.Canny(blurred,75,200)

#find contours in edge map ,then initialize the contour corresponds to the document

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,

cv2.CHAIN_APPROX_SIMPLE)

cnts = cnts[0] if  imutils.is_cv2()  else   cnts[1]

docCnt =None

# ensure that at least one contour was found

if      len(cnts) > 0:

# sort the contours according to their size in

# descending order

cnts =sorted(cnts,key=cv2.contourArea,reverse=True)

# loop over the sorted contours

for cin cnts:

# approximate the contour

peri = cv2.arcLength(c,True)

approx = cv2.approxPolyDP(c,0.02 * peri,True)

# if our approximated contour has four points,

# then we can assume we have found the paper

if len(approx) ==4:

docCnt = approx

break

# apply a four point perspective transform to both the

# original image and grayscale image to obtain a top-down

# birds eye view of the paper

paper = four_point_transform(image, docCnt.reshape(4,2))

warped = four_point_transform(gray, docCnt.reshape(4,2))

cv2.imshow("original", image)

cv2.imshow("Exam", paper)

cv2.waitKey(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值