openCV+python实现倾斜校正

文章目录

  • 示例代码
  • 函数列表
  • 参考资料

示例代码

import cv2
import imutils
import numpy as np

img = cv2.imread(r'C:\Users\ml\Desktop\0.png')

result3 = img.copy()

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
dilate = cv2.dilate(blurred, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)))
edged = cv2.Canny(dilate, 30, 120, 3)            # 边缘检测

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 轮廓检测
cnts = cnts[0] if imutils.is_cv2() else cnts[1]  # 判断是opencv2还是opencv3
docCnt = None

if len(cnts) > 0:
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True) # 根据轮廓面积从大到小排序
    for c in cnts:
        peri = cv2.arcLength(c, True)                                       # 计算轮廓周长
        approx = cv2.approxPolyDP(c, 0.02*peri, True)           # 轮廓多边形拟合
        # 轮廓为4个点表示找到纸张
        if len(approx) == 4:
            docCnt = approx
            break
print(docCnt)
box = [docCnt[0][0],docCnt[1][0],docCnt[2][0],docCnt[3][0]]
rectangle_L = int(np.sqrt(pow((box[0][0]-box[1][0]), 2) + pow((box[0][1]-box[1][1]), 2)))
rectangle_W = int(np.sqrt(pow((box[0][0]-box[3][0]), 2) + pow((box[0][1]-box[3][1]), 2)))
print(rectangle_L)
print(rectangle_W)

src = np.float32(docCnt)
dst = np.float32([[0, 0], [0, rectangle_L], [rectangle_W, rectangle_L], [rectangle_W, 0]])
m = cv2.getPerspectiveTransform(src, dst)
result = cv2.warpPerspective(result3, m, (rectangle_W, rectangle_L))

cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import imutils
from imutils.perspective import four_point_transform

img = cv2.imread(r'C:\Users\ml\Desktop\0.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)       # 转化为灰度图,加快处理速度
binary = cv2.medianBlur(gray,7)       # 中值滤波
cv2.imshow('binary',binary)
# blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# cv2.imshow('blurred',blurred)
dilate = cv2.dilate(binary, cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)))
cv2.imshow('dilate',dilate)
edged = cv2.Canny(dilate, 30, 120, 3)            # 边缘检测
cv2.imshow('edged',edged)

cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)  # 轮廓检测
cnts = cnts[0] if imutils.is_cv2() else cnts[1]  # 判断是opencv2还是opencv3

docCnt = None

if len(cnts) > 0:
    cnts = sorted(cnts, key=cv2.contourArea, reverse=True) # 根据轮廓面积从大到小排序
    for c in cnts:
        peri = cv2.arcLength(c, True)                                       # 计算轮廓周长
        approx = cv2.approxPolyDP(c, 0.02*peri, True)           # 轮廓多边形拟合
        # 轮廓为4个点表示找到纸张
        if len(approx) == 4:
            docCnt = approx
            break
print(docCnt)
result = four_point_transform(img, docCnt.reshape(4,2)) # 对原始图像进行四点透视变换

cv2.imshow('result', result)
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import operator

ix,iy = 0,0
box =[]

img = cv2.imread(r'C:\Users\ml\Desktop\tng.jpeg')

# mouse callback function
def draw_circle(event,x,y,flags,param):
    global ix, iy
    if event == cv2.EVENT_LBUTTONDBLCLK:  # 设置鼠标事件
        ix, iy = x, y
        cv2.circle(img,(x,y), 6, (0,0,255), -1)
        cv2.imshow('image', img)
        
cv2.namedWindow('image')
cv2.imshow('image', img)
cv2.setMouseCallback('image',draw_circle)
print(ix,iy)
box.append((ix,iy))
print(box)
i=1

'''
while(1):
    i=i+1
    cv2.setMouseCallback('image',draw_circle)
    if operator.eq(box[i], box[1])==True:
        break
'''
cv2.waitKey(0)
cv2.destroyAllWindows()

函数列表

函数 作用
cv2.GaussianBlur() 高斯滤波:去除噪音,使图像变得平滑
cv2.cvtColor() 颜色空间转换
cv2.Canny() 边缘检测
cv2.approxPolyDP(curve, epsilon, closed) 多边形逼近
cv2.getPerspectiveTransform(src, dst) 透视变换

参考资料

资料1
资料2
资料3
资料4:利用透视变换进行图像纠正
资料5:傅里叶变换和透视变换
资料6:透视变换项目
资料7
文档
图像平滑
颜色空间转换
边缘检测
轮廓处理
鱼眼矫正

你可能感兴趣的:(openCV)