嗨,我用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)使用此2173x3161输入文件input image
这是输出,the output image,图像是放大的,没有完整显示。
在源代码中,它们使用450x600px输入图像:input in source code,输出是:output in source code
我认为图像的像素会影响输出。那么,我如何才能得到像源代码中的输出一样的输出到所有图像呢?我必须调整输入图像的大小吗?怎么做?但如果我这样做,恐怕会影响他模糊的结果
发布于 2018-05-02 05:34:11
有一种特殊的情况,在这种情况下,您可以创建一个窗口,然后将图像加载到其中。在这种情况下,您可以指定窗口是否可调整大小。这是通过cv2.namedWindow()函数完成的。缺省情况下,该标志为cv2.WINDOW_AUTOSIZE.但是,如果将标志指定为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)发布于 2018-05-07 23:28:22
如果希望使用与所给示例完全相同的分辨率,则可以只使用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))
https://stackoverflow.com/questions/50078086
复制相似问题