简体   繁体   中英

Using OpenCV Python, How would you make all black pixels transparent, and then overlay it over original image

I'm trying to make a colored mask, white. And my idea is to:

  • make black pixels transparent in the mask
  • merge the two images
  • crop images

so then my original masked area will be white. What kind of OpenCV python code/methods would I need?

Like so:

Original

原版的

Mask

面具

Desired result (mocked up - no green edges)

期望的结果(模拟 - 没有绿色边缘)

Instead of

代替

I am assuming your mask is a boolean numpy array and your 2 images are numpy arrays image1 and image2.

Then you can use the boolean array as multiplier.

overlay= mask*image1 + (-mask)*image2

So you get the "True" pixels from image1 and the False pixels from image2

I suppose to do a color threshold to get the mask itself. The result I got in a first quick and dirty attempt with Hue 43-81, Saturation 39-197 and Brightness from 115-255 is: 在此输入图像描述

The next step is a whole fill algorithm to fill the inside of the mask. Note that also one small area to the right is selected.

在此输入图像描述

The next step is a substraction of the two results (mask-filled_mask):

在此输入图像描述

Again fill the wholes and get rid of the noisy pixels with binary opening:

在此输入图像描述

Last mask the image with the created mask.

在此输入图像描述

Every step can be adjusted to yield optimal results. A good idea is to try the steps out (for example with imageJ) to get your workflow set up and then script the steps in python/openCV.

Refer also to http://fiji.sc/Segmentation .

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