Reputation: 1
This is the image:
I want to turn all the colours to black except yellow and tried this code but showing an error can anyone please help?
import cv2 as cv
import numpy as np
img = cv.imread('Screenshot 2022-04-10 at 10.02.19 AM.png',1)
if(img.any() == [255, 255, 0]):
cv.imshow('image',img);
else:
ret , thresh1 = cv.threshold(img,500,255,cv.THRESH_BINARY);
cv.imshow("Timg",thresh1);
cv.waitKey(0)
cv.destroyAllWindows()
The error is showing for the if conditional statement. The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Upvotes: 0
Views: 874
Reputation: 21233
Steps: TLDR;
Code
# read the image in BGR
img = cv2.imread("image_path", 1)
# convert image to LAB color space
lab = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
# store b-component
b_component = lab[:,:,2]
# perform Otsu threshold
ret,th = cv2.threshold(b_component, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# mask over original image
result = cv2.bitwise_and(img, img, mask = th)
Details
Why did I do this?
The LAB color space contains 3 channels:
Since your image comprised of blue, magenta and yellow, I opted for LAB color space. And specifically the b-component because it highlights yellow color
Upvotes: 1