所以我想做一個并排的視頻比較。左邊是我的原始視頻,右邊是面具。我用 np.zeros 創建了一個大的空白影像來存盤這兩個影像。我的問題是掩碼影像在二維陣列中,而原始影像在 3 維中。我的代碼如下所示:
_, videoCam= vid.read()
f_height,f_width,_=videoCam.shape
hsv = cv.cvtColor(videoCam, cv.COLOR_BGR2HSV)
## mask of green (36,0,0) ~ (70, 255,255)
mask1 = cv.inRange(hsv, (36, 0, 0), (70, 255,255))
cv.bitwise_not(mask1)
blank_image = np.zeros((f_height,f_width*2,3), np.uint8)
blank_image[:,0:f_width] = hsv
blank_image[:,f_width:f_width*2] = mask1 #ValueError: could not broadcast input array from shape (480,640) into shape (480,640,3)
cv.imshow('',blank_image)
我想我應該將 2 維蒙版轉換為 3。但是該怎么做呢?空白維度填什么?
任何幫助或建議將不勝感激
uj5u.com熱心網友回復:
你可以做:
blank_image[:,f_width:f_width*2] = mask1[:,:,None]
或者
blank_image[:,f_width:f_width*2] = mask1[:,:,np.newaxis]
這將導致mask1值在分配給時被復制到 3 個維度blank_image(意味著每個像素處具有相等 R、G 和 B 的 RGB 影像)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/511470.html
