0

This is the image:

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()

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
jraj157
  • 1
  • 1

1 Answers1

1

Steps: TLDR;

  1. Convert image to LAB color space
  2. Otsu Threshold over b-component
  3. Mask the result over the original image in BGR color space

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]

enter image description here

# perform Otsu threshold
ret,th = cv2.threshold(b_component, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU)

enter image description here

# mask over original image
result = cv2.bitwise_and(img, img, mask = th)

enter image description here

Details

Why did I do this?

The LAB color space contains 3 channels:

  • L-component: highlights brightness value
  • a-component: represents color values between green and magenta
  • b-component: represents color values between blue and yellow

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

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
  • @jraj157 You have posted a question based on the final result of this question. Did this answer help you at all? – Jeru Luke Apr 11 '22 at 15:09
  • yes thanks for your help it helped and i tried the code this way. img = cv.imread('/Users/jagnyarajpatnaik/Desktop/Modules_py/Screenshot 2022-04-10 at 10.02.19 AM.png',1) img_HSV = cv.cvtColor(img, cv.COLOR_BGR2HSV) # converting to HSV color range img_threshold = cv.inRange(img_HSV,(25,100,100),(35,255,255)) # making the mask based on hsv range k1 = cv.bitwise_and(img, img, mask = img_threshold) # converting the white color to yellow by bitwise cv.imshow('image',img) cv.imshow("Timg",k1) cv.waitKey(0) cv.destroyAllWindows() – jraj157 Apr 11 '22 at 15:52
  • hope the HSV conversion worked – Jeru Luke Apr 11 '22 at 16:04
  • yeah first converting it into hsv and then masking it to form the black and white img of 0,1 matrix then using bitwise_and operator made me do it. Can u please check the second question that i posted. – jraj157 Apr 11 '22 at 17:26
  • @jraj157 I saw your code. You are going in the right direction by finding lines. Make sure you find 4 strong lines and then find angles between each pair of them – Jeru Luke Apr 11 '22 at 18:27
  • i posted an answer i found all the four line by houghman transform but i m not getting the intersection point like while i am trying to put the text ,the angle at the intersection point it is not showing off so can you please help in this? – jraj157 Apr 12 '22 at 05:44