我正在尝试确定图像是否被平方(像素化).
我听说过2D四重变换有numpy或scipy,但它有点复杂.
目标是确定由于压缩不良导致的平方区域数量(img a):
解决方法:
我不知道这是否可行 – 但是,你可以尝试的是让一个像素周围的最近邻居.像素化的正方形将是区域周围RGB值的可见跳跃.
你可以找到图像中每个像素的最近邻居
def get_neighbors(x,y, img):
ops = [-1, 0, +1]
pixels = []
for opy in ops:
for opx in ops:
try:
pixels.append(img[x+opx][y+opy])
except:
pass
return pixels
这将为您提供源图像区域中最近的像素.
要使用它,你会做类似的事情
def detect_pixellated(fp):
img = misc.imread(fp)
width, height = np.shape(img)[0:2]
# Pixel change to detect edge
threshold = 20
for x in range(width):
for y in range(height):
neighbors = get_neighbors(x, y, img)
# Neighbors come in this order:
# 6 7 8
# 3 4 5
# 0 1 2
center = neighbor[4]
del neighbor[4]
for neighbor in neighbors:
diffs = map(operator.abs, map(operator.sub, neighbor, center))
possibleEdge = all(diff > threshold for diff in diffs)
经过进一步思考后,使用OpenCV进行边缘检测并获得轮廓尺寸.这将更加容易和强大.