我的代码有什么问题?使用 numpy 进行图像处理并尝试对文件夹中的图像应用过滤器

问题描述 投票:0回答:1

我对编程很陌生,在这段代码中,我想对“dataFromVid”目录中的图像应用几个过滤器。相反,我得到第 11 行的 valueErrors。(np.hstack)

这是代码:

directory = "/content/dataFromVid/"
for filename in os.listdir(directory):
    if filename.endswith(".jpg"): # Check for image files
        # Read the image
        img = cv2.imread(directory + filename)

        # Apply grayscale filter
        gray = grayscale_filtre(img)
        monochrome = monochrome_filtre(img, 100)
        borderDetection = detectEdge(img)              
        stacked = np.hstack((img, gray, monochrome, borderDetection))

        # Show the stacked image
        cv2_imshow(stacked)

        # Save the grayscale image
        cv2.imwrite(directory + "gray_" + filename, gray)

这是错误信息:

Error message

我认为这与颜色通道有关,因为我试图将灰度滤镜放在彩色图像上作为我的第一步。但同样,我是初学者,所以我不太确定。感谢您的帮助或意见 :))

python directory numpy-ndarray valueerror
1个回答
0
投票

您得到的 ValueError 很可能是由于 np.hstack 的输入数组沿水平轴的形状不同。 您正在尝试水平堆叠四个图像。如果这些图像中的任何一个沿水平轴具有不同的形状,您将得到一个 ValueError。

要解决此问题,您可以尝试在堆叠之前将所有图像调整为具有相同的宽度。例如,使用 cv2.resize.

© www.soinside.com 2019 - 2024. All rights reserved.