使用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)
相关推荐
jndingxin7 小时前
OpenCV图像注册模块
人工智能·opencv·计算机视觉
R-G-B7 小时前
【P14 3-6 】OpenCV Python——视频加载、摄像头调用、视频基本信息获取(宽、高、帧率、总帧数)
python·opencv·视频加载·摄像头调用·获取视频基本信息·获取视频帧率·获取视频帧数
荼蘼7 小时前
OpenCv(三)——图像平滑处理
人工智能·opencv·计算机视觉
Monkey PilotX7 小时前
机器人“ChatGPT 时刻”倒计时
人工智能·机器学习·计算机视觉·自动驾驶
程序猿小D17 小时前
【完整源码+数据集+部署教程】孔洞检测系统源码和数据集:改进yolo11-RetBlock
yolo·计算机视觉·毕业设计·数据集·yolo11·孔洞检测
图灵学术计算机论文辅导19 小时前
傅里叶变换+attention机制,深耕深度学习领域
人工智能·python·深度学习·计算机网络·考研·机器学习·计算机视觉
R-G-B1 天前
OpenCV Python——报错AttributeError: module ‘cv2‘ has no attribute ‘bgsegm‘,解决办法
人工智能·python·opencv·opencv python·attributeerror·module ‘cv2‘·no attribute
Struart_R1 天前
SpatialVLM和SpatialRGPT论文解读
计算机视觉·语言模型·transformer·大语言模型·vlm·视觉理解·空间推理
似乎很简单1 天前
【opencv-Python学习笔记(5):几何变换】
笔记·opencv·学习
软件测试-阿涛1 天前
【AI绘画】Stable Diffusion webUI 常用功能使用技巧
人工智能·深度学习·计算机视觉·ai作画·stable diffusion