Home > front end >  How To Grab Parts of Specific Color from one Image and Draw Them into Another Image Using OpenCV Pyt
How To Grab Parts of Specific Color from one Image and Draw Them into Another Image Using OpenCV Pyt

Time:12-24

I have two pictures called pic1.jpg and pic2.jpg, and these two pictures are of the same size (same width, same height).

I want to take those parts whose color is yellow (rgb=255,255,0) from pic1, and then draw them to pic2 at the same position.

How can I do this via opencv-python? I googled and tried below code, but it doesn't work.

image1 = cv2.imread('pic1.jpg')
image2 = cv2.imread('pic2.jpg')

hsv = cv2.cvtColor(image1, cv2.COLOR_BGR2HSV)

# only want the yellow parts
lower_color = np.array([0, 255, 255])
upper_color = np.array([0, 255, 255])

# 
mask = cv2.inRange(hsv, lower_color, upper_color)

# add them to image2
result = cv2.bitwise_and(image2, image2, mask=mask)

cv2.imwrite('final.jpg', result)

CodePudding user response:

We can't use cv2.bitwise_and for replacing masked pixels in image2 with pixels from image1.
In C we may use enter image description here

image2:
enter image description here


result:
enter image description here

mask:
enter image description here


Update:

Code sample without converting to HSV color space:

Conversion to HSV is useful for finding larger range of yellow (yellowish) colored pixels.
Example: Searching pixels with Hue=30, Saturation in range [100, 255] and Value in range [30, 255] returns large range of yellow pixels (dark yellow, bright yellow...).

When looking for pure bright yellow, we may apply cv2.inRange to BGR color format, and search for pixels with Blue=0, Green=255, Red=255.
The example uses a bit wider range [0, 250, 250] to [5, 255, 255] (mainly because the JPEG compression modifies the original values).

Code sample, without converting to HSV:

import cv2
import numpy as np

image1 = cv2.imread('pic1.jpg')
image2 = cv2.imread('pic2.jpg')

# Only want the yellow parts. Yellow in BGR equls [0, 255, 255]
lower_color = np.array([0, 250, 250])
upper_color = np.array([5, 255, 255])

mask = cv2.inRange(image1, lower_color, upper_color)  # Apply inRange to image1 in BGR color format
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)  # Convert maks to 3D array - as np.concatenate((mask,mask,mask))

result = image2
result[mask==255] = image1[mask==255]  # Use logical indexing for replacing the masked pixels in image2 with pixels from image1.

cv2.imwrite('final.jpg', result)
  • Related