Python TypeError:需要一个整数(get type tuple)-(OpenCV/Numpy)

Python TypeError:需要一个整数(get type tuple)-(OpenCV/Numpy),python,numpy,opencv,integer,tuples,Python,Numpy,Opencv,Integer,Tuples,我知道有很多类似的问题,但这些问题似乎都不能解决我的问题。这就是我决定创建另一个帖子的原因 首先,下面代码的基本思想是,它将检测特定坐标的像素值,以便创建具有相同颜色的矩形 这是我的代码: # 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;

我知道有很多类似的问题,但这些问题似乎都不能解决我的问题。这就是我决定创建另一个帖子的原因

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

这是我的代码:

# 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)
通过使用像30、53、100这样的数字作为颜色值,一切都很好。在这幅图中设置坐标的像素值,我收到的值似乎有问题。我真的不知道问题出在哪里,所以我非常感谢大家的帮助


提前谢谢。

你自己回答了-你通过了type
numpy.int32
,他们希望
int
。对于人类来说这是一样的,但是python很难处理所有相互转换的类型。 你必须通过以下方式帮助他们:

image = cv2.rectangle(img, start_point, end_point, [int(x) for x in color], -1)

你自己回答-你通过了type
numpy.int32
,他们期望
int
。对于人类来说这是一样的,但是python很难处理所有相互转换的类型。 你必须通过以下方式帮助他们:

image = cv2.rectangle(img, start_point, end_point, [int(x) for x in color], -1)

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


问题是,即使在使用
pixel=img[start\u point].astype(int)
时,
pixel
的元素也是
类型,而不是
int
类型

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


问题是,即使在使用
pixel=img[start\u point].astype(int)
时,
pixel
的元素也是
类型,而不是
int
类型

哦,哇,我的错。基于缺乏知识,我甚至不知道Int32和“正常”整数之间有什么区别。多谢各位@哇,我的坏。基于缺乏知识,我甚至不知道Int32和“正常”整数之间有什么区别。多谢各位@纳达夫斯