python opencv实现图片旋转矩形分割

作者:Imcy 时间:2022-01-25 06:23:24 

有时候需要对有角度的矩形框内图像从原图片中分割出来。这里的程序思想是,先将图片进行矩形角度的旋转,使有角度的矩形处于水平状态后,根据原来坐标分割图片。
参考:python opencv实现旋转矩形框裁减功能

修改原来的程序:

1.旋转函数的输入仅为矩形的四点坐标
2.角度由公式计算出来
3.矩形四点pt1,pt2,pt3,pt4由txt文件读入
4.在旋转程序中还处理了顺时针和逆时针及出现矩形框翻转的问题。

代码:


# -*- coding:utf-8 -*-
import cv2
from math import *
import numpy as np
import time,math
import os
import re

'''旋转图像并剪裁'''
def rotate(
   img, # 图片
   pt1, pt2, pt3, pt4
):
 print pt1,pt2,pt3,pt4
 withRect = math.sqrt((pt4[0] - pt1[0]) ** 2 + (pt4[1] - pt1[1]) ** 2) # 矩形框的宽度
 heightRect = math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) **2)
 print withRect,heightRect
 angle = acos((pt4[0] - pt1[0]) / withRect) * (180 / math.pi) # 矩形框旋转角度
 print angle

if pt4[1]>pt1[1]:
   print "顺时针旋转"
 else:
   print "逆时针旋转"
   angle=-angle

height = img.shape[0] # 原始图像高度
 width = img.shape[1]  # 原始图像宽度
 rotateMat = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1) # 按angle角度旋转图像
 heightNew = int(width * fabs(sin(radians(angle))) + height * fabs(cos(radians(angle))))
 widthNew = int(height * fabs(sin(radians(angle))) + width * fabs(cos(radians(angle))))

rotateMat[0, 2] += (widthNew - width) / 2
 rotateMat[1, 2] += (heightNew - height) / 2
 imgRotation = cv2.warpAffine(img, rotateMat, (widthNew, heightNew), borderValue=(255, 255, 255))
 cv2.imshow('rotateImg2', imgRotation)
 cv2.waitKey(0)

# 旋转后图像的四点坐标
 [[pt1[0]], [pt1[1]]] = np.dot(rotateMat, np.array([[pt1[0]], [pt1[1]], [1]]))
 [[pt3[0]], [pt3[1]]] = np.dot(rotateMat, np.array([[pt3[0]], [pt3[1]], [1]]))
 [[pt2[0]], [pt2[1]]] = np.dot(rotateMat, np.array([[pt2[0]], [pt2[1]], [1]]))
 [[pt4[0]], [pt4[1]]] = np.dot(rotateMat, np.array([[pt4[0]], [pt4[1]], [1]]))

# 处理反转的情况
 if pt2[1]>pt4[1]:
   pt2[1],pt4[1]=pt4[1],pt2[1]
 if pt1[0]>pt3[0]:
   pt1[0],pt3[0]=pt3[0],pt1[0]

imgOut = imgRotation[int(pt2[1]):int(pt4[1]), int(pt1[0]):int(pt3[0])]
 cv2.imshow("imgOut", imgOut) # 裁减得到的旋转矩形框
 cv2.waitKey(0)
 return imgRotation # rotated image

#根据四点画原矩形
def drawRect(img,pt1,pt2,pt3,pt4,color,lineWidth):
 cv2.line(img, pt1, pt2, color, lineWidth)
 cv2.line(img, pt2, pt3, color, lineWidth)
 cv2.line(img, pt3, pt4, color, lineWidth)
 cv2.line(img, pt1, pt4, color, lineWidth)

#读出文件中的坐标值
def ReadTxt(directory,imageName,last):
 fileTxt=directory+"//rawLabel//"+imageName[:7]+last # txt文件名
 getTxt=open(fileTxt, 'r') # 打开txt文件
 lines = getTxt.readlines()
 length=len(lines)
 for i in range(0,length,4):
   pt2=list(map(float,lines[i].split(' ')[:2]))
   pt1=list(map(float,lines[i+1].split(' ')[:2]))
   pt4=list(map(float,lines[i+2].split(' ')[:2]))
   pt3=list(map(float,re.split('\n| ',lines[i+3])[:2]))
   # float转int

pt2=list(map(int,pt2))
   pt1=list(map(int,pt1))
   pt4=list(map(int,pt4))
   pt3=list(map(int,pt3))

imgSrc = cv2.imread(imageName)
   drawRect(imgSrc, tuple(pt1),tuple(pt2),tuple(pt3),tuple(pt4), (0, 0, 255), 2)
   cv2.imshow("img", imgSrc)
   cv2.waitKey(0)
   rotate(imgSrc,pt1,pt2,pt3,pt4)

if __name__=="__main__":
 directory = "G://grasp//grapCode//trainImage//jpg//4"
 last = 'cneg.txt'
 imageName="pcd0247r.png"
 ReadTxt(directory,imageName,last)

原带角度的矩形框:

python opencv实现图片旋转矩形分割

旋转矩形框:

python opencv实现图片旋转矩形分割

分割:

python opencv实现图片旋转矩形分割

来源:https://blog.csdn.net/u013948010/article/details/78605043

标签:python,opencv,图片旋转,分割
0
投稿

猜你喜欢

  • WAP中的ASP技术

    2008-05-18 13:30:00
  • asp产生不重复的随机数

    2008-06-03 13:29:00
  • python matplotlib坐标轴设置的方法

    2022-03-26 03:44:04
  • MySQL索引类型一览 让MySQL高效运行起来

    2010-04-22 16:52:00
  • python ImageDraw类实现几何图形的绘制与文字的绘制

    2023-10-14 10:58:13
  • layui table 获取分页 limit的方法

    2023-08-24 13:44:56
  • Go gRPC服务客户端流式RPC教程

    2023-07-16 06:08:55
  • Python flask 框架使用flask-login 模块的详细过程

    2021-05-02 04:38:08
  • JavaScript防抖与节流超详细全面讲解

    2023-09-22 07:05:13
  • 创建Shapefile文件并写入数据的例子

    2023-06-21 23:27:12
  • python实现异常信息堆栈输出到日志文件

    2022-04-26 20:26:21
  • 用sqlalchemy构建Django连接池的实例

    2021-09-22 20:20:30
  • 浅析Banner构成与创意设计

    2009-11-28 16:25:00
  • python使用ddt过程中遇到的问题及解决方案【推荐】

    2021-08-25 09:20:44
  • Python图像处理之图像的缩放、旋转与翻转实现方法示例

    2023-03-09 11:25:20
  • python GUI库图形界面开发之PyQt5信号与槽的高级使用技巧装饰器信号与槽详细使用方法与实例

    2022-11-27 15:06:22
  • oracle逻辑运算符与其优先级简介

    2023-07-15 00:28:26
  • 使用Python中PIL库给图片添加文本水印

    2021-09-07 19:09:52
  • python之生产者消费者模型实现详解

    2021-12-03 14:37:15
  • 用Dreamweaver MX制作导航下拉菜单

    2009-05-29 18:37:00
  • asp之家 网络编程 m.aspxhome.com