使用opencv+python 实现图像的斜向矫正

功能说明

参考博文:https://blog.csdn.net/WZZ18191171661/article/details/99174861

在处理现实生活中的图像处理问题时,我们经常会遇到一种情况-即我们将要处理的目标的位置是斜的,我们需要使用仿射变换进行矫正。当你做了很多现实场景中的案例之后,你就会发现这是一个非常通用的模块,因而本篇博客针对这个问题进行了详细的论述,具体的案例如下图所示,左边表示的是原始的输入图片,该图片中的目标是斜放的,我们要做的任务就是将其矫正过来。

代码实现

python 复制代码
import cv2
import numpy as np
import time
from pyzbar.pyzbar import decode



def order_points(pts):
	# 初始化坐标点
	rect = np.zeros((4, 2), dtype = "float32")

	# 获取左上角和右下角坐标点
	s = pts.sum(axis = 1)
	rect[0] = pts[np.argmin(s)]
	rect[2] = pts[np.argmax(s)]

	# 分别计算左上角和右下角的离散差值
	diff = np.diff(pts, axis = 1)
	rect[1] = pts[np.argmin(diff)]
	rect[3] = pts[np.argmax(diff)]

	return rect

def four_point_transform(image, pts):

    rect = order_points(pts)
    (tl, tr, br, bl) = rect

    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))

	# 计算新图片的高度值,选取垂直差值的最大值
    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))

    # # 构建新图片的4个坐标点
    dst = np.array([
		[0, 0],
		[maxWidth - 1, 0],
		[maxWidth - 1, maxHeight - 1],
		[0, maxHeight - 1]], dtype = "float32")

    M = cv2.getPerspectiveTransform(rect, dst)

    warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
    
    
    return warped


if __name__ == '__main__':
    # img = cv2.imread("bar.jpg")

    img_path = '../picture_2025-04-09_14-56-03-518_ORI.png'
    

    img=cv2.imread(img_path)
  



    # 将图像转换为灰度图
    gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

    box_small= [[1672,414],[2424,400],[2424,472],[1672,486]]
    print(type(box_small))


    box = np.array(box_small).astype(np.int32)

    for i in range(100):

        start_time = time.perf_counter()

        warped=four_point_transform(img, box)

        end_time = time.perf_counter()
        elapsed_time = (end_time - start_time)*1000
        print("从收图像到检测出结果回传结果用的时间!!!!!!!!!!: {} ms".format(elapsed_time))

    # cv2.polylines(img, [box], True, (0, 255, 0), 2)
    # print("绘制完成")


    # cv2.imwrite('E:/code/1.png',img)
    # cv2.imwrite('E:/code/2.png',warped)
相关推荐
爱数模的小驴6 小时前
2025 年“认证杯”数学中国数学建模网络挑战赛 C题 化工厂生产流程的预测和控制
深度学习·算法·计算机视觉
jndingxin7 小时前
OpenCV 图形API(31)图像滤波-----3x3 腐蚀操作函数erode3x3()
人工智能·opencv·计算机视觉
qp9 小时前
24.OpenCV中的霍夫直线检测
opencv·计算机视觉
新知图书9 小时前
OpenCV滑动条事件
人工智能·opencv·计算机视觉
2201_7549184111 小时前
OpenCv--换脸
人工智能·opencv·计算机视觉
Stara051111 小时前
YOLO11改进——融合BAM注意力机制增强图像分类与目标检测能力
人工智能·python·深度学习·目标检测·计算机视觉·yolov11
姚家湾12 小时前
MAC Mini M4 上测试Detectron2 图像识别库
目标检测·计算机视觉·detectron2
绝顶大聪明17 小时前
图像预处理(OpenCV)-part2
人工智能·opencv·计算机视觉
Despacito0o18 小时前
OpenCV图像增强实战教程:从理论到代码实现
人工智能·opencv·计算机视觉