繁体   English   中英

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

[英]How to resize output images in python?

嗨,我在python中运行此blurdetection代码(来源: 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输入文件输入图像

这是输出显示输出图像图像正在放大并且没有显示完整。

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

我认为图像的像素会影响输出。 那么,如何获得与所有图像一样的输出,如源代码中的输出? 我是否需要调整输入图像的大小? 如何? 但如果我这样做,恐怕会影响他模糊的结果

摘录自文档

在一种特殊情况下,您已经可以创建一个窗口并稍后将图像加载到该窗口。 在这种情况下,您可以指定窗口是否可调整大小。 这是通过函数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)

如果您想使用与示例相同的分辨率,则可以使用cv2.resize() https://docs.opencv.org/2.4/modules/imgproc/doc/geometric_transformations.html#调整大小方法或(如果要保持x / y坐标的比例)使用https://www.pyimagesearch.com/2015/02/02/just-open-sourced-personal-imutils中提供的imutils类-package-系列-opencv便利功能/

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM