-1

I was writing a program to resize an image while preserving the aspect ratio. Basically, I'm trying to get the source image into a 20x20 box with the aspect ratio preserved.

float ra= (float)20/image.width();
Size dsize = new Size((int)(image.height()*ra),20);
// System.out.println(image.height() + " "+image.width());
//System.out.println((int)(image.height()*ra)+" " + 20);
Imgproc.resize(image,output, dsize,ra,ra, Imgproc.INTER_LINEAR);    

The input, image is a Mat. image.height()=10, image.width()=28. The new size would be 7,20. However when i run the code I get a null pointer exception at Imgproc.resize(). I am unable to find the reason.
I get the same error even if I run

Imgproc.resize(image,output,dsize);
  • I'm sure dsize is not null. image and output are also not null as it works if i put dsize=new Size(20,20); This will not preserve aspect ratio. – user3531996 Jun 10 '14 at 07:46

2 Answers2

0
  • use either dsize or a scale factor, not both.

  • you probably forgot:

    output = new Mat(); // you can pass empty Mat's, but not null

berak
  • 39,159
  • 9
  • 91
  • 89
0

Turns out I was interchanging the rows and columns while strong the image into Mat. I was using BufferedImage and mixed up the height and width. It works now.. Thanks everyone!!