如何用opencv增加dpi? [英] how to increase dpi with opencv?

查看:732
本文介绍了如何用opencv增加dpi?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用Opencv进行ocr读取之前,我需要提高图像的dpi.问题是:

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

  1. 我现在不知道我的图像dpi
  2. 我不知道如何提高图像的dpi

我在google中搜索,几乎每个答案都建议使用resize

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

cv2.resize()

cv2.resize()

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

resize仅用于更改图像的大小,但毕竟并未增加图像的dpi,因为我尝试使用它,并且当我在photoshop中签入时,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

如何使用opencv做到这一点?

how to do it with opencv?

老实说我需要将dpi更改为300,为什么我需要知道当前的dpi?因为如果它已经dpi > 300,那么我就不需要转换.

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.

我用python做到了

oya i do it with python

推荐答案

dpi只是JPEG/TIFF/PNG标头中的数字.在您打印图像之前,它与世界和他的狗完全无关,然后它确定将以像素为单位的图像尺寸多大.

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.

我不相信您可以使用 OpenCV 进行设置.您当然可以在终端中使用 ImageMagick 进行设置:

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

您可以在终端机中使用exiftool做类似的事情-请注意,exiftool ImageMagick 小得多并且易于维护,因为它是"just" a (功能强大)单个Perl脚本:

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:

从EXIF IFD1信息中提取图像分辨率:

Extract image resolution from EXIF IFD1 information:

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

从图像中提取名称中包含"Resolution"一词的所有标签:

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

exiftool '-*resolution*' image.jpg

image.jpg上设置X/Y分辨率(密度):

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...

使用 ImageMagick 创建没有dpi信息的图像1024x768:

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

convert -size 1024x768 xc:black image.jpg

现在检查一下:

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
  ...
  ...

现在更改dpi并设置dpi单位,然后再次检查:

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.

这篇关于如何用opencv增加dpi?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆