在python中检测像素化图像

kol*_*llo 5 python image pixel

我正在尝试确定图像是否平方(像素化)。

我听说过使用 numpy 或 scipy 进行二维傅里叶变换,但它有点复杂。

目标是确定由于这样的不良压缩而产生的平方区域的数量(img a):

Mer*_*ing 2

我不知道这是否可行 - 但是,您可以尝试的方法是获取像素周围最近的邻居。像素化方块将是区域周围 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
Run Code Online (Sandbox Code Playgroud)

这将为您提供源图像区域中最近的像素。

要使用它,你会做类似的事情

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)
Run Code Online (Sandbox Code Playgroud)

经过进一步思考,使用 OpenCV 进行边缘检测并获取轮廓尺寸。这将变得更加容易和更加稳健。