Detecting a pixelated image in python

后端 未结 2 852
小鲜肉
小鲜肉 2021-01-19 23:11

I\'m trying to determine if an image is squared(pixelated).

I\'ve heard of 2D fourrier transform with numpy or scipy but it is a bit complicated.

The goal is

2条回答
  •  离开以前
    2021-01-19 23:24

    I have no idea if this would work - but, something you could try is to get the nearest neighbors around a pixel. The pixellated squares will be a visible jump in RGB values around a region.

    You can find the nearest neighbors for every pixel in an image with something like

    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
    

    This will give you the nearest pixels in a region of your source image.

    To use it, you'd do something like

    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)
    

    After further thought though, use OpenCV and do edge detection and get contour sizes. That would be significantly easier and more robust.

提交回复
热议问题