python opencv通過4坐標剪裁圖片
本文主要介紹了python opencv通過4坐標剪裁圖片,分享給大家,具體如下:
效果展示,
裁剪出的單詞圖像(如下)
這里程序我是用在paddleOCR里面,通過識別模型將識別出的圖根據(jù)程序提供的坐標(即四個頂點的值)進行摳圖的程序(上面的our和and就是扣的圖),并進行了封裝,相同格式的在這個基礎(chǔ)上改就是了
[[[368.0, 380.0], [437.0, 380.0], [437.0, 395.0], [368.0, 395.0]], [[496.0, 376.0], [539.0, 378.0], [538.0, 397.0], [495.0, 395.0]], [[466.0, 379.0], [498.0, 379.0], [498.0, 395.0], [466.0, 395.0]], [[438.0, 379
.0], [466.0, 379.0], [466.0, 395.0], [438.0, 395.0]], ]
從程序得到的數(shù)據(jù)格式大概長上面的樣子,由多個四個坐標一組的數(shù)據(jù)(如下)組成,即下面的[368.0, 380.0]為要裁剪圖片左上角坐標,[437.0, 380.0]為要裁剪圖片右上角坐標,[437.0, 395.0]為要裁剪圖片右下角坐標,[368.0, 395.0]為要裁剪圖片左下角坐標.
[[368.0, 380.0], [437.0, 380.0], [437.0, 395.0], [368.0, 395.0]]
而這里剪裁圖片使用的是opencv(由于參數(shù)的原因沒有設(shè)置角度的話就只能裁剪出平行的矩形,如果需要裁減出不與矩形圖片編譯平行的圖片的話,參考這個博客進行進一步的改進點擊進入)
裁剪部分主要是根據(jù)下面這一行代碼進行的,這里要記住(我被這里坑了一下午),
參數(shù) tr[1]:左上角或右上角的縱坐標值
參數(shù)bl[1]:左下角或右下角的縱坐標值
參數(shù)tl[0]:左上角或左下角的橫坐標值
參數(shù)br[0]:右上角或右下角的橫坐標值
crop = img[int(tr[1]):int(bl[1]), int(tl[0]):int(br[0]) ]
總的程序代碼如下
import numpy as np import cv2 def np_list_int(tb): tb_2 = tb.tolist() #將np轉(zhuǎn)換為列表 return tb_2 def shot(img, dt_boxes):#應(yīng)用于predict_det.py中,通過dt_boxes中獲得的四個坐標點,裁剪出圖像 dt_boxes = np_list_int(dt_boxes) boxes_len = len(dt_boxes) num = 0 while 1: if (num < boxes_len): box = dt_boxes[num] tl = box[0] tr = box[1] br = box[2] bl = box[3] print("打印轉(zhuǎn)換成功數(shù)據(jù)num =" + str(num)) print("tl:" + str(tl), "tr:" + str(tr), "br:" + str(br), "bl:" + str(bl)) print(tr[1],bl[1], tl[0],br[0]) crop = img[int(tr[1]):int(bl[1]), int(tl[0]):int(br[0]) ] # crop = img[27:45, 67:119] #測試 # crop = img[380:395, 368:119] cv2.imwrite("K:/paddleOCR/PaddleOCR/screenshot/a/" + str(num) + ".jpg", crop) num = num + 1 else: break def shot1(img_path,tl, tr, br, bl,i): tl = np_list_int(tl) tr = np_list_int(tr) br = np_list_int(br) bl = np_list_int(bl) print("打印轉(zhuǎn)換成功數(shù)據(jù)") print("tl:"+str(tl),"tr:" + str(tr), "br:" + str(br), "bl:"+ str(bl)) img = cv2.imread(img_path) crop = img[tr[1]:bl[1], tl[0]:br[0]] # crop = img[27:45, 67:119] cv2.imwrite("K:/paddleOCR/PaddleOCR/screenshot/shot/" + str(i) + ".jpg", crop) # tl1 = np.array([67,27]) # tl2= np.array([119,27]) # tl3 = np.array([119,45]) # tl4 = np.array([67,45]) # shot("K:\paddleOCR\PaddleOCR\screenshot\zong.jpg",tl1, tl2 ,tl3 , tl4 , 0)
特別注意對np類型轉(zhuǎn)換成列表,以及crop = img[tr[1]:bl[1], tl[0]:br[0]]
的中參數(shù)的位置,
實例
用了兩種方法保存圖片,opencv和Image,實踐證明opencv非???/p>
from PIL import Image import os import cv2 import time import matplotlib.pyplot as plt def label2picture(cropImg,framenum,tracker): pathnew ="E:\\img2\\" # cv2.imshow("image", cropImg) # cv2.waitKey(1) if (os.path.exists(pathnew + tracker)): cv2.imwrite(pathnew + tracker+'\\'+framenum + '.jpg', cropImg,[int(cv2.IMWRITE_JPEG_QUALITY), 100]) else: os.makedirs(pathnew + tracker) cv2.imwrite(pathnew + tracker+'\\'+framenum + '.jpg', cropImg,[int(cv2.IMWRITE_JPEG_QUALITY), 100]) f = open("E:\\hypotheses.txt","r") lines = f.readlines() for line in lines: li = line.split(',') print(li[0],li[1],li[2],li[3],li[4],li[5]) filename = li[0]+'.jpg' img = cv2.imread("E:\\DeeCamp\\img1\\" + filename) crop_img = img[int(li[3][:-3]):(int(li[3][:-3]) + int(li[5][:-3])), int(li[2][:-3]):(int(li[2][:-3]) + int(li[4][:-3]))] # print(int(li[2][:-3]),int(li[3][:-3]),int(li[4][:-3]),int(li[5][:-3])) label2picture(crop_img, li[0], li[1]) # # # x,y,w,h = 87,158,109,222 # img = cv2.imread("E:\\DeeCamp\\img1\\1606.jpg") # # print(img.shape) # crop = img[y:(h+y),x:(w+x)] # cv2.imshow("image", crop) # cv2.waitKey(0) # img = Image.open("E:\\DeeCamp\\img1\\3217.jpg") # # cropImg = img.crop((x,y,x+w,y+h)) # cropImg.show() # img = Image.open("E:\\deep_sort-master\\MOT16\\train\\try1\\img1\\"+filename) # print(int(li[2][:-3]),(int(li[2][:-3])+int(li[4][:-3])), int(li[3][:-3]),(int(li[3][:-3])+int(li[5][:-3]))) # #裁切圖片 # # cropImg = img.crop(region) # # cropImg.show() # framenum ,tracker= li[0],li[1] # pathnew = 'E:\\DeeCamp\\deecamp項目\\deep_sort-master\\crop_picture\\' # if (os.path.exists(pathnew + tracker)): # # 保存裁切后的圖片 # plt.imshow(cropImg) # plt.savefig(pathnew + tracker+'\\'+framenum + '.jpg') # else: # os.makedirs(pathnew + tracker) # plt.imshow(cropImg) # plt.savefig(pathnew + tracker+'\\'+framenum + '.jpg')
到此這篇關(guān)于python opencv通過4坐標剪裁圖片的文章就介紹到這了,更多相關(guān)opencv 剪裁圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python使用pyenv實現(xiàn)多環(huán)境管理
這篇文章主要介紹了Python使用pyenv實現(xiàn)多環(huán)境管理,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細使用方法與實例,需要的朋友可以參考下2020-03-03Python實現(xiàn)定時備份mysql數(shù)據(jù)庫并把備份數(shù)據(jù)庫郵件發(fā)送
這篇文章主要介紹了Python實現(xiàn)定時備份mysql數(shù)據(jù)庫并把備份數(shù)據(jù)庫郵件發(fā)送的相關(guān)資料,需要的朋友可以參考下2018-03-03Python?中的?Counter?模塊及使用詳解(搞定重復(fù)計數(shù))
Counter 是一個簡單的計數(shù)器,用于統(tǒng)計某些可哈希對象的數(shù)量。它以字典的形式存儲元素和它們的計數(shù),這篇文章主要介紹了Python?中的?Counter?模塊及使用詳解(搞定重復(fù)計數(shù)),需要的朋友可以參考下2023-04-04python學(xué)習(xí)--使用QQ郵箱發(fā)送郵件代碼實例
這篇文章主要介紹了python使用QQ郵箱發(fā)送郵件,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Python操作MySQL數(shù)據(jù)庫的三種方法總結(jié)
下面小編就為大家分享一篇Python操作MySQL數(shù)據(jù)庫的三種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01