在python中检测像素化图像 [英] Detecting a pixelated image in python

查看:362
本文介绍了在python中检测像素化图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我听说过2D四重变换有numpy或scipy但它有点很复杂。

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

目标是确定由于压缩不良导致的平方区域数量(img a):

The goal is to determine an amount of squared zone due to bad compression like this (img a):

推荐答案

<我不知道这是否可行 - 但是,你可以尝试的是让一个像素周围的最近邻居。像素化的正方形将是区域内RGB值的可见跳跃。

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.

您可以使用类似

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)

经过深思熟虑后,使用OpenCV进行边缘检测并获得轮廓尺寸。这将更加容易和强大。

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

这篇关于在python中检测像素化图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆