cv2.error: OpenCV(4.5.5) ?? error: (-5:Bad argument) in function ‘Sobel’
Overload resolution failed:
- src data type = 23 is not supported
- Expected Ptrcv::UMat for argument ‘src’
報(bào)錯(cuò)代碼:
def sobel(img):
add_x_total = torch.zeros(img.shape)
for i in range(img.shape[0]):
x = img[i, :, :, :].squeeze(0).cpu().detach().numpy().transpose(1, 2, 0)
x = x * 255
# print(x.dtype)
x_x = cv2.Sobel(x, cv2.CV_64F, 1, 0)
x_y = cv2.Sobel(x, cv2.CV_64F, 0, 1)
add_x = cv2.addWeighted(x_x, 0.5, x_y, 0.5, 0)
add_x = transforms.ToTensor()(add_x).unsqueeze(0)
add_x_total[i, :, :, :] = add_x
return add_x_total
報(bào)錯(cuò)原因:
這個(gè)錯(cuò)誤提示是由于傳入 cv2.Sobel()
函數(shù)的圖像類型不受支持所引起的。根據(jù)錯(cuò)誤信息,是圖像的數(shù)據(jù)類型不正確。
數(shù)據(jù)類型不匹配:Sobel函數(shù)要求輸入圖像的數(shù)據(jù)類型必須是浮點(diǎn)型。在代碼中,處理圖像前執(zhí)行了以下操作:x = x * 255
,這將將圖像數(shù)據(jù)的數(shù)據(jù)類型從浮點(diǎn)型轉(zhuǎn)換為整數(shù)。
解決辦法:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-811416.html
在調(diào)用Sobel函數(shù)之前,將圖像 x
的數(shù)據(jù)類型進(jìn)行調(diào)整,確保輸入圖像的數(shù)據(jù)類型是浮點(diǎn)型。使用x = x.astype(np.float32)
將NumPy數(shù)組的數(shù)據(jù)類型轉(zhuǎn)換為浮點(diǎn)型。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-811416.html
x = x * 255
x = x.astype(np.float32) # 將數(shù)據(jù)類型轉(zhuǎn)換為浮點(diǎn)型
x_x = cv2.Sobel(x, cv2.CV_64F, 1, 0)
x_y = cv2.Sobel(x, cv2.CV_64F, 0, 1)
到了這里,關(guān)于cv2.error: OpenCV(4.5.5) :-1: error: (-5:Bad argument) in function ‘Sobel‘的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!