1

I want to detect pollen grains but the detection mask that I'm generating is not working. These are the results I'm getting:

Original Image:

enter image description here

HSV Image:

enter image description here

Nonworking Mask:

enter image description here

First, I convert the RGB image to HSV. Then, I search for the target contours. I can't use the cv.findCountours function. If you have another idea for a solution, it would be helpful. Also I want to count the number of pollen grains.

import cv2 as cv
import numpy as np

def rgb2hsv(rgb):
       """
       Input: imagen RGB
       Output: imagen HSV
       """
       rgb = np.float32(rgb)/255
       R=rgb[:,:,0]
       G=rgb[:,:,1]
       B=rgb[:,:,2]
       rows,cols=R.shape

       Cmax=np.maximum(np.maximum(R,G),B)
       Cmin=np.minimum(np.minimum(R,G),B)
       delta= np.subtract(Cmax,Cmin)
       #print('delta:',delta)

       #Calculate Hue
       def hue(R,G,B):
               H= np.zeros((rows,cols),dtype=float)
               for row in range(rows):
                       for col in range(cols):
                               if (Cmax[row,col]==0):
                                       H[row,col]=0
                               elif (Cmax[row,col]==R[row,col]):
                                       H[row,col]=60*(((G[row,col]-B[row,col])/delta[row,col])%6)
                               elif (Cmax[row,col]==G[row,col]):
                                       H[row,col]=60*(((B[row,col]-R[row,col])/delta[row,col])+2)
                               elif (Cmax[row,col]==B[row,col]):
                                       H[row,col]=60*(((R[row,col]-G[row,col])/delta[row,col])+4)

               return H

       #Calculate Saturation
       def saturation(Cmax,delta):
               S= np.zeros((rows,cols),dtype=float)
               for row in range(rows):
                       for col in range(cols):
                               if (Cmax[row,col]==0):
                                       S[row,col]=0;
                               else:
                                       S[row,col]=delta[row,col]/Cmax[row,col]
               return S

       #Calculate Value
       V = Cmax.astype(np.float64)
       hsv=cv.merge((hue(R,G,B),saturation(Cmax,delta),V))
       return hsv


img = cv.imread('pollengrains.jpeg', 1)
cv.imshow('original', img)
hsv = rgb2hsv(img)

cv.imshow('hsv',hsv)
cv.imwrite('hsv.jpg',hsv*255)

mask = cv.inRange(hsv, (148,18,59),(168,38,139))
cv.imshow('mask',mask)
cv.imwrite('mask.jpg',mask*255)
cv.waitKey(0)
cv.destroyAllWindows()
stateMachine
  • 5,227
  • 4
  • 13
  • 29
student
  • 23
  • 5
  • Use a Python GUI point picker in HSV color space. Search Google or this forum. I have seen code for several. – fmw42 May 09 '20 at 00:12
  • But a point pickre just generates three numbers and I ned two tuples – student May 09 '20 at 00:14
  • 2
    Pick an H,S,V color for the low color and another H,S,V color for the high color. See https://gist.github.com/trhura/e69fca88bbd941c7024b and https://answers.opencv.org/question/134248/how-to-define-the-lower-and-upper-range-of-a-color/ and https://stackoverflow.com/questions/44480131/python-opencv-hsv-range-finder-creating-trackbars – fmw42 May 09 '20 at 00:15

0 Answers0