如何在不使用Python编写的情况下引用图像

2024-09-29 03:37:09 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我目前拥有的代码。我想避免写一个图像,然后再次加载它,然后复制它。为什么我的代码在第二部分不起作用

import cv2

load_imaged = cv2.imread("image.png", 0)

# Apply GaussianBlur to reduce image noise if it is required
otsu_threshold, otsu_result = cv2.threshold(
    load_imaged, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU, )
# Optimal threshold value is determined automatically.

# Visualize the image after the Otsu's method application
cv2.imwrite("otsu.png", otsu_result)

hole_image = cv2.imread("otsu.png")

# copy image
img = hole_image.copy()
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Image", imgray)
cv2.waitKey()
cv2.destroyAllWindows()

我试图引用像这样的图像(第9行),但它返回了一个错误

Invalid number of channels in input image: 'VScn::contains(scn)' where 'scn' is 1

import cv2

load_imaged = cv2.imread("image.png", 0)

# Apply GaussianBlur to reduce image noise if it is required
otsu_threshold, otsu_result = cv2.threshold(
    load_imaged, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU, )
# copy image
img = otsu_result.copy()
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cv2.imshow("Image", imgray)
cv2.waitKey()
cv2.destroyAllWindows()

谢谢你的帮助


Tags: 代码imageimgthresholdpngisloadresult