Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6126199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:20:33+00:00 2026-05-23T16:20:33+00:00

I have a problem with cropping image using OpenCV library on iPhone. I have

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T16:20:34+00:00Added an answer on May 23, 2026 at 4:20 pm

    I had an error in my code. If someone is interested here is working code:

    IplImage *src = [OpenCV CreateIplImageFromUIImage:image];
    
    IplImage *dst = cvCloneImage(src);
    dst->origin = src->origin;
    dst->nChannels = src->nChannels;
    dst->depth = src->depth;
    
    cvZero(dst);
    
    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];
    
    CGFloat width = src->width;
    CGFloat height = src->height;
    
    CvMat* mmat = cvCreateMat(3, 3, CV_32FC1);
    
    CvPoint2D32f *c1 = (CvPoint2D32f *)malloc(4 * sizeof(CvPoint2D32f));
    CvPoint2D32f *c2 = (CvPoint2D32f *)malloc(4 * sizeof(CvPoint2D32f));
    
    c1[0].x = round(p1.x);   c1[0].y = round(p1.y);
    c1[1].x = round(p4.x);   c1[1].y = round(p4.y);
    c1[2].x = round(p2.x);   c1[2].y = round(p2.y);
    c1[3].x = round(p3.x);   c1[3].y = round(p3.y);
    
    c2[0].x = 0;            c2[0].y = 0;
    c2[1].x = width -1;     c2[1].y = 0;
    c2[2].x = 0;            c2[2].y = height - 1;
    c2[3].x = width - 1;    c2[3].y = height - 1;
    
    mmat = cvGetPerspectiveTransform(c1, c2, mmat);
    free(c1);
    free(c2);
    
    cvWarpPerspective(src, dst, mmat, CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS, cvScalarAll(0));
    
    cvReleaseImage(&src);
    cvReleaseMat(&mmat);
    
    UIImage *newImage = [OpenCV UIImageFromIplImage:dst];
    cvReleaseImage(&dst);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem creating new instance of excel 2007 using VBA (from Access 2002).
I'm working in image processing project, and I have this problem: If I made
I have already selected an image from SD card in my activity's ImageView using
I have problem with this method in NLog library: NLog.Targets.Wrappers.AsyncTargetWrapper.ProcessPendingEvents(object state) It consume too
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with ActionLink. I'd like to pass to my ActionLink parameter for
i have problem with autorotate on iphone i set up in all classes -
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
Have problem while getting data from Memcached on .NET MVC solution. I have this
I have problem with displaying bitmap image on imageview on high density screen (480x800).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.