使用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)
相关推荐
Lhan.zzZ36 分钟前
笔记_2026.4.28_003
c++·笔记·qt·opencv
深度学习lover2 小时前
<数据集>yolo 常见对象检测<目标检测>
人工智能·python·yolo·目标检测·计算机视觉·常见对象检测
硅谷秋水3 小时前
《自动驾驶系统开发》英文版《Autonomous Driving Hanbook》推荐
人工智能·深度学习·机器学习·计算机视觉·语言模型·自动驾驶
医学AI望远镜3 小时前
医学检测结合自监督学习:两篇新论文解析3D头部CT与目标检测进展!
人工智能·计算机视觉·医学图像
sali-tec4 小时前
C# 基于OpenCv的视觉工作流-章56-OCR
图像处理·人工智能·opencv·算法·计算机视觉·ocr
StfinnWu4 小时前
论文阅读:Frequency Domain-Based Diffusion Model for Unpaired Image Dehazing
人工智能·计算机视觉
nap-joker13 小时前
Fusion - Mamba用于跨模态目标检测
人工智能·目标检测·计算机视觉·fusion-mamba·可见光-红外成像融合·远距离/伪目标问题
jay神14 小时前
VisDrone2019-DET 无人机小目标检测数据集
人工智能·深度学习·yolo·目标检测·计算机视觉·毕业设计·无人机
_李小白15 小时前
【android opencv学习笔记】Day 2: Mat类(图片数据结构体)
android·opencv·学习
深度之眼16 小时前
强化学习与目标检测王炸组合,IEEE Trans顶刊发表!
目标检测·计算机视觉·强化学习