tags:

views:

36

answers:

0

Hi all, 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.

I use that code to crop image:

    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->depth, src->nChannels);

cvSetImageROI(src, cvRect(minX, minY, width, height));

cvCopy(src, cropped, NULL);
cvResetImageROI(src);
cvReleaseImage(&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));

I will be grateful for any help.