您好,登錄后才能下訂單哦!
這篇文章主要介紹了怎么在python中利用opencv對矩形進(jìn)行分割,億速云小編覺得不錯,現(xiàn)在分享給大家,也給大家做個參考,一起跟隨億速云小編來看看吧!
Python主要應(yīng)用于:1、Web開發(fā);2、數(shù)據(jù)科學(xué)研究;3、網(wǎng)絡(luò)爬蟲;4、嵌入式應(yīng)用開發(fā);5、游戲開發(fā);6、桌面應(yīng)用開發(fā)。
代碼:
# -*- coding:utf-8 -*- import cv2 from math import * import numpy as np import time,math import os import re '''旋轉(zhuǎn)圖像并剪裁''' 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) # 矩形框旋轉(zhuǎn)角度 print angle if pt4[1]>pt1[1]: print "順時針旋轉(zhuǎn)" else: print "逆時針旋轉(zhuǎn)" angle=-angle height = img.shape[0] # 原始圖像高度 width = img.shape[1] # 原始圖像寬度 rotateMat = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1) # 按angle角度旋轉(zhuǎn)圖像 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) # 旋轉(zhuǎn)后圖像的四點(diǎn)坐標(biāo) [[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]])) # 處理反轉(zhuǎn)的情況 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) # 裁減得到的旋轉(zhuǎn)矩形框 cv2.waitKey(0) return imgRotation # rotated image ?!「鶕?jù)四點(diǎn)畫原矩形 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) # 讀出文件中的坐標(biāo)值 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轉(zhuǎn)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對矩形進(jìn)行分割,如何覺得億速云網(wǎng)站的內(nèi)容還不錯,歡迎將億速云網(wǎng)站推薦給身邊好友。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。