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

yipeiwu_com5年前Python基础

有时候需要对有角度的矩形框内图像从原图片中分割出来。这里的程序思想是,先将图片进行矩形角度的旋转,使有角度的矩形处于水平状态后,根据原来坐标分割图片。
参考: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设计】。

相关文章

python psutil模块使用方法解析

psutil(进程和系统实用程序)是一个跨平台的库,用于 在Python中检索有关运行进程和系统利用率(CPU,内存,磁盘,网络,传感器)的信息。 它主要用于系统监视,分析和限制流程资源...

Python 文件操作的详解及实例

Python 文件操作的详解及实例 一、文件操作 1、对文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 现有文件如下:...

pycharm 使用心得(五)断点调试

pycharm 使用心得(五)断点调试

【运行】和【调试】前的设置,详见前面的文章,helloword。 1,设置断点 在代码前面,行号的后面,鼠标单击,就可以设置断点。如下: 2,调试 断点点击那个绿色的甲虫图标(似乎甲虫...

Python FTP两个文件夹间的同步实例代码

具体代码如下所示: # -*- coding: utf-8 -*- ''''''' ftp自动检测源文件夹的更新,将源文件夹更新的内容拷贝到目标文件夹中 使用树的层序遍...

win系统下为Python3.5安装flask-mongoengine 库

环境: windows 10、python 3.5、flask-mongoengine 0.8.2或0.9.0 使用以下命令安装 flask-mongoengine pip instal...