我试图判断图像是否像素化。我试图使用拉普拉斯方差方法来执行此操作,但我不确定它是否正常工作,因为使用我的代码返回的图像非常扭曲/像素化,方差非常高,为 1011:
import sys
import cv2
import imutils as im
csv_filename = sys.argv[1]
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
# image = cv2.copyMakeBorder(image, 100, 100, 100, 100, cv2.BORDER_CONSTANT, value = [255, 255, 255])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# invert gray image
gray = 255 - gray
# cv2.imshow("result", gray)
# cv2.waitKey(0)
laplacian_var = cv2.Laplacian(gray, cv2.CV_64F).var()
return laplacian_var
image = im.url_to_image(sys.argv[1])
laplacian_var = variance_of_laplacian(image)
print laplacian_var
是否有另一种方法来检测图像中的像素化或类似的东西?
Here is one image that I would deem pixelated/distorted/blurry for my tests:
![test image](https://i.stack.imgur.com/jjHhB.jpg)
我认为你的问题与“.var()”有关。我不知道那是什么。但这是使用 numpy.var() 执行您想要的操作的一种方法。
但请注意,拉普拉斯方差并不是测试像素化的好方法,除非您知道拥有相同的图像。
原始输入:
像素化输入:
import cv2
import numpy as np
# read original and pixelated image
img1 = cv2.imread('mandril3.jpg')
img2 = cv2.imread('mandril3_pixelated.png')
# convert to grayscale
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
# compute laplacians
laplacian1 = cv2.Laplacian(gray1,cv2.CV_64F)
laplacian2 = cv2.Laplacian(gray2,cv2.CV_64F)
# get variances
variance1 = np.var(laplacian1)
variance2 = np.var(laplacian2)
print ('variance of original image:', variance1)
print ('variance of pixelated image:', variance2)
# save images
cv2.imwrite('mandril3_laplacian.png', (255*laplacian1).clip(0,255).astype(np.uint8))
cv2.imwrite('mandril3_pixelated_laplacian.png', (255*laplacian2).clip(0,255).astype(np.uint8))
# show laplacian using OpenCV
cv2.imshow("laplacian1", laplacian1)
cv2.imshow("laplacian2", laplacian2)
cv2.waitKey(0)
cv2.destroyAllWindows()
原始图像的拉普拉斯:
像素化图像的拉普拉斯算子:
方差结果:
variance of original image: 4014.7300553284585
variance of pixelated image: 779.2810668945312
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)