提高缩小图像的精度

2024-05-03 18:41:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python2.7和PIL(枕头)

我有一个脚本,采取相当粗糙的图像迷宫,使一个更干净,更小的图像。输入和输出示例:

Sample

从这张图片中生成:

This one

在这种情况下,脚本并没有很好地工作,但工作得很好。你知道吗

然而,相同迷宫的another image产生了这个结果:

less good

那就不太好了。你知道吗

我通过查看16x16网格上每个正方形的平均值来生成并排显示的图像,然后确定正方形是主要代表黑色还是主要代表白色像素。但是,由于透视变换不是完美的,所以正方形并不总是排成一行。你知道吗

有什么算法可以帮助提高精确度吗?有什么方法可以看网格中不是完全正方形的方块吗?你知道吗

我的一段代码:

#This image is already transformed and thresholded, like the first half of my side-by-side images
thresh = Image.open('Thresholded_Image.jpg')
pixsize = thresh.size[0]/16
segments = []
for i in range(16):
    for j in range(16):
        box = (j*pixsize,i*pixsize,(j+1)*pixsize,(i+1)*pixsize)
        segments.append(thresh.crop(box))
def blackWhite(image):
    '''Return `True` if the image is mostly white, else `False`'''
    l=image.convert('L').load()
    w,h=image.size
    lums=sum([[l[x,y] for x in range(w)] for y in range(h)],[])
    return sum(lums)/float(len(lums))>127
whites = []
for y in range(16):
    for x in range(16):
        seg = segments[16*y+x]
        if blackWhite(seg):
            whites.append((x,y))

maze = Image.new('L',(16,16))
l=maze.load()
for w in whites:
    x,y=w
    l[x,y] = 255

Tags: in图像image脚本网格forrange迷宫
1条回答
网友
1楼 · 发布于 2024-05-03 18:41:15

(根据要求,回复评论。)

考虑对靠近正方形中心的像素进行加权,你要对其进行更严格的评估,而那些朝向边缘的像素进行加权,这将有助于解决小的不对齐问题。您也可以尝试定位角点,然后调整图像,使角点形成一个完美的正方形,以帮助打击歪斜。你知道吗

相关问题 更多 >