国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python opencv實現圖片旋轉矩形分割

2020-02-15 22:32:01
字體:
來源:轉載
供稿:網友

有時候需要對有角度的矩形框內圖像從原圖片中分割出來。這里的程序思想是,先將圖片進行矩形角度的旋轉,使有角度的矩形處于水平狀態后,根據原來坐標分割圖片。
參考:python opencv實現旋轉矩形框裁減功能

修改原來的程序:

1.旋轉函數的輸入僅為矩形的四點坐標
2.角度由公式計算出來
3.矩形四點pt1,pt2,pt3,pt4由txt文件讀入
4.在旋轉程序中還處理了順時針和逆時針及出現矩形框翻轉的問題。

代碼:

# -*- coding:utf-8 -*-import cv2from math import *import numpy as npimport time,mathimport osimport 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?!「鶕狞c畫原矩形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+"http://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)            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吴川市| 宝山区| 南宫市| 晋江市| 西和县| 湖北省| 惠安县| 巴楚县| 老河口市| 和顺县| 伽师县| 嘉峪关市| 蒙城县| 乐陵市| 皋兰县| 朝阳区| 安新县| 深州市| 新龙县| 澄江县| 苍溪县| 平阴县| 盖州市| 岳池县| 吉林市| 潮州市| 肃北| 青田县| 吉隆县| 盐山县| 彰化市| 云霄县| 卫辉市| 星子县| 砀山县| 旬阳县| 桂东县| 石楼县| 莲花县| 景德镇市| 茶陵县|