Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory allocation error in Wrapper.h #174

Open
spramuditha opened this issue Nov 17, 2015 · 2 comments
Open

Memory allocation error in Wrapper.h #174

spramuditha opened this issue Nov 17, 2015 · 2 comments

Comments

@spramuditha
Copy link

Hi Kyle,
I have successfully run several examples in ofxCv. But I have been trying to run example-blink in ofxFaceTracker . And I ran in to several issues. To help others I will post my answer here for issue 1.
Kyle I need your insight on issue 2.
I am working on Visual Studio 2015 on Windows 7. I have also Installed the Windows 8.1 SDK.

  • Issue 1. Unresolved symbol, trackingDistance(..)
    Solution. There exists two files with the name Tracker.h in ofxFaceTracker and ofxCv. There were many methods to workaround this one. None of them worked for. I had to change the name of one file. And also refactor the code accordingly.
  • Issue 2. Memory Allocation Error.
[warning] ofGLRenderer: drawing an unallocated texture
OpenCV Error: Bad argument (When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified) in cv::arithm_op, file ..\..\..\modules\core\src\arithm.cpp, line 1313_

I have backtracked the error and found it originates from this line in Wrapper.h

        if(black != 0) {
            add(dstMat, cv::Scalar(black), dstMat);
        }

I think the problem is add function taking Mat& type and cv::Scalar types as inputs and trying to give a Mat& output. Please provide a solution.
Thanks a lot.

@kylemcdonald
Copy link
Owner

this might be a change in the newer version of opencv being used in OF 0.9.0.

you could try replacing the line with dstMat += cv::Scalar(black). there is definitely a way to do this kind of thing.

it could also be that dstMat is unallocated for some reason, and that's why add() issues an error: it says "the output array type must be explicitly specified" but that's not possible if the output array is unallocated.

i can't look into this right now, but if you find a solution please send a PR! otherwise i'll take a deeper look when i get the chance.

@spramuditha
Copy link
Author

hi, I am sorry I didn't catch the meaning of PR! But yeah, I found a tweak to avoid the error in add function.
The lines I have commented are replaced by lines followed by KASP-sol-X

    template <class S, class D>
    void CLD(S& src, D& dst, int halfw = 4, int smoothPasses = 2, double sigma1 = .4, double sigma2 = 3, double tau = .97, int black = 0) {
        copy(src, dst);
        int width = getWidth(src), height = getHeight(src);
        //imatrix img;
        //img.init(height, width);
        //KASP-sol-01
        imatrix img = new imatrix(height,width);
        Mat dstMat = toCv(dst);
        if(black != 0) {
            add(dstMat, cv::Scalar(black), dstMat);
        }
        // copy from dst (unsigned char) to img (int)
        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                img[y][x] = dstMat.at<unsigned char>(y, x);
            }
        }
        //ETF etf;
        //etf.init(height, width);
        //KASP-sol-01
        ETF etf = new ETF(height, width);
        etf.set(img);
        etf.Smooth(halfw, smoothPasses);
        GetFDoG(img, etf, sigma1, sigma2, tau);
        // copy result from img (int) to dst (unsigned char)
        for(int y = 0; y < height; y++) {
            for(int x = 0; x < width; x++) {
                dstMat.at<unsigned char>(y, x) = img[y][x];
            }
        }
    }

This avoids error from add function. But Still there was an error in 'etf.smooth' I fixed that too.. For a certain extent. Here's the code for that.

void ETF::Smooth(int half_w, int M)
{
    int i, j, k;
    int MAX_GRADIENT = -1;
    double weight;
    int s, t;
    int x, y;
    double mag_diff;

    int image_x = getRow();
    int image_y = getCol();

    //ETF e2;
    //e2.init(image_x, image_y); 
    //KASP-sol-01
    ETF *e2 = new ETF(image_x,image_y); 

    e2->copy(*this); 

    double v[2], w[2], g[2];
    double angle;
    double factor;

    for (k = 0; k < M; k++) {
        ////////////////////////
        // horizontal
        for (j = 0; j < image_y; j++) {
            for (i = 0; i < image_x; i++) {
                g[0] = g[1] = 0.0;
                v[0] = p[i][j].tx;
                v[1] = p[i][j].ty;
                for (s = -half_w; s <= half_w; s++) {
                    ////////////////////////////////////////
                    x = i+s; y = j;
                    if (x > image_x-1) x = image_x-1;
                    else if (x < 0) x = 0;
                    if (y > image_y-1) y = image_y-1;
                    else if (y < 0) y = 0;
                    ////////////////////////////////////////
                    mag_diff = p[x][y].mag - p[i][j].mag; 
                    //////////////////////////////////////////////////////
                    w[0] = p[x][y].tx;
                    w[1] = p[x][y].ty;
                    ////////////////////////////////
                    factor = 1.0;
                    angle = v[0] * w[0] + v[1] * w[1];
                    if (angle < 0.0) {
                        factor = -1.0; 
                    }
                    weight = mag_diff + 1;  
                    //////////////////////////////////////////////////////
                    g[0] += weight * p[x][y].tx * factor;
                    g[1] += weight * p[x][y].ty * factor;
                }
                make_unit(g[0], g[1]);
                //e2[i][j].tx = g[0];
                //e2[i][j].ty = g[1];
                //KASP-sol-01
                (*e2)[i][j].tx = g[0];
                (*e2)[i][j].ty = g[1];
            }
        }
        //this->copy(e2);
        //KASP-sol-01
        this->copy(*e2);
        /////////////////////////////////
        // vertical
        for (j = 0; j < image_y; j++) {
            for (i = 0; i < image_x; i++) {
                g[0] = g[1] = 0.0;
                v[0] = p[i][j].tx;
                v[1] = p[i][j].ty;
                for (t = -half_w; t <= half_w; t++) {
                    ////////////////////////////////////////
                    x = i; y = j+t;
                    if (x > image_x-1) x = image_x-1;
                    else if (x < 0) x = 0;
                    if (y > image_y-1) y = image_y-1;
                    else if (y < 0) y = 0;
                    ////////////////////////////////////////
                    mag_diff = p[x][y].mag - p[i][j].mag; 
                    //////////////////////////////////////////////////////
                    w[0] = p[x][y].tx;
                    w[1] = p[x][y].ty;
                    ////////////////////////////////
                    factor = 1.0;
                    ///////////////////////////////
                    angle = v[0] * w[0] + v[1] * w[1];
                    if (angle < 0.0) factor = -1.0; 
                    /////////////////////////////////////////////////////////
                    weight = mag_diff + 1; 
                    //////////////////////////////////////////////////////
                    g[0] += weight * p[x][y].tx * factor;
                    g[1] += weight * p[x][y].ty * factor;
                }
                make_unit(g[0], g[1]);
                //e2[i][j].tx = g[0];
                //e2[i][j].ty = g[1]; 
                //KASP-sol-01
                (*e2)[i][j].tx = g[0];
                (*e2)[i][j].ty = g[1];
            }
        }
        //this->copy(e2);
        //KASP-sol-01
        this->copy(*e2);
    }
    ////////////////////////////////////////////
}

however it still keeps giving me errors. So, try to have a look at it. Anyways, all that I have done about is allocating memory dynamically Please send me an email on sachithra.pramuditha@gmail.com. I'll forward if I fixed anything else.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants