Ask Your Question

imdecode/imwrite PNG file changes from 96 to 72 DPI

asked 2018-05-08 11:13:47 -0500

ronaldwhite gravatar image

updated 2018-05-08 13:54:56 -0500

Apologies in advance for what maybe a silly noob question. I am using the following java code to read and immediately write out a PNG file (attached):

    byte[] buf = getBytesFromFile("./t2.png");
    Mat mat = imdecode(new MatOfByte(buf), IMREAD_COLOR);
    MatOfInt params = new MatOfInt(CV_IMWRITE_PNG_COMPRESSION, 0);
    imwrite("./t3.png", mat, params);

The original file has 96 dpi, and the output file has 72 dpi.

Also, for what its worth, the same thing happens if I replace the first 2 lines with Mat mat = imread("./t2.png");

Why is that? I would like for the output file to have the same settings as the original. Thanks


edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-05-08 11:18:17 -0500

berak gravatar image

updated 2018-05-08 11:27:01 -0500

opencv does not save any dpi information, so the readings from your image viewer are bogus.

(the 72 is likely some default number, when it can't find anything else)

(dpi is important for printing, not for computer-vision. it is already discarded, while reading the image)

edit flag offensive delete link more


well, I am certainly no expert on computer vision, but I believe that DPI settings are important for OCR, which what I am ultimately trying to do.

Let me ask my question a different way - is there a way to imread/imwrite the PNG file so that the original settings are preserved? Thanks

ronaldwhite gravatar imageronaldwhite ( 2018-05-08 11:38:33 -0500 )edit

" is there a way to imread/imwrite the PNG file so that the original settings are preserved?" -- NO. most meta-information (exif tags and such) will be discarded while reading the image already.

the only information retained in a cv::Mat are: W,H,C,depth(or pixel type). nothing else.

berak gravatar imageberak ( 2018-05-08 12:06:36 -0500 )edit

Anything less than 300 dpi is not good. 72 dpi was based off of some 14" monitor back when they used cathode rays, and the electric shock was strong enough to kill a person. It was the stone age.

sjhalayka gravatar imagesjhalayka ( 2018-05-08 15:43:03 -0500 )edit

Question Tools

1 follower


Asked: 2018-05-08 11:13:47 -0500

Seen: 1,528 times

Last updated: May 08 '18