我正试图判断一张图像是否有像素化。我试图使用拉普拉斯方差法来做这件事,但我不确定它是否正常工作,因为使用我的代码,一个相当扭曲的像素化图像会以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
有没有其他方法来检测图像中的像素化或类似的东西?
我认为你的问题与".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()
原始图像的Laplacian
像素化图像的Laplacian。
差异结果:
variance of original image: 4014.7300553284585
variance of pixelated image: 779.2810668945312
这里有一种方法来判断一个图像是否被像素化。在同一跳过因子的两个不同的偏移处对其进行分解,并计算绝对差异的平均值。如果平均值为零,则为像素化。
在下面的例子中,我使用的跳过系数为2,偏移量为0和1。
原始图像。
像素化图像:
import cv2
import numpy as np
# read original and pixelated image
img1 = cv2.imread('mandril3.jpg')
img2 = cv2.imread('mandril3_pixelated.png')
# decimate images by some skip factor (2) for two different offsets (0 and 1)
dec1A = img1[::2, ::2]
dec1B = img1[1::2, 1::2]
dec2A = img2[::2, ::2]
dec2B = img2[1::2, 1::2]
# get mean of absolute difference
diff1 = cv2.absdiff(dec1A, dec1B)
mean1 = np.mean(diff1)
diff2 = cv2.absdiff(dec2A, dec2B)
mean2 = np.mean(diff2)
print('mean absdiff original image:', mean1)
print('mean absdiff pixelated image:', mean2)
# convert to grayscale
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
平均结果
mean absdiff original image: 20.53973388671875
mean absdiff pixelated image: 0.0
如果我们将FFT应用于你的图像,我们会看到林线,不是很明显,但可见。
这里有标记
你可以沿X轴和Y轴计算直方图,并找出是否有尖峰。如果尖峰高于定义的阈值,就说明图像有像素化。
您可以使用应用程序进行快速实验。 http:/www.jcrystal.comproductsftlseindex.htm