简体   繁体   English

更改图像中所选ROI的颜色

[英]Change the color of selected ROI in an image

I have an image shown as below, 我有如下图所示,

在此处输入图片说明

I choose three region of interest (ROI) labeled by the red box and I want to remove all yellow colors and replace them with white color (background color). 我选择了用红色框标记的三个感兴趣区域(ROI),并且我想删除所有黄色并将其替换为白色(背景色)。

在此处输入图片说明

My code is, 我的代码是

a=np.copy(img)
a[0:0, 50:50][:,:,:]=255
a[130:270, 210:350][:,:,:]=255
a[0:340, 210:390][:,:,:]=255
plt.imshow(a)

However, the result is unexpected (nothing change). 但是,结果是出乎意料的(不变)。

Having got to a console I can see the problem. 进入控制台后,我可以看到问题所在。

To simulate what you seem to be doing I used a random array with shape (400, 210, 3) 为了模拟您似乎在做什么,我使用了形状为(400,210,3)的随机数组

a = np.random.randint(256, size = (400,210,3))

Your selections 您的选择

a[0:0, 50:50]
Out[10]: array([], shape=(0, 0, 3), dtype=int64)

0:0 and 50:50 both return zero element selections so there's a 3D array with two zero length axes. 0:0和50:50都返回零元素选择,因此存在一个带有两个零长度轴的3D数组。 Setting this to 255 affected no elements. 将此值设置为255不会影响任何元素。

a[130:270, 210:350]
Out[13]: array([], shape=(140, 0, 3), dtype=int64)

In this case 210:350 is out of the range for the axis of length 210. Numpy returns a zero length axis again. 在这种情况下,210:350不在长度轴210的范围内。Numpy再次返回零长度轴。 Setting this to 255 has no effect. 将此设置为255无效。

a[0:350, 210:390][:,:,:] 
Out[14]: array([], shape=(350, 0, 3), dtype=int64)

the axis 1 selection is again out of range so an array with a zero length axis is returned. 轴1的选择再次超出范围,因此返回长度为零的轴的数组。

You are specifying the rectangles as (top left , bottom right) coordinates. 您将矩形指定为(左上,右下)坐标。 It is row_range, column_range that is required. 所需的是row_range,column_range。

I guess you want something like: 我想你想要类似的东西:

a[0:50, 0:50, : ] = 255
a[270:350, 130:210, : ] = 255
a[340:390, 0:210, : ] = 255

The axis 0 selection being the rows (y axis) and axis 1 being the columns, (x axis). 选择的轴0是行(y轴),轴1是列(x轴)。 Axis2 being the rgb components of the colours. Axis2是颜色的rgb分量。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM