简体   繁体   中英

Why I am not able to extract the object using the masked image?

Here are two images in the drive, one is masked and another one is real image:

https://drive.google.com/drive/folders/1hv3NLQeIHT5Iicgnt74S5qFRG5aJyMLw?usp=sharing

I want to get the masked object out of the real image, not in the white color but in the actual color that it does have.

Here is the code that I hav written:

import cv2
import numpy as np

img = cv2.imread('im010.jpg')
mask = cv2.imread('im010.png')

img_foreground = np.array((mask/255)*img)

cv2.imshow('', img_foreground)
cv2.waitKey()

I have converted all masked elements into one, and multiplied it with the actual image. Since the rest of the pixels that are not masked are black, there it will be zero. If any number multiply with it, it remains zero. Multiplication with one value element ends up the same value of the real image. But I have ploted it, it is showing the same masked img?

Can anyone show me the solution?

Here are 4 different ways to do that in Python/OpenCV/Numpy.

import cv2
import numpy as np

# read input
img = cv2.imread('im010.jpg')


# Method 1 -- Bitwise_And

mask = cv2.imread('im010.png',0)
result1 = cv2.bitwise_and(img, img, mask=mask)
cv2.imwrite('im010_masked1.jpg', result1)
cv2.imshow("result1", result1)
cv2.waitKey(0)
cv2.destroyAllWindows()


# Method 2 -- Python Multiplication

mask = cv2.imread('im010.png')
result2 = (img * mask.astype(np.float64)/255).clip(0,255).astype(np.uint8)
cv2.imwrite('im010_masked2.jpg', result2)
cv2.imshow("result2", result2)
cv2.waitKey(0)
cv2.destroyAllWindows()


# Method 3 -- Numpy Blackening

mask = cv2.imread('im010.png',0)
result3 = img.copy()
result3[mask==0] = (0,0,0)
cv2.imwrite('im010_masked3.jpg', result3)
cv2.imshow("result3", result3)
cv2.waitKey(0)
cv2.destroyAllWindows()


# Method 4 -- Numpy Where

mask = cv2.imread('im010.png')
result4 = np.where(mask==255, img, mask)
cv2.imwrite('im010_masked4.jpg', result4)
cv2.imshow("result4", result4)
cv2.waitKey(0)
cv2.destroyAllWindows()

They all produce the following result:

在此处输入图像描述

your code just needs an additional /255 .

import cv2
import numpy as np

img = cv2.imread('im010.jpg')
mask = cv2.imread('im010.png')

img_foreground = np.array((mask/255)*(img/255))

cv2.imshow('', img_foreground)
cv2.waitKey()

that's because, when you look at the values, they're floats, right? and when they're floats, they must be in the range of 0.0 to 1.0, because that's what imshow expects.

when you give it values scaled differently, it still maps 1.0 to white. any larger value (like 2, 3...) is also white, even if they would be shown as very dark in a range of 0..255, which goes for uint8 type input.

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