0

How can I recognize pink wood in an image? I used this code but I did not find any pink small wood in the image.

I expect that if I give such an image as input, the output of pink wood will be recognized.

Other than this method, do you have a suggestion for recognizing pink wood????

input:

output expected (Manually marked)

Code:

import numpy as np


import cv2
from cv2 import *
im = cv2.imread(imagePath)

im = cv2.bilateralFilter(im,9,75,75)
im = cv2.fastNlMeansDenoisingColored(im,None,10,10,7,21)
hsv_img = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)   # HSV image


COLOR_MIN = np.array([233, 88, 233],np.uint8)       # HSV color code lower and upper bounds
COLOR_MAX = np.array([241, 82, 240],np.uint8)       # color pink 

frame_threshed = cv2.inRange(hsv_img, COLOR_MIN, COLOR_MAX)     # Thresholding image
imgray = frame_threshed
ret,thresh = cv2.threshold(frame_threshed,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print(contours)
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    print(x,y)
    cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imwrite("extracted.jpg", im)

output Code:

print(contours)
()

The problem is that pink wood is not recognized

Miladfa7
  • 102
  • 1
  • 7

1 Answers1

2

Change your HSV lower and upper bounds as below:

COLOR_MIN = np.array([130,0,220],np.uint8)    
COLOR_MAX = np.array([170,255,255],np.uint8)  

enter image description here

  • Thank you for your answer. That was the answer. One question, why did you choose this color? [170,255,255] – Miladfa7 Nov 22 '21 at 21:40
  • Just play with those HSV range.Normal HSV range:H = 0-360, S = 0-100 and V = 0-100. Opencv HSV range: H: 0-179, S: 0-255, V: 0-255.Different applications use different scales for HSV. Refer similar problem[https://stackoverflow.com/questions/10948589/choosing-the-correct-upper-and-lower-hsv-boundaries-for-color-detection-withcv] – Nishani Kasineshan Nov 23 '21 at 13:46