Ask Your Question

How to copy vector to a row of a Mat using openCV 2.4.11?

asked 2017-08-20 17:51:16 -0500

linengmiao gravatar image


How to copy an entire vector to a row of a Mat using openCV 2.4.11?

I tried those ways, but none of them worked:

        vector<float> ders;
        Mat Hogfeat;

        Hogfeat.row(j) = ders.clone();

But I get those errors at compile time:

         main.cpp:75:26: error: ‘class std::vector<float>’ has no member named ‘clone’
                  Hogfeat.row(j) = ders.clone();

        main.cpp:76:9: error: ‘class std::vector<float>’ has no member named ‘copyTo’

I would like to not use for loops to copy every element of the vector "manually" to the Mat in question. What am I doing wrong or what alternatives are there?


edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted

answered 2017-08-20 18:01:40 -0500

berak gravatar image

updated 2017-08-20 18:06:39 -0500

i'm guessing, that you want Mat Hogfeat; for further ml processing, where you'd need each feature on a row, while just putting a vector<float> into a cv::Mat gives you a column Mat:

vector<float> ders;
Mat Hogfeat;

HogFeat.push_back( Mat(ders, true).reshape(1,1) );  // deep copy & reshape
edit flag offensive delete link more


p.s. - why on earth are you using the outdated 2.4 branch ?

berak gravatar imageberak ( 2017-08-20 18:16:50 -0500 )edit

something seems to be wrong with this approach: It still fils the Mat column per column. I tried to fill the Mat differently for debugging purposes (and to avoid having to transpose):

     std::cerr<<"der size: "<<ders.size()<<std::endl;
hogFeat.push_back( Mat(ders, true).reshape(1,1) );
std::cerr<<"hogFeat size: "<<hogFeat.size();

    corresponding output:

     der size: 22680

     hogFeat size: [22680 x 1]read 1

     der size: 22680

     hogFeat size: [22680 x 2]read 2

     der size: 22680

     hogFeat size: [22680 x 3]read 3


     der size: 22680

     hogFeat size: [22680 x 6]

I would have expected 1x22680; 2x22680 as each row contains the features for ML.

linengmiao gravatar imagelinengmiao ( 2017-08-21 02:28:26 -0500 )edit

I am filling the Mat in a loop. So what happens is that every iteration I read an image, resize it to 192x192 and calculate the histogram of gradient of it using a 62x62 kernel. I obtain a vector (ders) of length 22680 and am trying to put that into the Mat for ml (SVM), so that later I can perform object recognition. those are the positive samples: and negative ones: (which are supposed to represent the environment of where the people are whose hands we are going to detect). Currently for testing purposes I only used 10 images. The goal is to detect peoples hands in a room to later track them and analyze their movements. Current issue is that no hand is being detected on this image:

linengmiao gravatar imagelinengmiao ( 2017-08-21 02:33:04 -0500 )edit

I would have expected 1x22680;

no, it simply prints [W x H], so all ok, i'd think ;)

Current issue is that no hand is being detected

your hog features are very long (10x of the usual), and you're onlyusing 10 images (x1000 needed)

berak gravatar imageberak ( 2017-08-21 02:47:16 -0500 )edit

What do you suggest for the hog features, if you say they are 10x bigger than the usual? This is how I did it: I think the way i did lays in line with the examples provided by the docs and the infor I could find online.

Also I currently don't have the time to download 1000 images of hands :) , I ll try to do that later, currently I am using this just for testing purposes. Do I understand this corrrectly: if I have 1k images or less I can use eg SVM, while if I have more than 1k images I m better off using neural nets?

linengmiao gravatar imagelinengmiao ( 2017-08-21 02:51:27 -0500 )edit

the params in hog.compute() influence the size, see here

if you want to train the HOGDescriptor for detection, you''ll also need different SVM params (SVR & LINEAR kernel), again, please look at train_HOG.cpp

... and you simply need a LOT of images in any case, no matter what algo you use.

berak gravatar imageberak ( 2017-08-21 02:57:20 -0500 )edit

thanks for the hints, I have read the docs and had a look at the source code. Note that I already have a linear kernel (line 83) and use C_SVC as a SVM type rather than SVR because I want classification (hand/ no hand) rather than regression in order to detect a hand in an image. Feel free to correct me if I misunderstood what you meant or something else.

linengmiao gravatar imagelinengmiao ( 2017-08-21 03:09:37 -0500 )edit

ah, ok. difference is probably only important, if you want to train it for detectMultiscale()

berak gravatar imageberak ( 2017-08-21 03:19:46 -0500 )edit

hmm well I want to detect the location of hands in an image. So I guess I'll need the function detectMultiScale at one moment or another.... So does that mean I have to modify my parameters (although -without wanting to sound rude- what you suggest doesn't seem to be what I need)?

linengmiao gravatar imagelinengmiao ( 2017-08-21 03:24:36 -0500 )edit

oh, no problem at all.

let's see, what you find, when you're there ?

berak gravatar imageberak ( 2017-08-21 03:32:24 -0500 )edit

Question Tools

1 follower


Asked: 2017-08-20 17:51:16 -0500

Seen: 1,833 times

Last updated: Aug 20 '17