OpenCV:将特定BGR值的所有像素设置为另一个BGR值 [英] OpenCV: setting all pixels of specific BGR value to another BGR value

查看:97
本文介绍了OpenCV:将特定BGR值的所有像素设置为另一个BGR值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将OpenCV与Python结合使用.我有一幅图像,我要做的是将BGR值[0,0,255]的所有像素都设置为[0,255,255].

我问了一个上一个问题,该问题关于如何对图像进行海报化,以及从答案中,我了解了如何使用索引数组进行索引,例如: 图片[图片> 128] = 255

我了解这是如何工作的,因为image> 128将返回一个满足条件的多维索引数组,然后将该数组应用于图像并将其设置为255.但是,我得到了困惑于如何将其扩展为为数组取值.

我尝试执行以下操作:

      red = np.array([0, 0, 255])
      redIndex = np.where(np.equal(image, red))
      image[redIndex] = np.array([0, 255, 255])

但它不起作用,并显示错误:

ValueError: array is not broadcastable to correct shape

有没有一种有效的方法来解决这个问题?

解决方案

考虑类似数组的图像,如下所示:

>>> red
array([[[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]]])

其所有元素均为[0,0,255].它的形状是2x5x3.只是认为其中还包含其他值. (我不能创建所有这些).

现在,您可以找到[0,0,255]的位置,并将其更改为[0,255,255].您可以按照以下步骤进行操作:

>>> red[np.where((red == [0,0,255]).all(axis = 2))] = [0,255,255]

现在检查红色.

>>> red
array([[[  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255]],

       [[  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255]]])

希望这就是您想要的.

测试结果:

从以下链接中查看代码: https://stackoverflow.com/a/11072667/1134940

我要按照问题将所有红色像素更改为黄色.

所以我在下面的代码末尾添加了这个代码:

im2[np.where((im2 == [0,0,255]).all(axis = 2))] = [0,255,255]

以下是我得到的结果:

如果我想将绿色地面更改为黄色地面:

im2[np.where((im2 == [0,255,0]).all(axis = 2))] = [0,255,255]

结果:

I am using OpenCV with Python. I have an image, and what I want to do is set all pixels of BGR value [0, 0, 255] to [0, 255, 255].

I asked a previous question on how to posterize an image, and from the answer I learned about indexing with an Array of indices, for ex: image[image > 128] = 255

I understand how this works, since image > 128 will return an array of multi-dimensional array of indices that satisfy the condition, and then I apply this array to the image and set those to 255. However, I'm getting confused with how to extend this to doing a value for an array.

I tried doing the following:

      red = np.array([0, 0, 255])
      redIndex = np.where(np.equal(image, red))
      image[redIndex] = np.array([0, 255, 255])

but it doesn't work, with the error:

ValueError: array is not broadcastable to correct shape

Is there an efficient way to handle this?

解决方案

Consider an image like array as below :

>>> red
array([[[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]],

       [[  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255],
        [  0,   0, 255]]])

Its all elements are [0,0,255]. Its shape is 2x5x3. Just think there are other values also in it. (I can't create all those).

Now you find where [0,0,255] are present and change them to [0,255,255]. You can do it as follows :

>>> red[np.where((red == [0,0,255]).all(axis = 2))] = [0,255,255]

Now check the red.

>>> red
array([[[  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255]],

       [[  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255],
        [  0, 255, 255]]])

Hope this is what you want.

Test Results:

Check out the code from this link : https://stackoverflow.com/a/11072667/1134940

I want to change all red pixels to yellow as asked in question.

So i added this below piece of code at the end :

im2[np.where((im2 == [0,0,255]).all(axis = 2))] = [0,255,255]

Below is the result I got :

What if i want to change green ground to yellow ground :

im2[np.where((im2 == [0,255,0]).all(axis = 2))] = [0,255,255]

Result :

这篇关于OpenCV:将特定BGR值的所有像素设置为另一个BGR值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆