0

What I'm trying to do is to detect 2D objects in an image. The differences between this and typical eyes/faces detection however is that the 2D objects I'm trying to detect are not transformed in any way except scaled, rotated or partially blocked (ex. a certain icon in a computer screenshot.)

I have tried to train a Haar cascade with one positive image (the object) and 600 negative image (randomly selected from a background image), however the result was not satisfactory. In particular, opencv_createsamples create weirdly scaled samples (parts are cut off, some are scaled too big). These were the parameters I used:

# Create samples
opencv_createsamples -img positive_images/icon.png -bg negatives.txt\
  -num 1500 -bgcolor 0 -bgthresh 0 -maxxangle 0  -maxyangle 0 -maxzangle 6.28\
  -maxidev 40 -vec pos.vec -maxscale 2 -w 25 -h 25

# Train cascade
opencv_traincascade -data classifier -vec pos.vec -bg negatives.txt\
  -numStages 20 -minHitRate 0.999 -maxFalseAlarmRate 0.5 -numPos 1000\
  -numNeg 600 -w 25 -h 25 -mode ALL -precalcValBufSize 4096\
  -precalcIdxBufSize 4096

Since the task seems to be simpler than a general object detection task, I'm wondering if there are better ways of doing this. If not, what are some possible ways to get a better result?


Edit, here are some sample images that I'm using:

Sample (positive) object:
enter image description here

Sample backgrounds (2 of 600):
enter image description here enter image description here

Some interesting things is that I tried directly using both opencv_createsamples (above) and the wrapper provided here (direct link). With opencv_createsamples the result was not satisfactory. It was not detecting the sample image and classifying other unrelated objects as positive. With the wrapper however the result was a bit better but it only detects the object when there is a white background. I am not sure why using the wrapper would have that effect, but there might be a chance that my parameters, or the way I'm using, was incorrect.

Directory structure:

- positive_images/
    - icon.png
- negative_images/
    - 600 images
- classifier/
- positives.txt
- negatives.txt
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • 3
    How about using feature detectors? – zindarod Sep 27 '17 at 23:07
  • @Zindarod The image might contain multiple objects of the same kind, so I'm not sure if feature detectors would work. – Derek 朕會功夫 Sep 27 '17 at 23:39
  • Please include a few sample images. – zindarod Sep 28 '17 at 00:34
  • yeah... createsamples doesn't work well if original image (of item) has different width and height (if that's the case). Because when it rotates them it just cuts off the image. But there is a way to prevent it creating too big images. `-maxscale` – Arash Rohani Sep 28 '17 at 03:31
  • @Zindarod I have added some details and sample images that I am using to train the cascade classifier. Thanks! – Derek 朕會功夫 Sep 28 '17 at 06:31
  • did you control, whether merging the object with the background did work? If there is a mistake, the alpha channel might not be applied correctly. See https://stackoverflow.com/questions/35125138/opencv-how-to-create-vec-file-to-use-with-opencv-traincascade/35130506#35130506 – Micka Sep 28 '17 at 08:01
  • 1
    @Micka I actually didn't think of converting the image into grayscale before sending it to `createsamples`. It might fix the white background only problem that I will have to test it out later. – Derek 朕會功夫 Sep 28 '17 at 08:08
  • one additonal comment: just converting to grayscale might not be enough. According to the comments under that answer, you'll have to fill be opaque pixels to black (or the value you want to use as background value) – Micka Sep 28 '17 at 09:42

0 Answers0