在一幅图像中,我有大量不同颜色的细胞被黑色的边界隔开。但是,边界绘制得并不完美,现在有些单元格有少数错误颜色的像素(每个单元格只能包含一种颜色)。你知道吗
在下图中,我圈出了错误颜色的像素。左上角包围的蓝色像素应为灰色,其他三个点包围的灰色像素应为蓝色。你知道吗
问题:如何找到错误的颜色像素,以便用正确的颜色替换它们?
目前我正在使用Python和NumPy将图像加载到一个数组中,并使用一个双for循环逐行逐列检查每个像素。你知道吗
我目前的方法是为每个像素检查直接与它相邻的像素(行+1、行-1、列+1和列-1)。如果这些像素的颜色与原始像素不同,那么我检查它们的颜色。你知道吗
但是,如果相邻有多个不正确的像素,则无法正常工作,从而导致以下图像:
下面是我使用的脚本。我在寻找一种改进的方法,或者一种完全不同的算法。代码所需的图像就在它的正下方。我已经把代码里的名字和stackoverflow给它的名字匹配起来了。你知道吗
import Image
import numpy as np
BLACK = (0,0,0)
im = Image.open("3gOg0.png").convert('RGB')
im.load()
im_array = np.asarray(im, dtype="int32")
(height, width, dim) = im_array.shape
newim_array = np.array(im_array)
for row in range(height):
for col in range(width):
rgb = tuple(im_array[row,col])
if rgb == BLACK:
continue
n = tuple(im_array[row-1,col])
s = tuple(im_array[row+1,col])
e = tuple(im_array[row,col+1])
w = tuple(im_array[row,col-1])
if n != BLACK and n != rgb:
nn = tuple(im_array[row-2,col])
ne = tuple(im_array[row-1,col+1])
nw = tuple(im_array[row-1,col-1])
if (nn != BLACK and nn != rgb) or (nw != BLACK and nw != rgb) or (ne != BLACK and ne != rgb):
newim_array[row,col] = n
continue
if s != BLACK and s != rgb:
ss = tuple(im_array[row+2,col])
se = tuple(im_array[row+1,col+1])
sw = tuple(im_array[row+1,col-1])
if (ss != BLACK and ss != rgb) or (sw != BLACK and sw != rgb) or (se != BLACK and se != rgb):
newim_array[row,col] = s
continue
if e != BLACK and e != rgb:
ee = tuple(im_array[row,col+2])
ne = tuple(im_array[row-1,col+1])
se = tuple(im_array[row+1,col+1])
if (ee != BLACK and ee != rgb) or (se != BLACK and se != rgb) or (ne != BLACK and ne != rgb):
newim_array[row,col] = e
continue
if w != BLACK and w != rgb:
ww = tuple(im_array[row,col-2])
nw = tuple(im_array[row-1,col-1])
sw = tuple(im_array[row+1,col-1])
if (ww != BLACK and ww != rgb) or (nw != BLACK and nw != rgb) or (sw != BLACK and sw != rgb):
newim_array[row,col] = w
im2 = Image.fromarray(np.uint8(newim_array))
im2.save("fix.png")
这是正确的非缩放大小的示例图像:
我会采取连接元件的标记方法。。虽然剥猫皮的方法很多。。你知道吗
实施示例:
听起来你有两个问题:
要查找区域,并用当前最常见的颜色填充每个区域:
相关问题 更多 >
编程相关推荐