Cropping a portion from a Mat image.

asked 2016-12-07 00:34:37 -0500

VPallavi gravatar image

updated 2016-12-07 00:45:18 -0500

Hello All...I am building an application (in Android using OpenCV library) in which cropping a portion from a Mat type image is required. When I'm running the application it's getting crashed by throwing NullPointerException. My code is as follows:

   nim = Highgui.imread(fpath+"/new/frame" +i+ ".jpg");
   Rect roi = new Rect(x, y, width, height);
   Mat cropped = new Mat(nim, roi);
   Imgproc.cvtColor(cropped, gray, Imgproc.COLOR_RGB2GRAY);

Here x=120, y=120, height=351, width=481; The correspoding logcat is shown below:image description

Please tell where I'm going wrong?? Thanks in advance.

edit retag flag offensive close merge delete


most probably, your image ws not loaded. check nim.empty() after imread(), also make sure, that the desired roi fits into your image

berak gravatar imageberak ( 2016-12-07 02:22:57 -0500 )edit

You need to combine your string before reading. The imread cannot handle concatenation well in my experience.

StevenPuttemans gravatar imageStevenPuttemans ( 2016-12-07 03:09:31 -0500 )edit

@berak:: I checked, image is loading. I just removed "Imgproc.cvtColor" line and it worked. I didn't understand why is it so.

VPallavi gravatar imageVPallavi ( 2016-12-09 07:19:14 -0500 )edit

Because the image is not loading. All the functions before cvtColor do not care if the pointer you are trying to initialize, points to empty/random memory. However cvtColor does care ...

StevenPuttemans gravatar imageStevenPuttemans ( 2016-12-20 07:03:09 -0500 )edit