我使用OpenCV
中的各种方法来预处理一些图像。当传递对象是方法时,我经常会遇到与数据类型相关的错误,例如:
import cv2
import numpy as np
#import image and select ROI
image1 = cv2.imread('image.png')
roi_1 = cv2.selectROI(image1) # spacebar to confirm selection
cv2.waitKey(0)
cv2.destroyAllWindows()
# preprocesssing
imCrop_1 = image1[int(roi_1[1]):int(roi_1[1]+roi_1[3]), int(roi_1[0]):int(roi_1[0]+roi_1[2])]
grey1 = cv2.cvtColor(imCrop_1, cv2.COLOR_RGB2GRAY)
thresh, bw_1 = cv2.threshold(grey1, 200, 255, cv2.THRESH_OTSU)
canny_edge1 = cv2.Canny(bw_1, 50, 100)
#test=roi_1 # Doesn't work with error: /home/bprodz/opencv/modules/photo/src/denoising.cpp:182: error: (-5) Type of input image should be CV_8UC3 or CV_8UC4! in function fastNlMeansDenoisingColored
#test = imCrop_1 # works
#test = grey1 # doesn't work with error above
#test = bw_1 # doesn't work with error above
#test = canny_edge1 # doesn't work with error above
dst = cv2.fastNlMeansDenoisingColored(test,None,10,10,7,21)
# Check object types
type(imCrop_1) # returns numpy.ndarray - would like to see ~ CV_8UC3 etc.
type(grey1) # returns numpy.ndarray
目前我只使用试错法,有没有一种更为系统化的方法可以用来检查和转换不同的对象类型?在
您可能使用了错误的方法,为此,您还可能从所使用的方法名中得到提示,如^{} 的文档所示:
因此,如果要使用
cv2. fastNlMeansDenoisingColored
,则需要将src mat转换为一个3通道矩阵,其操作如下:但是如果您有灰度图像,那么您可以使用^{} ,它接受单通道和三通道作为源垫,并节省转换图像的步骤。在
您还可以使用
img.shape
检查给定矩阵的通道数。对于灰度矩阵,它将返回(100,100),对于3通道矩阵返回(100,100,3),对于4通道矩阵返回(100,100,4)。也可以使用img.dtype
获得矩阵的类型相关问题 更多 >
编程相关推荐