我正在使用物件檢測演算法來檢測影像中的物件。代碼如下。使用的影像是一輛汽車,如下所示。
我想裁剪原始影像以僅保留影像中檢測到的物件以及保持 4/3 和 16/9 之間的縱橫比所需的任何內容。
汽車周圍的盒子已經從下面的演算法[變數是盒子]中推匯出來,影像尺寸在下面的代碼中是[變數是高度,寬度]。
如果我們手動執行此操作,由于需要多次迭代,這將很麻煩,例如:我們必須確保調整大小不會超出原始影像大小。
下面包含 3 張影像,原始影像、檢測到汽車的修改影像以及調整大小以滿足縱橫比范圍的影像。(4/3 到 16/9)
python中是否有一個現有的函式來完成這個任務。因此,將框尺寸從 [91, 90, 226, 158] 調整/增加到最小必要量,以在原始影像尺寸 183x275 的限制范圍內,同時保持縱橫比
提前致謝。
代碼:
import cv2
import matplotlib.pyplot as plt
import cvlib as cv
from cvlib.object_detection import draw_bbox
imagepath='/home/usr/Desktop/car.jpeg'
img = cv2.imread(imagepath)
####STEP 1
img1 = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
box, label, count = cv.detect_common_objects(img)
output = draw_bbox(img, box, label, count)
output = cv2.cvtColor(output,cv2.COLOR_BGR2RGB)
plt.figure(figsize=(10,10))
plt.axis('off')
plt.imshow(output)
plt.show()
print("Number of objects in this image are " str(len(label)))
height,width,_=img1.shape
print(height,width)
print(box)
#box size is [91, 90, 226, 158] (w1,h1,w2,h2)
#image size is 183x275 (heightxwidth)
#STEP2 (cropping original image to car dimensions as per box size)
crop_img = img[90:158, 91:226]
cv2.imshow("cropped", crop_img)
cv2.waitKey(0)
影像示例:

檢測物體(Step1)

裁剪影像(步驟 2)

預期結果(Step3)

uj5u.com熱心網友回復:
下面的代碼將影像裁剪到所需的坐標,然后根據其 >16/9 或 <4/3 增加其大小以匹配縱橫比。這種方法的優點是在調整影像大小和校正縱橫比時不會裁剪中心物體(汽車),如果右側沒有空間增加,則會增加影像左側(反之亦然,對于高度和寬度)來實作縱橫比
import cv2
import math
import sys
imagepath=('/home/usr/Desktop/filename.jpeg')
img=cv2.imread(imagepath)
h,w,_=img.shape#height and width of original image
#Dimensions of car or object you want to crop (See step 2 in question)
crop_dimensions=[96, 56, 602, 686] #w1,h1,w2,h2
def cropimage(crop_dimensions,imgx):
crop_img = imgx[crop_dimensions[1]:crop_dimensions[3], crop_dimensions[0]:crop_dimensions[2]]
return crop_img
crop_img=cropimage(crop_dimensions,img)
height,width,_=crop_img.shape #height and width of cropped image
if width/height>16/9 or width/height<4/3:
crop_centrepoint = ((crop_dimensions[2] - crop_dimensions[0])/2, (crop_dimensions[3] - crop_dimensions[1])/2)
print(crop_centrepoint) #Centre point of cropped image
if width/height<4/3:
print('<4/3')
newwidth=4/3*height
newheight=height
additionalwidth=newwidth-width
w1maxadditional = crop_dimensions[0] - 0 #distance from cropped image to left edge (0)
w2maxadditional=w-crop_dimensions[2]#distance between cropped image and right end
if w2maxadditional > additionalwidth/2:
correction2=0
w2=(additionalwidth/2)
else:
correction2=abs(w2maxadditional-(additionalwidth/2))
w2=w2maxadditional
if w1maxadditional > additionalwidth/2:
correction1=0
w1=(additionalwidth/2) correction1
else:
correction1=abs(w2maxadditional-(additionalwidth/2))
w1=w1maxadditional
w1=w1 correction2
w2 = w2 correction1
if w1>w1maxadditional:
w1=w1maxadditional
if w2>w2maxadditional:
w2=w2maxadditional
w1 = crop_dimensions[0] - w1
w2 = w2 crop_dimensions[2]
h1=crop_dimensions[1]
h2=crop_dimensions[3]
if width / height > 16/9:
print('>16/9')
newheight = width * 9 / 16
newwidth = width
additionalheight = newheight - height
h1maxadditional = crop_dimensions[1] - 0 # distance from cropped image to top edge
h2maxadditional = h - crop_dimensions[3] # distance between cropped image to bottom end
if h2maxadditional > additionalheight / 2:
correction2 = 0
h2 = (additionalheight / 2)
else:
correction2 = abs(h2maxadditional - (additionalheight / 2))
h2 = h2maxadditional
if h1maxadditional > additionalheight / 2:
correction1 = 0
h1 = (additionalheight / 2) correction1
else:
correction1 = abs(h2maxadditional - (additionalheight / 2))
h1 = h1maxadditional
h1 = h1 correction2
h2 = h2 correction1
if h1 > h1maxadditional:
h1 = h1maxadditional
if h2 > h2maxadditional:
h2 = h2maxadditional
h1 = crop_dimensions[1] - h1
h2 = h2 crop_dimensions[3]
w1 = crop_dimensions[0]
w2 = crop_dimensions[2]
else:
[w1,h1,w2,h2]=crop_dimensions
#Rounding down because cropimage function takes integers
w1=math.trunc(w1)
h1=math.trunc(h1)
w2=math.trunc(w2)
h2=math.trunc(h2)
new_image=cropimage([w1,h1,w2,h2],img)
cv2.imshow('img',new_image)
cv2.waitKey(0)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/393762.html
