Python 在边界之间查找错误的彩色像素
在一幅图像中,我有大量不同颜色的细胞,它们之间用黑色边界隔开。但是,边界绘制得并不完美,现在一些单元格中有少量错误颜色的像素(每个单元格应仅包含一种颜色) 在下图中,我圈出了错误颜色的像素。左上角包围的蓝色像素应为灰色,其他三个点包围的灰色像素应为蓝色 问题:如何找到错误颜色的像素以替换为正确颜色? 目前,我正在使用Python和NumPy将图像加载到数组中,并使用双for循环逐行逐列检查每个像素 我目前的方法是对每个像素检查直接与之相邻的像素(行+1、行-1、列+1和列-1)。如果这些是不同的非黑色,我会检查像素的边界像素,如果它们的颜色与原始像素不同,那么我会更改原始像素的颜色 但是,当多个不正确的像素相邻时,它无法正常工作,导致出现以下图像: 下面是我使用的脚本。我正在寻找一种改进它的方法,或者一种完全不同的算法。代码所需的图像就在它的正下方。我已经将代码中的名称与stackoverflow给它的名称进行了匹配Python 在边界之间查找错误的彩色像素,python,numpy,colors,Python,Numpy,Colors,在一幅图像中,我有大量不同颜色的细胞,它们之间用黑色边界隔开。但是,边界绘制得并不完美,现在一些单元格中有少量错误颜色的像素(每个单元格应仅包含一种颜色) 在下图中,我圈出了错误颜色的像素。左上角包围的蓝色像素应为灰色,其他三个点包围的灰色像素应为蓝色 问题:如何找到错误颜色的像素以替换为正确颜色? 目前,我正在使用Python和NumPy将图像加载到数组中,并使用双for循环逐行逐列检查每个像素 我目前的方法是对每个像素检查直接与之相邻的像素(行+1、行-1、列+1和列-1)。如果这些是不同
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")
这是正确的非缩放大小的示例图像:
听起来您有两个问题:
For each non-black pixel not visited yet:
Start a new region; initialize a counter for each color
Recursively:
Mark the pixel as in-region
Increment the counter for that color
Visit each of the adjacent pixels that are not black nor in-region
When done,
Color all of the in-region pixels to the color with the highest count, and mark them as visited
我将采用连接组件标记方法。。虽然剥猫皮的方法很多
将numpy导入为np
从scipy导入ndimage
从scipy导入统计信息
#输入数组假设0表示黑色1表示蓝色2表示紫色
arr=np.数组(…)
已标记,标签=nImage.measurements.label(arr!=0,#连接非黑色区域
结构=[[0,1,0],
[1,1,1],
[0,1,0]]#这是默认值,但无论如何我们都会显式指定它。。。
对于范围(1,标签+1)内的labelnum:
region=arr[np.where(标签==labelnum)]#获取该区域成员的简单列表
mode=stats.mode(区域)#查找最常出现的颜色
arr[np.where(label==labelnum)]=模式#将该颜色设置为该区域中的所有像素