Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to increase dpi with opencv?

Tags:

I need to increase dpi my image before read by ocr with opencv. the problem are :

  1. I dont know my image dpi right now
  2. and I dont know how to increase dpi of the image

i search in google, and almost every answer suggest using resize

cv2.resize()

image = cv2.imread("source.png")
resized_image = cv2.resize(image, (100, 50)) #I need to change it to 300 DPI

resize only to change size of image, but after all not increase dpi in image, because i tried use it, and when i check in photoshop, dpi not changed

show please help me for that 2 questions

how to do it with opencv?

honestly I need to change dpi to 300, why i need to know current dpi? because if it already dpi > 300, so I dont need to convert it.

oya i do it with python

like image 967
yozawiratama Avatar asked May 24 '18 07:05

yozawiratama


People also ask

How can I increase dpi of an image?

To change an image's DPI in Photoshop, go to Image > Image Size. Uncheck Resample Image, because this setting will upscale your image, which will make it lower quality. Now, next to Resolution, type in your preferred resolution, set as Pixels/Inch.

How do I change the resolution of an image in OpenCV Python?

The first step is to create an object of the DNN superresolution class. This is followed by the reading and setting of the model, and finally, the image is upscaled. We have provided the Python and C++ codes below. You can replace the value of the model_path variable with the path of the model that you want to use.

How to resize an image in OpenCV?

Let’s begin by taking a look at the OpenCV resize () function syntax. Notice that only two input arguments are required: The source image. The desired size of the resized image, dsize. We will discuss the various input argument options in the sections below.

Does CV2 resize resize increase DPI?

I searched in Google, and almost every answer suggests using cv2.resize resize only changes the size of image, but after all does not increase the dpi. I tried to use it, and then checked in Photoshop, the dpi was not changed. How to do it with opencv? I need to change dpi to 300, why do I need to know current dpi?

What is the use of interpolation in OpenCV?

It also helps in zooming in images. Many times we need to resize the image i.e. either shrink it or scale up to meet the size requirements. OpenCV provides us several interpolation methods for resizing an image. cv2.INTER_AREA: This is used when we need to shrink an image. cv2.INTER_CUBIC: This is slow but more efficient.

How can I improve the performance of my OpenCV algorithm?

Vectorize the algorithm/code to the maximum extent possible, because Numpy and OpenCV are optimized for vector operations. Exploit the cache coherence. Never make copies of an array unless it is necessary. Try to use views instead.


Video Answer


1 Answers

The dpi is just a number in the JPEG/TIFF/PNG header. It is entirely irrelevant to the world and his dog until you print the image and then it determines how large the print will be given the image's dimensions in pixels.

During image processing, it is irrelevant. The only thing of any interest is the number of pixels you have. That is the ultimate determinant of image quality, or information content - however you want to describe it.

I don't believe you can set it with OpenCV. You can certainly set it with ImageMagick like this in the Terminal:

mogrify -set density 300 *.png           # v6 ImageMagick
magick mogrify -set density 300 *.png    # v7 ImageMagick

You can check it with:

identify -format "Density: %x x %y" SomeImage.jpg    # v6 ImageMagick
magick identify -format ... as above                 # v7 ImageMagick

You can do similar things with exiftool in Terminal - note that exiftool is MUCH smaller and easier to maintain than ImageMagick because it is "just" a (very capable) single Perl script:

Extract image resolution from EXIF IFD1 information:

exiftool -IFD1:XResolution -IFD1:YResolution image.jpg

Extract all tags with names containing the word "Resolution" from an image|:

exiftool '-*resolution*' image.jpg

Set X/Y Resolution (density) on image.jpg:

exiftool -xresolution=300 -yresolution=300 image.jpg

Here is a little demonstration of what I mean at the beginning of my answer...

Use ImageMagick to create an image 1024x768 with no dpi information:

convert -size 1024x768 xc:black image.jpg

Now examine it:

identify -verbose image.jpg

Image: image.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: PseudoClass
  Geometry: 1024x768+0+0
  Units: Undefined
  Colorspace: Gray
  Type: Bilevel
  ...
  ...

Now change the dpi and set the dpi units and examine it again:

mogrify -set density 300 -units pixelsperinch image.jpg   # Change dpi

identify -verbose image.jpg                               # Examine

Image: image.jpg
  Format: JPEG (Joint Photographic Experts Group JFIF format)
  Mime type: image/jpeg
  Class: PseudoClass
  Geometry: 1024x768+0+0            <--- Number of pixels is unchanged
  Resolution: 300x300               <---
  Print size: 3.41333x2.56          <--- Print size is now known
  Units: PixelsPerInch              <---
  Colorspace: Gray
  Type: Bilevel
  ...
  ...

And now you can see that suddenly we know how big a print will come out and that the number of pixels has not changed.

like image 141
Mark Setchell Avatar answered Sep 19 '22 12:09

Mark Setchell