我在使用蒙版只保留影像的幾個部分時遇到問題。我所做的 :
1 -> 選擇我要保留的顏色(確定)
2 -> 我將所有內容都轉換為 Lab 空間以計算 deltaE (OK)
3 -> 使用 deltaE 值創建掩碼(確定)
4 -> 面罩的形態開口(OK)
5 -> 將此蒙版應用于原始影像(不行)
當我嘗試將蒙版應用于影像時,出現此錯誤:
"shape mismatch: value array of shape (720,1280,3) could not be broadcast to indexing result of shape (21045,3)"
形狀 (720,1280,3) 是我原始影像的形狀,但是對于第二個形狀,每次啟動代碼時都會得到不同的結果。
所有資料(原始影像和最終影像)為 720x1280x3,掩碼為 720x1280。此外,所有資料都是 uint8。
這是我的代碼:
couleur_mire = im_Lab[mouseY,mouseX,:]
image_unie = np.ones_like(im) * couleur_mire
video.set(cv2.CAP_PROP_POS_FRAMES,frame_k)
_, im = video.read()
im_Lab = cv2.cvtColor(im, cv2.COLOR_BGR2Lab)
mask = np.zeros((h,w))
im_finale = np.zeros_like(im)
L1 = np.array(im_Lab[:,:,0],dtype=int)
a1 = np.array(im_Lab[:,:,1],dtype=int)
b1 = np.array(im_Lab[:,:,2],dtype=int)
L2 = np.array(image_unie[:,:,0],dtype=int)
a2 = np.array(image_unie[:,:,1],dtype=int)
b2 = np.array(image_unie[:,:,2],dtype=int)
deltaE =np.sqrt(np.multiply(L1-L2,L1-L2) np.multiply(a1-a2,a1-a2) np.multiply(b1-b2,b1-b2))
th = 25
mask[deltaE<th] = 1
mask = cv2.morphologyEx(mask,cv2.MORPH_OPEN,np.ones((20,20)))
mask = np.array(mask,dtype=np.uint8)
im_finale = np.zeros_like(im)
im_finale[mask==1] = im <--THIS IS THE LINE THAT DOES NOT WORK
歡迎任何幫助。感謝任何需要一些時間來回答的人:)
編輯:當蒙版改變時,第二個形狀會改變(如果我連續兩次選擇相同的顏色,第二個形狀保持不變)
編輯 2:第二個形狀中的第一個值對應于掩碼中 1 的數量(在這種情況下,此掩碼保留 21045 個像素)
編輯3:當我這樣做時,它可以完美運行,但速度太慢:
for i in range(h):
for j in range(w):
if mask[i,j]:
im_finale[i,j,:] = im[i,j,:]
uj5u.com熱心網友回復:
根據我的理解,您需要根據您創建的蒙版對影像進行蒙版,您可以使用乘法來實作這種效果。
def mask_image( img, mask ):
newImg = img.copy()
newImg[:,:] = img[:,:] * mask[:,:]
return newImg
有了這個,您不必將掩碼轉換為 uint8。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/424174.html
上一篇:將兩個列分組后求平方和
