gpt4 book ai didi

python - Python TypeError:需要一个整数(元组类型元组)-(OpenCV/Numpy)

转载 作者:行者123 更新时间:2023-12-02 16:14:41 25 4
gpt4 key购买 nike

我知道周围也有很多类似的问题,但是这些问题似乎都不适合我的问题。这就是为什么我决定创建另一篇文章的原因。

首先,以下代码的基本思想是它将检测特定坐标的像素值,以便创建具有相同颜色的矩形。

这是我的代码:

# open image
img = cv2.imread("image.png")

# set coordinates for rectangle
start_point = (35, 39)
end_point = (50, 60)

# get pixel value of start point; outputs something like "[132 42 52]"
pixel = img[start_point].astype(int)
R = pixel[0]
G = pixel[1]
B = pixel[2]

# outputs type: "<class 'numpy.ndarray'> <class 'numpy.int32'> <class 'numpy.int32'> <class 'numpy.int32'>"
print(type(pixel), type(R), type(G), type(B))

# draws rectangle
color = (R, G, B)
image = cv2.rectangle(img, start_point, end_point, color, -1)

即使使用“astype(int)”将值“R”,“G”和“B”转换为整数,我也会遇到以下错误:
image = cv2.rectangle(img, start_point, end_point, color, -1)
TypeError: an integer is required (got type tuple)

通过使用数字30、53、100这样的颜色值,一切工作都非常完美。通过设置此图像中坐标的像素值,我收到的值似乎有问题。我真的不知道问题可能在哪里,所以我非常感谢您的帮助!

提前致谢。

最佳答案

我认为最简单的解决方案是使用color = (int(R), int(G), int(B))

问题是,即使使用pixel = img[start_point].astype(int)pixel的元素也是<class 'numpy.int32'>类型,而不是int类型。

关于python - Python TypeError:需要一个整数(元组类型元组)-(OpenCV/Numpy),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60948480/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com