Ask Your Question

Hansg91's profile - activity

2019-10-11 03:40:27 -0600 received badge  Popular Question (source)
2018-07-09 00:47:08 -0600 received badge  Popular Question (source)
2016-06-15 22:08:09 -0600 received badge  Popular Question (source)
2014-09-20 17:07:13 -0600 answered a question Q: Live Camera feed without preview?

If you mean with without a preview, that you don't show the images, then sure! Basically a simple "hello world" type of application for opencv would be :

  • Open the camera
  • Repeat until done:
    • Grab image
    • Show image

If you omit the last step, then I suppose you have what you are looking for.

2014-09-20 11:44:33 -0600 asked a question VideoCapture latency

Hey!

I am using a VideoCapture object to stream images from my webcam built in my macbook. I notice somewhat of a delay though (in the order of 200-400msec I would guess). If I use Photobooth to show the camera output, there is hardly any lag at all.

I tried to call VideoCapture::read in a separate thread and have the main thread visualize the image, but this had no effect as far as I could tell. Does anyone have a suggestion?

Best regards, Hans

2014-08-21 08:10:53 -0600 received badge  Nice Answer (source)
2014-05-27 07:19:12 -0600 asked a question Gaussian derivative with orientation

Hey all,

I am working out an idea from a paper which talks about taking the Gaussian derivative in 8 orientations. In summary, my question is: How can I do this using OpenCV?

I assume the Gaussian derivative means a difference of Gaussian in a certain orientation. How can I acquire the Gaussian filter in these orientations to use in a convolution? Or is it easier to rotate the image instead?

Best regards, Hans

2014-05-23 03:27:17 -0600 commented answer calcHist on BGR in python

Aha I see, very clear answer. Thank you! I didn't realize the histogram was of combinations of the channels.

2014-05-23 03:26:10 -0600 received badge  Supporter (source)
2014-05-22 16:13:36 -0600 asked a question calcHist on BGR in python

Hey,

I want to use the calcHist function in python on an BGR, but I get some unexpected results. The code I use to compute the histogram is as follows:

hist = cv2.calcHist([image], [0,1,2], (seg == 1).astype(np.uint8), [25,25,25], [0, 255, 0, 255, 0, 255])

However the result of hist is of shape (25, 25, 25). I was expecting to get (25,3), as in 25 bins per channel. What is this result I am getting? The opencv documentation simply states

hist – Output histogram, which is a dense or sparse dims -dimensional array.

But I don't quite understand what this means exactly.

Best regards, Hans

2014-04-30 02:42:48 -0600 commented answer PCA+SVM Explanation

I didn't check your code before, but it seems like you use PCA/SVM fine there. That is of course no guarantee that it'll work ;) good luck!

2014-04-29 09:18:38 -0600 answered a question PCA+SVM Explanation

Your PCA object basically creates a new coordinate system for your data, such that the first principal component captures the 'most interesting data', the second principal component the second most, etc. With most interesting data I mean that the data in this direction has the highest variation.

If you have projected your samples for your SVM classifier using some PCA projection, you need to do that exact same projection if you want to classify a new image. Otherwise you'd be comparing different coordinate systems with eachother, like comparing meters with yards, celsius with fahrenheit, apples with pears.

2014-04-17 06:35:59 -0600 commented answer SVM bias on weights of positives and negatives

Yes I tried that. The thing is the code that handles the conversion for CvSVMParams skips class_weights because of a missing flag for that parameter (CV_PROP_RW). If you add that, a linking error occurs that it cannot convert CvMat* to PyObj and vice versa. Same thing for non pointer, cv::Mat or cv::Mat*.

2014-04-17 05:24:57 -0600 commented answer SVM bias on weights of positives and negatives

As requested, I added the code. Do note, adding these weights should _in theory_ work, but the python opencv bindings have a bug which causes the weights to not be set (as mentioned earlier, I hardcoded them in the OpenCV source just to test the weights).

2014-04-17 03:23:29 -0600 received badge  Enlightened (source)
2014-04-17 03:23:28 -0600 received badge  Good Answer (source)
2014-04-16 04:54:05 -0600 commented question SVM weights parameter not set through Python

Thank you, I checked it out. It seems only properties with the CV_PROP_RW flag get exported. If I add that flag to the CvMat attribute I get a linking error. Same thing when I switch it to a cv::Mat object. Are there no other properties with Mat's that can be used in python?

2014-04-16 03:49:47 -0600 commented question SVM weights parameter not set through Python

Ah great, some proof ! :) How is this generated though? My knowledge of the opencv python bindings is very limited.

2014-04-16 00:55:02 -0600 received badge  Nice Answer (source)
2014-04-15 10:12:26 -0600 received badge  Necromancer (source)
2014-04-15 10:08:01 -0600 answered a question SVM bias on weights of positives and negatives

Regarding the weights, I did some small tests to check the influence. Here are my results:

No weights:

image description

With weights [0.9, 0.1] (0.9 for the largest class, 0.1 for the smallest class):

image description

You can see the change of the weights clearly in these pictures. I hope this clears things up a bit.

On a side note: I tried to do this in python but the class_weights variable does not appear to get set properly (see this question for more information). To circumvent this I had hardcoded the weights in the opencv source, using c++ would've most likely given the correct results as well, as the weights seem to get properly set for c++.

Code used to generate the example:

import numpy as np
import cv2
import matplotlib.pyplot as plt
from os.path import isfile
import scipy.misc
import xml.dom.minidom

"""Originally from: http://stackoverflow.com/questions/8687885/python-opencv-svm-implementation"""
class StatModel(object):
    """parent class - starting point to add abstraction"""
    def load(self, fn):
        self.model.load(fn)
    def save(self, fn):
        self.model.save(fn)

"""Wrapper for OpenCV SimpleVectorMachine algorithm"""
class SVM(StatModel):
    def __init__(self):
        self.model = cv2.SVM()

    def train(self, X, Y):
        #setting algorithm parameters
        params = dict( kernel_type = cv2.SVM_LINEAR, 
                       svm_type = cv2.SVM_C_SVC,
                       C = 1,
                       class_weights = [0.9, 0.1],) # THIS DOES NOT WORK IN OPENCV PYTHON

        self.model.train(X.astype(np.float32), Y.astype(np.float32), params = params)

# generates Gaussian dataset
def gen_gauss_dat(mu1, cov1, N1, mu2, cov2, N2):
    X1 = np.random.multivariate_normal(mu1, cov1, (N1))
    X2 = np.random.multivariate_normal(mu2, cov2, (N2))
    X = np.vstack([X1,X2])
    Y = np.hstack([np.ones((X1.shape[0]))*-1, np.ones((X2.shape[0]))])
    return X, Y

def load_model(path):
    if path == None:
        return None

    if isfile(path) == False:
        return None

    print "Loading model from {}".format(path)
    model = xml.dom.minidom.parse(path)

    # TODO: handle multiple support vectors?
    weights = np.fromstring(model.getElementsByTagName("_")[0].childNodes[0].nodeValue, sep=" ")
    alpha = float(model.getElementsByTagName("alpha")[0].childNodes[0].nodeValue)
    rho = float(model.getElementsByTagName("rho")[0].childNodes[0].nodeValue)
    weights = weights * -alpha
    return weights, rho

# generate Gaussian data
X, Y = gen_gauss_dat([0,0], np.identity(2)*1.5, 1000, [2,2], np.identity(2) * 0.5, 100)

# train on the generated Gaussian data
svm = SVM()
svm.train(X, Y)
svm.save("svm.xml")
w, b = load_model("svm.xml", )

# plot the data
plt.scatter(X[Y==-1,0],X[Y==-1,1], c="g", alpha=0.6, s=100)
plt.scatter(X[Y==1,0],X[Y==1,1], c="r", alpha=0.6, s=100)

x = [-10, 10]
y = [(-w[0] * x[0] - b) / w[1], \
     (-w[0] * x[1] - b) / w[1]]
plt.plot(x, y)
plt.axis("off")
plt.title("Linear SVM classifier Gaussian dataset")
plt.show()
2014-04-15 10:03:41 -0600 asked a question SVM weights parameter not set through Python

Hey,

I am using the SVM implementation in OpenCV (python) and I am running into some trouble setting the class weights parameter. The attribute is called class_weights inside the CVSvmParams class, so I'd expect this to remain the same in the Python interface. I try to assign the parameters as follows:

params = dict( kernel_type = cv2.SVM_LINEAR, 
               svm_type = cv2.SVM_C_SVC,
               C = 1,
               class_weights = [0.1, 0.9],)

However the class_weights variable does not seem to get set inside OpenCV. I added a line to log the value of the _params.class_weights pointer value in CvSVM::train and it prints 0x0.

If I try a similar thing in c++ it does get set. Any idea what is going wrong or what I can further investigate? The wrapper function for CvSvm::train() is the last step I could identify between Python and c++, is there a function before CvSvm::train() that I can check for faults?

Best regards, Hans

2014-04-01 09:25:59 -0600 commented question SVM weights to HOGDescriptor

If I come across something like that I will let you know. It does indeed sounds like some bad implementation. It would be one thing if the algorithms came from different packages, but they should both have been adapted for the 'opencv standard'.

2014-04-01 08:19:11 -0600 commented question SVM weights to HOGDescriptor

Thank you for your reply! I looked through your topic and noticed that when you convert it to a primal form, you use '-myalpha'. I added a minus sign, and I get much better results now! I had only 24 positive and 36 negative examples, so I didn't expect perfect results , but what I have now seems promising. I will try to increase my dataset now, see how that affects my results. Thanks for your help!

2014-04-01 07:44:39 -0600 commented question SVM weights to HOGDescriptor

I updated it twice, to add more detailed information. Sorry about before, I was trying to be brief and precise as to not create an incredibly large post, but I understand it wouldn't help my cause. I would really like to do anything I can to fix this, so let me know if there is anything else I can add and I will do my best to do so.

2014-04-01 07:29:36 -0600 commented question SVM weights to HOGDescriptor

This isn't my complete project, I had already taken the effort to size it down to what it is now. I wanted to show at least how I trained the classifier and that it was indeed working using cross validation, but that it failed to work on the multi scale detection. What do you suggest?

2014-04-01 07:12:40 -0600 asked a question SVM weights to HOGDescriptor

Hey,

I am working on a detection algorithm and I got a working SVM classifier that uses HOG features calculated with a HOGDescriptor object. If I manually test this classifier (ie. feed it with positive and negative examples that have not been used for training), it classifies them fine. The step where I go from my SVM classifier to the HOGDescriptor is where I assume it goes wrong, it doesn't identify a single object correctly. I have no idea where to start how to debug this..

Here is my code: https://dl.dropboxusercontent.com/u/40610835/HOG%20Detection.html

Any help is greatly appreciated!

Best regards, Hans

Update:

To make things clearer so you don't have to go through the entire code, this is how I read in data from the stored SVM classifier:

# load the stored SVM
f = open('model.svm', 'r')
model = yaml.load(f)
rho = model['my_svm']['decision_functions'][0]['rho']
svm_detector = np.array(model['my_svm']['support_vectors'])[0,:]
hog.setSVMDetector(np.hstack([svm_detector, rho]))

There is only one support vector in my classifier, I assumed the SVM weights I need to give to HOGDescriptor.setSVMDetector are these weights with appended the threshold (stored as 'rho' in the yaml file). The HOGDescriptor is initialised as follows :

self.hog = cv2.HOGDescriptor((64,64), (16,16), (8,8), (8,8), 9)

Detection is then simply done using :

locations, weights = hog.detectMultiScale(image)

I didn't quite understand what the parameters in detectMultiScale do exactly and there isn't much documentation regarding their effects. I supplied the SVM with objects of size 64x64 which didn't differ much from the default size of 64x128 so I kept the rest of the parameters the same. I tried 'playing' around with them, but did not achieve much better results.

The summary of my issue now is: If I manually cut out unseen positive/negative examples, my SVM classifier of OpenCV correctly classifies its labels (using SVM.predict). However if I use this classifier inside a HOGDescriptor with mostly default parameters and use HOGDescriptor.detectMultiScale, I get unexpected results (ie. only a few detections at false locations). I have two assumptions, either my parameters are incorrect or my process of getting the SVM weights from the SVM classifier to the HOGDescriptor is false.

For more information, don't hesitate to ask.

Update2:

After reading through this question, I noticed I need to take the negative of my weights when going from SVM -> HOGDescriptor. This improved my results a lot. I used a very small training set, so I didn't expect it to be flawless, this seems to be what I had expected.

Results using negative weights

2014-02-24 13:03:10 -0600 received badge  Nice Answer (source)
2014-02-22 17:44:41 -0600 received badge  Necromancer (source)
2014-02-22 17:44:41 -0600 received badge  Self-Learner (source)
2014-02-22 07:16:07 -0600 answered a question Watershed on gray image

I made some small changes to cv::watershed, it works now on grayscale images as well.

https://github.com/Itseez/opencv/pull/2392

2014-02-19 08:32:47 -0600 commented question Watershed on gray image

I guess I have something to do in the weekend then.. I really need it :P

2014-01-20 04:25:20 -0600 commented question Canny edge detector without thresholds

Aha, I didn't know it uses Sobel to get the initial edge image. Then theres not much use for me as I already have the Sobel image. Thanks!

2014-01-20 02:58:35 -0600 commented question Canny edge detector without thresholds

It does, which is the result of the picture above, but I wanted to compare multiple edge detector results for my project. There is no way to disable this thresholding step then?

2014-01-19 16:49:15 -0600 asked a question Canny edge detector without thresholds

Hey everyone,

I am interesting in getting a edge map with some sort of probability embedded for the edges. This would result in something like this :

image description

The Canny edge detector in opencv seems to force thresholds though, resulting in a binary map. Is there something I can do to disable this? Or am I looking at implementing my 'own' Canny method which does not perform this step?

Best regards, Hans

2014-01-18 05:59:10 -0600 commented question Watershed on gray image

It is, which is why it is giving an error. It expects a 3 channel image.

2014-01-15 06:38:29 -0600 asked a question Watershed on gray image

Hello,

I am trying to perform the watershed transform on a grayscaled image (edge map, to be precise), but the opencv implementation of watershed gives me the error that my image is not in 3 dimensions.

segmentation.cpp:147: error: (-210) Only 8-bit, 3-channel input images are supported in function cvWatershed

Why would this not be supported? Should I modify the watershed algorithm or implement my own based on grayscale images?

Best regards, Hans