Issue
How do I make it so everything in the image is in gray-scale except the orange cone. Using opencv python.
Solution
You can achieve your goal by using bitwise_and()
function and thresholding
.
Steps:
- generate
mask
for the required region.(herethresholding
is used but other methods can also be used) - extract required
regions
usingbitwise_and
(image & mask). - Add
masked regions
to get output.
Here's sample code:
import cv2
import numpy as np
img = cv2.imread('input.jpg')
# creating mask using thresholding over `red` channel (use better use histogram to get threshoding value)
# I have used 200 as thershoding value it can be different for different images
ret, mask = cv2.threshold(img[:, :,2], 200, 255, cv2.THRESH_BINARY)
mask3 = np.zeros_like(img)
mask3[:, :, 0] = mask
mask3[:, :, 1] = mask
mask3[:, :, 2] = mask
# extracting `orange` region using `biteise_and`
orange = cv2.bitwise_and(img, mask3)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
# extracting non-orange region
gray = cv2.bitwise_and(img, 255 - mask3)
# orange masked output
out = gray + orange
cv2.imwrite('orange.png', orange)
cv2.imwrite('gray.png', gray)
cv2.imwrite("output.png", out)
Results:
masked orange image
masked gray image
output image
Answered By - Girish Dattatray Hegde
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.