我正在尝试确定一张图片是否为正方形(像素化)。
我听说过可以使用Numpy或SciPy进行2D Fourier变换,但这有点复杂。
目标是确定由于压缩不良而导致的正方形区域的数量,就像这张图片(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进行边缘检测并获取轮廓大小会更加易于操作和稳定。
有许多算法可以使用,包括在线性图上使用快速傅里叶变换,就像音频一样,您可以使用来自图片的线条进行处理,这比2D图像算法更简单。