简体   繁体   中英

Applying a mask on another mask (or AND condition) for image analysis

I'm trying to do an AND operation on two masks to get their co-positive pixels but it doesn't seem to work - I get the original mask somehow after the AND operation:

plt.imshow(mask);plt.show()
plt.imshow(other_mask);plt.show()
masked_both = cv2.bitwise_and(mask, other_mask)
plt.imshow(masked_both);plt.show()

输出

What's the right way to do it? Thanks

Is is most likely that other_mask contains only positive values, but imshow scales the values. For example, min value of other_mask can be 100 and displayed as purple, and max value can be 200 and displayed as yellow. In that case masked_both will be the same as mask. You can check this by inspecting min and max values of your masks.

If this is the case, you can fix it by normalizing value ranges of both masks:

min_value = mask.min()
max_value = mask.max()
masked_normalized = (mask - min_value) / (max_value - min_value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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