【问题标题】:How to resize output images in python?如何在python中调整输出图像的大小?
【发布时间】:2018-04-28 15:24:15
【问题描述】:

嗨,我在 python 中运行这个模糊检测代码(来源:https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/

# import the necessary packages
from imutils import paths
import argparse
import cv2

def variance_of_laplacian(image):
    # compute the Laplacian of the image and then return the focus
    # measure, which is simply the variance of the Laplacian
    return cv2.Laplacian(image, cv2.CV_64F).var()

# loop over the input images
for imagePath in paths.list_images("images/"):
    # load the image, convert it to grayscale, and compute the
    # focus measure of the image using the Variance of Laplacian
    # method
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    fm = variance_of_laplacian(gray)
    text = "Not Blurry"

    # if the focus measure is less than the supplied threshold,
    # then the image should be considered "blurry"
    if fm < 100:
        text = "Blurry"

        # show the image
    cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
    cv2.imshow("Image", image)
    print("{}: {:.2f}".format(text, fm))
    key = cv2.waitKey(0)

使用这个 2173 x 3161 的输入文件 input image

这是输出显示 the output image 图片已放大,未完整显示。

在源代码中,他们使用 450 x 600 px 的输入图像: input in source code 这是输出: output in source code

我认为图像的像素会影响输出。那么,我怎样才能得到像源代码中的输出一样的输出到所有图像? 我必须调整输入图像的大小吗?如何?但如果我这样做我怕会影响他的模糊效果

【问题讨论】:

  • 你试过cv2.resize()吗?您可以使用它来显示输出图像的外观。 cv2.resize(image, (0,0), fx=0.5, fy=0.5)
  • 为什么不直接创建一个带有WINDOW_NORMAL 标志的namedWindow? (docs) 然后你会得到一个可调整大小的窗口,它可以缩放图像以适应它。添加WINDOW_KEEPRATIO进行缩放以保留图像的纵横比。
  • 如何实现?
  • @DanMa 的意思是在cv2.imshow("Image", image) 之前使用cv2.namedWindow("Image", cv2.WINDOW_NORMAL)。使用它,您将能够随意调整输出窗口的大小。

标签: python opencv image-processing spyder imshow


【解决方案1】:

摘自DOCUMENTATION

有一种特殊情况,您已经可以创建一个窗口并稍后将图像加载到其中。在这种情况下,您可以指定窗口是否可调整大小。它是通过函数 cv2.namedWindow() 完成的。默认情况下,标志是 cv2.WINDOW_AUTOSIZE。但是如果你指定 flag 为 cv2.WINDOW_NORMAL,你可以调整窗口大小。当图像尺寸过大和在窗口中添加轨迹栏时会很有帮助。

我只是使用了问题中的代码,但添加了行 cv2.namedWindow("Image", cv2.WINDOW_NORMAL),如 cmets 中所述。

# import the necessary packages
from imutils import paths
import argparse
import cv2

def variance_of_laplacian(image):
    # compute the Laplacian of the image and then return the focus
    # measure, which is simply the variance of the Laplacian
    return cv2.Laplacian(image, cv2.CV_64F).var()

# loop over the input images
for imagePath in paths.list_images("images/"):
    # load the image, convert it to grayscale, and compute the
    # focus measure of the image using the Variance of Laplacian
    # method
    image = cv2.imread(imagePath)
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    fm = variance_of_laplacian(gray)
    text = "Not Blurry"

    # if the focus measure is less than the supplied threshold,
    # then the image should be considered "blurry"
    if fm < 100:
        text = "Blurry"

        # show the image
    cv2.putText(image, "{}: {:.2f}".format(text, fm), (10, 30),
    cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
    cv2.namedWindow("Image", cv2.WINDOW_NORMAL)       #---- Added THIS line
    cv2.imshow("Image", image)
    print("{}: {:.2f}".format(text, fm))
    key = cv2.waitKey(0)

【讨论】:

    【解决方案2】:

    如果您想使用与您给出的示例完全相同的分辨率,您可以使用 cv2.resize() https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#resize 方法或(如果您想保持 x/y 坐标的比例)使用https://www.pyimagesearch.com/2015/02/02/just-open-sourced-personal-imutils-package-series-opencv-convenience-functions/中提供的imutils类

    您仍然必须决定是否要先调整大小。灰度或调整大小的顺序无关紧要。

    您可以添加的命令: resized_image = cv2.resize(image, (450, 600))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-27
      • 1970-01-01
      • 1970-01-01
      • 2016-08-25
      相关资源
      最近更新 更多