Python 如何为图像中的像素指定值
我有一个图像,我想给特定的颜色赋值。代码如下:Python 如何为图像中的像素指定值,python,opencv,numpy,Python,Opencv,Numpy,我有一个图像,我想给特定的颜色赋值。代码如下: for x in range(mask.shape[0]): //my image is of size:(400,300) for y in range(mask.shape[1]): if r[x,y] == 1: mask[x,y]=1 elif g[x,y] == 1: mask[x,y] = 1 else
for x in range(mask.shape[0]): //my image is of size:(400,300)
for y in range(mask.shape[1]):
if r[x,y] == 1:
mask[x,y]=1
elif g[x,y] == 1:
mask[x,y] = 1
else
mask[x,y] = 0
这里r,g是从cv.split(mask)生成的
这是正确的方法吗?在opencv中,您可以像在numpy中一样对图像中的像素进行索引/切片:
img[x,y] = 42 #sets the value of pixel at x,y
p = img[x,y] #gets the value of pixel at x,y
如果看不到任何代码,很难判断您可能做错了什么
对于颜色,您将需要考虑第三个维度:
img[x,y,0] = 42 #sets the BLUE value of pixel at x,y
v = img[x,y,1] #gets the GREEN value of pixel at x,y
请记住,在opencv中,颜色的默认顺序是BGR,而不是RGB。规则1:永远不要在像素上循环。(你想要的东西总是有一个内置函数)。显示代码。“我无法想象为什么那样不行。@Markransem我没有写任何代码。我不是编码出身。你能帮助我吗。我只是对如何继续有了一个想法。“我使用了两个for循环”意味着您编写了一些代码。很抱歉,我忘了提到我要为特定颜色赋值。这就是为什么我使用两个for循环。