Ask Your Question
0

how can i convert a vector of float into mat image

asked 2014-05-28 15:04:58 -0600

Hm gravatar image

updated 2014-05-29 01:36:05 -0600

berak gravatar image

i i need to convert vector of float to mat image then use this image in imgshow and resize function i have this function but it doesn't work

   memcpy(lbp.data,Vtrn.data(),Vtrn.size()*sizeof(float)); 
    resize(lbp, lbp, cvSize(50, 50),100,100,1); //in order to make all image same size
    imwrite(In.vectorofotrainpath[i].c_str(),lbp);// true with mat image or not

i have this error

   Windows has triggered a breakpoint in mycode.exe.
  This may be due to a corruption of the heap, which indicates a bug in mycode.exe or any of the DLLs it has loaded.

  This may also be due to the user pressing F12 while mycode.exe has focus.

the vector i want to convert is the histograms vector i obtained from this function

vector<float> spatialhist::spatialHistogramforknnorsvm( const Mat& lbpImage, const Size& grid)
{
    vector<float> histograms;
    histograms.resize(grid.width*grid.height*sizeb);
     int width=lbpImage.cols/grid.width;
    int height=lbpImage.rows/grid.height;
    int cnt=0;
    vector<Mat> histogramsimage;
    //#pragma omp parallel for
for(int i=0;i<grid.height;i++)
{
    for(int j=0;j<grid.width;j++)
    {
        Mat cell=lbpImage(Rect(j*width,i*height,width,height));
        Mat cell_hist=computeHistogram(cell);
        histogramsimage.push_back(cell_hist);
        //imshow("FFF",cell_hist);

        Mat tmp_feature;
       Mat feature =cell_hist.reshape(1,1);
        feature.convertTo(tmp_feature,CV_32FC1);

        float * ptr=tmp_feature.ptr<float>(0);
        for(int k=0;k<tmp_feature.cols-1;k++)
        {
            if(ptr[k]==0)
                ptr[k]=1.0/sizeb;
            histograms[(cnt*sizeb)+k]=ptr[k];
          //  cerr << ptr[k] << endl;
        }
        cnt++;
    }

}


return histograms;
}
edit retag flag offensive close merge delete

Comments

when using cv::resize, please give it a Size or scale factors, those things are pretty much xor.

berak gravatar imageberak ( 2014-05-28 15:14:21 -0600 )edit

how can i do this sorry i don't understand i need the image to be 50*50

Hm gravatar imageHm ( 2014-05-28 15:18:39 -0600 )edit

resize(lbp, lbp, cvSize(50, 50));

(that's just general advice, unsure if this is the real error here)

berak gravatar imageberak ( 2014-05-28 15:21:36 -0600 )edit

i run the code with this new resize function but i have the same error

Hm gravatar imageHm ( 2014-05-28 15:27:02 -0600 )edit

yea, already doubted so.

re-edit your question, show a bit more code happening before

berak gravatar imageberak ( 2014-05-28 15:42:01 -0600 )edit

i re-edit the question pls see it and tell me can i use the histogramsimg mat vector for my need instead of converting the histogrames vector or can i convert it from vector of mat into just one mat image

Hm gravatar imageHm ( 2014-05-28 16:02:12 -0600 )edit

or can i convert it from vector of mat into just one mat image

Hm gravatar imageHm ( 2014-05-28 16:10:43 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-05-29 01:35:29 -0600

berak gravatar image

updated 2014-05-29 03:20:48 -0600

// 1. just return a Mat, not a vector<float>
Mat spatialhist::spatialHistogramforknnorsvm( const Mat& lbpImage, const Size& grid)
{
    Mat histograms; // 2. start with an *empty* one, push_back the items later
    int width=lbpImage.cols/grid.width;
    int height=lbpImage.rows/grid.height;
    int cnt=0;
    //#pragma omp parallel for  // 3. dear, make it run, then make it fast. in exactly that order ;)
    for(int i=0;i<grid.height;i++)
    {
        for(int j=0;j<grid.width;j++)
        {
            Mat cell=lbpImage(Rect(j*width,i*height,width,height));
            Mat cell_hist=computeHistogram(cell);
            histograms.push_back(cell_hist);
            // 4. you're already done here.
        }
    }
    return histograms.reshape(1,1); // all in one row now
}
edit flag offensive delete link more

Comments

ok i do it but when i reach the imwrite function i have this error

Unhandled exception at 0x7592812f in mycode.exe: Microsoft C++ exception: cv::Exception at memory location 0x001aefe4.. opencv error: unspecified error<could not find a writer for the specified extension> in cv:: imwrite_, file......\ opencv\modules\highui\src\loadsave.cpp,line275

Hm gravatar imageHm ( 2014-05-29 05:16:12 -0600 )edit

no idea, that's not the code you showed here

berak gravatar imageberak ( 2014-05-29 05:29:48 -0600 )edit

Question Tools

Stats

Asked: 2014-05-28 15:04:58 -0600

Seen: 4,659 times

Last updated: May 29 '14