Ask Your Question

NebuK's profile - activity

2019-08-28 01:15:41 -0500 received badge  Popular Question (source)
2017-02-18 16:03:16 -0500 received badge  Famous Question (source)
2016-04-12 16:02:59 -0500 received badge  Notable Question (source)
2015-12-01 04:01:44 -0500 received badge  Popular Question (source)
2015-03-16 04:56:39 -0500 received badge  Good Question (source)
2015-03-16 04:13:58 -0500 commented question How to use a custom SVM with HOGDescriptor, in Python

Added the import — crd319 was right, thanks for telling quicker than me :-). @crd319: What exactly happens with those svm vectors in the depths of HOG.detectMultiScale is a big mytho for me too — i fear i can not be of much help. A few things i would check is the strides, meanShift can get funky results too. Also, i had „glitches” with too small scales happening where i’d always get full-frame BBoxes. But … all of those i didn’t understand why. Sorry!

2015-03-03 19:00:09 -0500 received badge  Nice Question (source)
2015-03-03 07:30:24 -0500 asked a question How to use a custom SVM with HOGDescriptor, in Python

Hi,

this is a bit against the Idea here, but as its a topic i struggeled quite a bit, i’ll post it here anyways.

The problem is using a custom SVM in HOGDescriptor.detect[MultiScale]. Now, HOGDescriptor wants a float numpy array with mystery content for setSVMDetector(…). As to what the mystery-content is, we can look at the source: https://github.com/Itseez/opencv/blob...

Now the problem is how to get the list of SVs and rho — while CvSVM seems to have facilities for this, they’re not exposed to Python. This is where a dirty but working hack comes in: Saving the SVM to XML, then parsing that file to get the parameters. Something like:

<collect your training data in descs and labels in resps as usual>
import xml.etree.ElementTree as ET
svm = cv2.SVM()
svm.train_auto(descs, resps, None, None, params=svm_params, k_fold=5)

svm.save("svm.xml")
tree = ET.parse('svm.xml')
root = tree.getroot()
# now this is really dirty, but after ~3h of fighting OpenCV its what happens :-)
SVs = root.getchildren()[0].getchildren()[-2].getchildren()[0] 
rho = float( root.getchildren()[0].getchildren()[-1].getchildren()[0].getchildren()[1].text )
svmvec = [float(x) for x in re.sub( '\s+', ' ', SVs.text ).strip().split(' ')]
svmvec.append(-rho)
pickle.dump(svmvec, open("svm.pickle", 'w'))

And for using it:

img = cv2.imread(inp)
hog = cv2.HOGDescriptor((32,64), (16,16), (8,8), (8,8), 9)
svm = pickle.load(open("svm.pickle"))
hog.setSVMDetector( np.array(svm) )
del svm
found, w = hog.detectMultiScale(img)

this seems to work for me — so if anyone ever needs a custom SVM for HOG in OpenCV-Python without touching C++, i hope you can find this post!

Best Regards, hope this helps! Dario Ernst

2015-02-18 05:16:32 -0500 commented answer HoughLinesP vastly different in Python and C++

Hi,

this now works as expected for me. I was rather dumb to overlook that parameter. Thats what you get for intense Cargo-Culting!

Thanks a lot!

2015-02-18 03:17:02 -0500 received badge  Scholar (source)
2015-02-17 09:51:02 -0500 received badge  Nice Question (source)
2015-02-17 09:13:56 -0500 received badge  Editor (source)
2015-02-17 07:39:38 -0500 received badge  Student (source)
2015-02-17 07:37:14 -0500 asked a question HoughLinesP vastly different in Python and C++

Hi all,

I’ve spent some time to debug a pole detector i wanted to build on basis of the probabilistic line transform. The results i got from my python code, using Opencv 2.4.x as well as 3.0.0-beta, always looked just so bad. So i made what i hope to be a kind of self-contained example…

Python

#!/usr/bin/python

import cv2
import numpy as np
import sys
src = cv2.imread(sys.argv[1], 0)

dst = cv2.Canny(src, 50, 200, 3)
color_dst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
lines = cv2.HoughLinesP(dst, 1, np.pi/180, 80, 30, 10)

for x1,y1,x2,y2 in lines[0]:
    cv2.line(color_dst, (x1,y1), (x2,y2), (0,255,0), 3, 8)
cv2.imwrite("test_py.jpg", color_dst)

C++

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/types_c.h>
#include <iostream>

using namespace cv;

int main(int argc, char** argv)
{
    Mat src, dst, color_dst;
    if( argc != 2 || !(src=imread(argv[1], 0)).data)
        return -1;

    Canny( src, dst, 50, 200, 3 );
    cvtColor( dst, color_dst, CV_GRAY2BGR );
    std::vector<Vec4i> lines;
    HoughLinesP( dst, lines, 1, CV_PI/180, 80, 30, 10 );
    for( size_t i = 0; i < lines.size(); i++ )
    {
        line( color_dst, Point(lines[i][0], lines[i][1]),
              Point(lines[i][2], lines[i][3]), Scalar(0,0,255), 3, 8 );
    }

    imwrite( "test_cpp.jpg", color_dst );
    return 0;
}

Results

I’ve taken the example image from the OpenCV docs for HoughLinesP, as this looked to me like a good tasting ground…

Input Image

Input image

C++ Result 2.4.9+3.0.0-beta (expected)

Result of the above using the C++ version

Python 2.7.9 OpenCV 2.4.9 (broken?)

Result of the above using the Python version

Python 2.7.9 OpenCV 3.0.0-beta (totally broken?)

image description

As you can see, in Python with OpenCV 2.4.9 (3.0.0-beta looks worse!), the resulting lines are more, shorter, generally more clutter-y as compared with the C++ version. I’ve also checked the Canny output (visually), and it looks like it’s the same. Did i make any mistake? If not, how can this happen? I thought that python bindings are generated using an automated wrapper generator (swig?), so the code run in the background should be the same, right?

Any hints or tips on this would be greatly appreciated!