Ask Your Question
1

Can OpenCV write binarized image as 1-bit depth indexed PNG?

asked 2018-01-16 09:50:22 -0600

Zaets gravatar image

Hi there!

We're developing an Android app and looking for a tool for compressing doc images as much as possible. We're going to send them to a server for recognition and file size makes sense. Finally these images are binarized for recognition purposes (i.e. contain black and white colors only). We've considered a lot of variants but without success. Now we're trying to apply OpenCV for this namely to save these binarized images as PNG. We'd like to use indexed PNG with 1-bit depth as the most appropriate format for black&white only images. We tested this format manually and it worked quite well giving about 10 compression ratio (comparing JPEG). We found IMWRITE_PNG_BILEVEL parameter which looks relevant for our task. Its description says

Binary level PNG, 0 or 1, default is 0.

OK, we applied it like this

Bitmap image = BitmapFactory.decodeFile(inputFilePath);
Mat matrix = new Mat(image.getHeight(), image.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(image, matrix);
MatOfInt parameters = new MatOfInt(
        Imgcodecs.CV_IMWRITE_PNG_COMPRESSION, 9,
        Imgcodecs.CV_IMWRITE_PNG_BILEVEL, 1
);
boolean result = Imgcodecs.imwrite(outputFilePath, matrix, parameters);

The problem is this gives false result and empty image files. We tried parameters in different combinations. If we set 0 for IMWRITE_PNG_BILEVEL (or remove it) the app creates correct files, otherwise files are empty. We couldn't find any explanation on this parameter excepting the quote above which is not too verbose. So far could anyone either give working example of using this parameter or point out an error in our code or provide some additional explanation on this? This parameter might be not appropriate for our needs at all, who knows? We're not experts in OpenCV and any help will be appreciated!

edit retag flag offensive close merge delete

Comments

We've been working with 3.3.1 version. Some minutes ago I updated OpenCV in the app to the last 3.4.0 version and tested once again. Result is the same, I see empty files. Can you share your working code? Your platform doesn't matter, I'd like to see what we do wrong. And another question: how does this parameter affect your PNG files? We still don't have clear understanding on this...

Zaets gravatar imageZaets ( 2018-01-16 12:29:59 -0600 )edit

Hi, Would be great if you please share your solution you got for this issue, because I also got the same issue, but the following answer (by sturkmen72) works for me. Thanks in advance.

Chinna gravatar imageChinna ( 2018-10-11 02:37:45 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
3

answered 2018-01-16 11:14:38 -0600

updated 2018-01-16 13:20:18 -0600

what is your OpenCV version? i tested with OpenCV 3.4 and setting IMWRITE_PNG_BILEVEL to 1 seems working

test code:

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfInt;
import org.opencv.imgcodecs.Imgcodecs;
public class test
{
   public static void main( String[] args )
   {
      System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
      Mat img = new Mat(1000, 1000, CvType.CV_8UC1);
      Core.randu(img, 0, 2);
      MatOfInt parameters = new MatOfInt(
                Imgcodecs.IMWRITE_PNG_BILEVEL, 1
        );
      Imgcodecs.imwrite("c:/test/test.png", img, parameters);
   }
}

result image is 126.283 bayt

edit flag offensive delete link more

Comments

Thanks a lot! Bug was in another place but your example was quite useful and convinced us not to drop OpenCV for our task. Now it works OK and the solution is quite suitable for us since we already use OpenCV for other purposes. An alternative was to try libpng library and this would take much more efforts.

Zaets gravatar imageZaets ( 2018-01-17 08:06:13 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-16 09:50:22 -0600

Seen: 4,632 times

Last updated: Jan 16 '18