2

I've searched and searched but couldn't find: When I use cv.InRanges I need to place a min and max HSV values. In all the examples I have seen so far they use Cv.Scalar on constants

i tried using it myself but couldnt figure what should be there i tried a list, a tuple and numpy.array all with three values and i keep getting errors.

"a float is requierd" for a list

"a float is required" for tuple

"only length -1 arrays can be converted to python scalars" for numpy.array

Though come to think of it array and list should be the same....

Can anyone please tell me how to use it properly, this is a sample of one of many tries:

def thresh(img, pixel):

    hsv_min = pixel
    hsv_min[0] = hsv_min[0] - 5
    hsv_min[1] = hsv_min[1] - 20
    hsv_min[2] = hsv_min[2] - 20
    hsv_max[0] = hsv_max[0] + 5
    hsv_max[1] = 255
    hsv_max[2] = 255
    cv.InRangeS(ingHSV, cv.Scalar(hsv_min), cv,Scalar(hsv_max), imgThreshold)
Lt_Shade
  • 590
  • 1
  • 7
  • 18
Gsramati
  • 93
  • 1
  • 2
  • 7
  • http://stackoverflow.com/questions/10948589/choosing-correct-hsv-values-for-opencv-thresholding-with-inranges – doctorlove Sep 26 '13 at 13:33

2 Answers2

2

So i figured it out: using:

cv.Scalar(float(hsv_min[0]),float(hsv_min[1]),float(hsv_min[2]))

and then Cv.InRangeS works fine.

Gsramati
  • 93
  • 1
  • 2
  • 7
1

Maybe this code can help you, but it uses numpy & opencv2, I've never tried with opencv first.

import cv2
import numpy

def thresh(img, pixel):
  hsv_min = pixel
  low_threshold   = numpy.array(( hsv_min[0] - 5, hsv_min[1] - 20, hsv_min[2] - 20), dtype=numpy.uint8, ndmin=1)
  high_threshold  = numpy.array(( hsv_max[0] + 5, 255, 255), dtype=numpy.uint8, ndmin=1)
  cv2.InRange(ingHSV, low_threshold, high_threshold, imgThreshold)
double_g
  • 836
  • 7
  • 9
  • 1
    Thanks but it still doesn't work, I get an error: "CvArr argument must be IplImage, CvMat ot CvMatND, use fromarray() to convert numpy toCvMat or cvMatND." Also if I try to use cv.Scalar on the arrays I get, as I mentioned in the question : "only length -1 arrays can be converted to python scalars" – Gsramati Sep 27 '13 at 09:37