【发布时间】:2011-09-10 12:07:41
【问题描述】:
有一个尺寸为 10x10 像素的图像 (imA) 和更多 60 000 个尺寸为 10x10 的图像 (imN)
所有图片都是黑白的
找到将第一张图片 (imA) 与所有其他图片 (imN) 区分开来的最小点数的任务 - 抱歉我的英语不好,我添加了 img 和评论
我做的第一件事,就是用 numpy 把所有的图片都变成了一个矩阵
q=0
for file in inputImages:
eachImage = os.path.join(generatorFolder, file)
a[q]=numpy.asarray(Image.open(eachImage))
q+=1
b=numpy.asarray(Image.open(templateimage))
b[y,x,color] 为其列表 [255,255,255] 着色
a[1-60000,y,x,颜色]
接下来我使用嵌套比较,深度为 3 点的非递归搜索看起来像这样:
for y1 in range(b.shape[0]):
for x1 in range(b.shape[1]):
for y2 in range(b.shape[0]):
for x2 in range(b.shape[1]):
for y3 in range(b.shape[0]):
for x3 in range(b.shape[1]):
if y1==y2==y3 and x1==x2==x3:continue
check=0
for a_el in range(a.shape[0]):
if numpy.array_equal(b[y1,x1],a[a_el,y1,x1]) and \
numpy.array_equal(b[y2,x2],a[a_el,y2,x2]) and \
numpy.array_equal(b[y3,x3],a[a_el,y3,x3]):
check=1
break
if not check:return 'its unic dots'
这段代码的问题是它非常慢。例如,我们的第一张图片与其他所有图片至少有五点不同:
得到 100! / 95! * 60 000 次比较 - 542,070,144,000,000
没错,我使用了一个稍微不同的算法,它可以让你把它变成: 40!/35!*60000 = 4.737.657.600.000 这不算太少。
有没有办法更漂亮地解决我的问题,而不是蛮力。
更新添加 img
0 行:3 个其他图像 (imN) 4x4
1 行:0 模板图像 (imA) 和 1-3 红色标记处的图像差异 (imA XOR imN)
2 line: 0 图中蓝色标出两点两点供比较,
1 image green its difference, red its compare - difference yes - NEXT
2 image red its compare - difference NO - Break (these two points is not enough to say that imA differs from imN(2))
3 行:与第 2 行一样,其他点
4 行:我们选择了两个点就足以说明 imA 与 imN(1-3) 不同
【问题讨论】:
-
第二行图像:一些在imA中但不在imB中的像素被标记,而另一些则没有被标记。为什么?
-
据我所知,这两个像素不足以区分 imA 和 im1 或 im2。这将是 (0,1) 和 (2,1),这三个组合中的任何一个都没有设置。