位平面Slicing_位和输入参数的大小不匹配



我正在尝试执行位平面切片。我使用这里共享的代码,在导出的最后只做了很少的修改:https://dsp.stackexchange.com/questions/41635/bit-plane-slicing-in-python.

当我一开始使用jpeg图像时,它工作得很好。然而,大多数时候,我会使用tif图像。所以我修改了第一行,现在代码看起来像这样:

img1 = Image.open('C:/Users/Caroline/Documents/Cours/G5/PFE/Donnees/2018_gris.tif') 
img = np.array(img1)
out = []
k=7
# create an image for the k bit plane
plane = np.full((img.shape[0], img.shape[1]), 2 ** k, np.uint8)
# execute bitwise and operation
res = cv2.bitwise_and(plane, img)
# multiply ones (bit plane sliced) with 255 just for better visualization
x = res * 255
# append to the output list
out.append(x)
outfinal = np.hstack(out)
imgfinale = Image.fromarray(outfinal.astype(np.uint8))
imgfinale.imsave("test1.tif")

当我执行它时,我会收到一条错误消息,上面写着

Traceback (most recent call last): File "C:UsersCarolineDesktopBitPlaneSlicing.py", line 24, in <module> res = cv2.bitwise_and(plane, img) cv2.error: OpenCV(4.1.2) D:BuildOpenCVopencv-4.1.2modulescoresrcarithm.cpp:229: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function 'cv::binary_op'

我查看了一些网站,从中我了解到,这是因为tiff图像比我的"平面"图像有更多的通道。我没有得到的是,我的tiff图像是一个没有阿尔法带的灰度图像。为了证明,我写了type(img(和type(plane(,答案是相同的(<class 'numpy.ndarray'>(,img.shape也给出了与plane.shape相同的结果。

此外,我是pyhton的新手,所以我现在完全迷失了方向,甚至不知道该尝试什么。

img1=cv2.imread('C:/Users/Caroline/Documents/Cours/G5/PFE/Donnees/2018_gris.tif',0) 
img = np.array(img1)
out = []
k=7
# create an image for the k bit plane
plane = np.full((img.shape[0], img.shape[1]), 2 ** k, np.uint8)
# execute bitwise and operation
res = cv2.bitwise_and(plane, img)
# multiply ones (bit plane sliced) with 255 just for better visualization
x = res * 255
# append to the output list
out.append(x)
outfinal = np.hstack(out)
imgfinale = Image.fromarray(outfinal.astype(np.uint8))
imgfinale.imsave("test1.tif")

相关内容

最新更新