Note that there are some explanatory texts on larger screens.

plurals
  1. POCrop image with OpenCV
    text
    copied!<p>I have a problem with cropping image using OpenCV library on iPhone. I have an image with selected area and I'd like to crop image with this area. But the new image isn't map to rectangle and the new image has also blue color of text.</p> <p>I use that code to crop image:</p> <pre><code> IplImage *src = [OpenCV CreateIplImageFromUIImage:image]; NSValue *val = [corners objectAtIndex:0]; CGPoint p1 = [val CGPointValue]; val = [corners objectAtIndex:1]; CGPoint p2 = [val CGPointValue]; val = [corners objectAtIndex:2]; CGPoint p3 = [val CGPointValue]; val = [corners objectAtIndex:3]; CGPoint p4 = [val CGPointValue]; float minX = fmin(fmin(p1.x, p2.x), fmin(p3.x, p4.x)); float minY = fmin(fmin(p1.y, p2.y), fmin(p3.y, p4.y)); float maxX = fmax(fmax(p1.x, p2.x), fmax(p3.x, p4.x)); float maxY = fmax(fmax(p1.y, p2.y), fmax(p3.y, p4.y)); CGFloat width = maxX - minX; CGFloat height = maxY - minY; IplImage *dst = cvCreateImage(cvSize(width, height), 8, 3); p1 = CGPointMake(p1.x - minX, p1.y - minY); p2 = CGPointMake(p2.x - minX, p2.y - minY); p3 = CGPointMake(p3.x - minX, p3.y - minY); p4 = CGPointMake(p4.x - minX, p4.y - minY); IplImage* cropped = cvCreateImage(cvSize(width, height), src-&gt;depth, src-&gt;nChannels); cvSetImageROI(src, cvRect(minX, minY, width, height)); cvCopy(src, cropped, NULL); cvResetImageROI(src); cvReleaseImage(&amp;src); CvMat* mmat = cvCreateMat(3, 3, CV_32FC1); CvPoint2D32f *c1 = (CvPoint2D32f *)malloc(4 * sizeof(CvPoint2D32f)); CvPoint2D32f *c2 = (CvPoint2D32f *)malloc(4 * sizeof(CvPoint2D32f)); c1[0].x = p1.x; c1[0].y = p1.y; c1[1].x = p4.x; c1[1].y = p4.y; c1[2].x = p2.x; c1[2].y = p2.y; c1[3].x = p3.y; c1[3].y = p3.y; c2[0].x = 0; c2[0].y = 0; c2[1].x = width; c2[1].y = 0; c2[2].x = 0; c2[2].y = height; c2[3].x = width; c2[3].y = height; mmat = cvGetPerspectiveTransform(c1, c2, mmat); cvWarpPerspective(cropped, dst, mmat, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0)); </code></pre> <p>I will be grateful for any help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload