如何在python中调整输出图像的大小?

时间:2018-04-28 15:24:15

标签: python opencv image-processing spyder imshow

嗨我在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像素输入图像: input in source code 这是输出: output in source code

我认为图像的像素会影响输出。那么,我怎样才能将源代码中的输出输出到所有图像? 我是否必须调整输入图像的大小?如何?但如果我这样做,我担心它会影响他模糊的结果

2 个答案:

答案 0 :(得分:0)

摘自DOCUMENTATION

  

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

我刚刚使用了问题中的代码,但添加了评论中提到的行cv2.namedWindow("Image", cv2.WINDOW_NORMAL)

# 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)

答案 1 :(得分:0)

如果您想使用与您给出的示例完全相同的分辨率,则可以使用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))