Ask Your Question

Crop Image using four points

asked 2013-04-11 23:16:31 -0600

andern gravatar image

I am trying to crop an image then be able to display the image. I have four points in two arrays and it would make a rectangle shape. I have a code that i think would do the job but it is giving an error. Here is the code i hope you can help me out. I am working with android and only with Java. I have looked at but i am unsure how to convert that info over to Java

    int startX =locListX.get(0);
    int startY =locListY.get(0);
    int width=locListX.get(3)-locListX.get(0);
    int height=locListY.get(3)-locListY.get(0);
    Mat image = mFind;
    Rect rectCrop = new Rect(startX, startY, width, height);
    Mat imCrop=  new Mat(image,rectCrop); 
    Utils.matToBitmap(imCrop, bmp5);
edit retag flag offensive close merge delete


Basically can you also show us the code where you fill up the locListX and locListY data object? Because it is difficult to know how the points are defined and in what order. Can we expect something like (topleft, topright, bottomright, bottomleft)?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-12 02:37:28 -0600 )edit

Yea it is basically topleft, topright, bottomright, bottomleft i just want to get a rough rectangle of what i want to see. I used template match to get the points so i would just use the top left point as the start point and then use the sort to get the longest width and height for the image

andern gravatar imageandern ( 2013-04-12 11:21:03 -0600 )edit

Error: Assertion failed (src.dims==2&&info.height == (uint32_t)src.rows&&info.width==(uint32_t)src.cols) in void Java_org_opencv_android_Utils)_nMatToBitmap2(JNIEnv*,jclass,jlong,jobject,jboolean,file /home/reports/ci/slave/opencv/modules/java/generator/src/cpp/util.cpp, line 107

nMatToBitmap catched cv::Exception: /home/report/ci/slave/opencv/modules/java/generator/src/utils.cpp:107: error: (-215) src.dims==2 && info.height ==(uint32_t)src.rows&&info.width==(uint32_t)src.cols in function void Java_org)opencv_android_Utils_nMatToBitmap2(KNIENV*,jclass,jlong,jobject,jboolean)

andern gravatar imageandern ( 2013-04-12 12:53:54 -0600 )edit

Thanks that helped get me a better approach at it and it now works thanks StevenPuttemans

andern gravatar imageandern ( 2013-04-12 16:08:20 -0600 )edit

I will add my solution as an answer, please accept it, so this question show solved :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-12 16:14:03 -0600 )edit

Hi andern, Can you please tell how you solved this error.

Pranjal Jain gravatar imagePranjal Jain ( 2019-04-06 23:35:55 -0600 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-04-12 16:16:31 -0600

Hmm try this approach for calculating the region of interest depending on the fact that you have four points in the following order: topleft, topright, bottomright, bottomleft.

  • Pick the top left as starting point.
  • Then calculate an average y coordinate based on Point 1 and Point 2.
  • Use the x of the top right together with the first average x.
  • Calculate an average x using Point 2 and Point 3.
  • Use the y of point 3 together with the average xas third point.
  • Use the y of point 3 together with the x of point 4 to define the last point.
  • Calculate width and heigth from this set.
  • Set a region of interest.

Then use this region of interest for whatever processing you should need it.

edit flag offensive delete link more


Hi Steven Puttemans. I am new to OpenCV. Could you please explain these calculations with Example. Like If I have (top_leftX, top_leftY),(top_rightX, top_rightY), (bottom_leftX, bottom_leftY), (bottom_rightX, bottom_rightY). How to find ROI based on these coordinates.

sravanalakshmi gravatar imagesravanalakshmi ( 2015-03-23 04:55:54 -0600 )edit

It is described above?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-24 04:17:40 -0600 )edit

Hi Steven Puttemans. Can you please explain the 3rd point "Use the x of the top right together with the first average x" .

Pranjal Jain gravatar imagePranjal Jain ( 2019-04-06 16:07:24 -0600 )edit

Question Tools

1 follower


Asked: 2013-04-11 23:16:31 -0600

Seen: 20,375 times

Last updated: Apr 12 '13