Skip to content
This repository has been archived by the owner on Mar 29, 2022. It is now read-only.

Memory usage is summed up, Receive Memory warning , image size is doubled and app crashes #12

Open
beeka5 opened this issue Aug 26, 2015 · 1 comment

Comments

@beeka5
Copy link

beeka5 commented Aug 26, 2015

my app consumes 80 MB above memory after perspective correction. After logging,i found image size was approximately doubled (original size:1936X2592 and perspective corected size:2566X3494). This consumed memory was not relased completely ,only release upto 50MB and internally release somtimes only. So memory usage was summed up for each image's perspective correction and app crashes in device with 512MB ram.Reason was below operation. I don't understand reason to use these operations.

CGFloat w1 = sqrt( pow(anchorPts[2].x - anchorPts[3].x , 2) + pow(anchorPts[2].x - anchorPts[3].x, 2));
CGFloat w2 = sqrt( pow(anchorPts[1].x - anchorPts[0].x , 2) + pow(anchorPts[1].x - anchorPts[0].x, 2));

CGFloat h1 = sqrt( pow(anchorPts[1].y - anchorPts[2].y , 2) + pow(anchorPts[1].y - anchorPts[2].y, 2));
CGFloat h2 = sqrt( pow(anchorPts[0].y - anchorPts[3].y , 2) + pow(anchorPts[0].y - anchorPts[3].y, 2));

CGFloat maxWidth = (w1 < w2) ? w1 : w2;
CGFloat maxHeight = (h1 < h2) ? h1 : h2;

Finally i figure out this was causing memory problem and app crashes..I changes size of perspectively corrected image to original image size. And memory reduces by half. and also app don't crashes.Changes I made

  • (void)correctPerspective{
    // Define the destination image
    cv::Mat original = [OpenCV cvMatFromUIImage:self.pickedImg];

    CGFloat scaleFactor =[self.imgDoc contentScale];
    vector anchorPts;

      CGPoint pt=[boundary.allPoints[0] CGPointValue];
       anchorPts.push_back(Point2f(pt.x/scaleFactor,pt.y/scaleFactor));
    
      pt=[boundary.allPoints[2] CGPointValue];
      anchorPts.push_back(Point2f(pt.x/scaleFactor,pt.y/scaleFactor));
    
       pt=[boundary.allPoints[4] CGPointValue];
      anchorPts.push_back(Point2f(pt.x/scaleFactor,pt.y/scaleFactor));
    
      pt=[boundary.allPoints[6] CGPointValue];
      anchorPts.push_back(Point2f(pt.x/scaleFactor,pt.y/scaleFactor));
    

    cv::Point2f src[4], dst[4];
    src[0].x = anchorPts[0].x;
    src[0].y = anchorPts[0].y;
    src[1].x = anchorPts[1].x;
    src[1].y = anchorPts[1].y;
    src[2].x = anchorPts[2].x;
    src[2].y = anchorPts[2].y;
    src[3].x = anchorPts[3].x;
    src[3].y = anchorPts[3].y;

    dst[0].x = 0;
    dst[0].y = 0;
    dst[1].x = original.cols - 1;
    dst[1].y = 0;
    dst[2].x = original.cols - 1;
    dst[2].y = original.rows - 1;
    dst[3].x = 0;
    dst[3].y = original.rows - 1;

    cv::Mat undistorted= cv::Mat( cvSize(original.cols,original.rows), CV_8UC1);

    cv::warpPerspective(original, undistorted, cv::getPerspectiveTransform(src, dst), undistorted.size());

    original.release();

    finalImg=[OpenCV UIImageFromCVMat:undistorted];
    undistorted.release();

}

@hiRahul
Copy link

hiRahul commented Oct 6, 2016

The two main compiler switches that you would often use are when you build your application with a third party library that is not ARC compliant and vice versa, are
-fno-objc-arc
-fobjc-arc
-f is the switch and no-objc-arc and objc-arc are the options that you are turning on. As evident from the names, the first one turns off ARC and the second turns on.

For example, if your application is ARC enabled but a third party library is not, you use the first switch -fno-objc-arc to exclude the third party library. Conversely, if your application is not yet ARC enabled (gasp!) but the third party library you are integrating is, you use the second switch -fobjc-arc You add these flags to the project from the Build phases tab as shown below.

screen shot 2016-10-06 at 5 20 00 pm

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

No branches or pull requests

2 participants