python opencv實(shí)現(xiàn)圖片旋轉(zhuǎn)矩形分割
有時(shí)候需要對有角度的矩形框內(nèi)圖像從原圖片中分割出來。這里的程序思想是,先將圖片進(jìn)行矩形角度的旋轉(zhuǎn),使有角度的矩形處于水平狀態(tài)后,根據(jù)原來坐標(biāo)分割圖片。
參考:python opencv實(shí)現(xiàn)旋轉(zhuǎn)矩形框裁減功能
修改原來的程序:
1.旋轉(zhuǎn)函數(shù)的輸入僅為矩形的四點(diǎn)坐標(biāo)
2.角度由公式計(jì)算出來
3.矩形四點(diǎn)pt1,pt2,pt3,pt4由txt文件讀入
4.在旋轉(zhuǎn)程序中還處理了順時(shí)針和逆時(shí)針及出現(xiàn)矩形框翻轉(zhuǎn)的問題。
代碼:
# -*- 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 "順時(shí)針旋轉(zhuǎn)" else: print "逆時(shí)針旋轉(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+"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轉(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)
原帶角度的矩形框:
旋轉(zhuǎn)矩形框:
分割:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- python驗(yàn)證碼識(shí)別教程之利用滴水算法分割圖片
- python驗(yàn)證碼識(shí)別教程之利用投影法、連通域法分割圖片
- python3 實(shí)現(xiàn)對圖片進(jìn)行局部切割的方法
- python opencv將表格圖片按照表格框線分割和識(shí)別
- python實(shí)現(xiàn)將文件夾內(nèi)的每張圖片批量分割成多張
- python實(shí)現(xiàn)對任意大小圖片均勻切割的示例
- python實(shí)現(xiàn)圖片中文字分割效果
- Python+opencv 實(shí)現(xiàn)圖片文字的分割的方法示例
- python3 實(shí)現(xiàn)驗(yàn)證碼圖片切割的方法
- Python?Opencv實(shí)現(xiàn)圖片切割處理
相關(guān)文章
Python PyWebIO提升團(tuán)隊(duì)效率使用介紹
這篇文章主要為大家介紹了Python PyWebIO提升團(tuán)隊(duì)效率使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01在PyCharm中找不到Conda創(chuàng)建的環(huán)境的解決方法
本文主要介紹了在PyCharm中找不到Conda創(chuàng)建的環(huán)境的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能
手機(jī)上突然收到了某銀行的短信提示,看了一下手機(jī)的位數(shù),正好是11位,我一想,這不就是標(biāo)準(zhǔn)的手機(jī)號(hào)碼嗎?于是想用python的庫實(shí)現(xiàn)查詢手機(jī)號(hào)碼歸屬地查詢自由,所以本文給大家介紹了如何用python實(shí)現(xiàn)手機(jī)號(hào)歸屬地查詢功能,需要的朋友可以參考下2024-03-03如何基于python實(shí)現(xiàn)畫不同品種的櫻花樹
這篇文章主要介紹了如何基于python實(shí)現(xiàn)畫不同品種的櫻花樹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01