在边界之间查找错误的彩色像素

2024-04-26 00:03:11 发布

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

在一幅图像中,我有大量不同颜色的细胞被黑色的边界隔开。但是,边界绘制得并不完美,现在有些单元格有少数错误颜色的像素(每个单元格只能包含一种颜色)。你知道吗

在下图中,我圈出了错误颜色的像素。左上角包围的蓝色像素应为灰色,其他三个点包围的灰色像素应为蓝色。你知道吗

cells

问题:如何找到错误的颜色像素,以便用正确的颜色替换它们?

目前我正在使用PythonNumPy将图像加载到一个数组中,并使用一个双for循环逐行逐列检查每个像素。你知道吗

我目前的方法是为每个像素检查直接与它相邻的像素(行+1、行-1、列+1和列-1)。如果这些像素的颜色与原始像素不同,那么我检查它们的颜色。你知道吗

但是,如果相邻有多个不正确的像素,则无法正常工作,从而导致以下图像:

wrong

下面是我使用的脚本。我在寻找一种改进的方法,或者一种完全不同的算法。代码所需的图像就在它的正下方。我已经把代码里的名字和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")

这是正确的非缩放大小的示例图像:

enter image description here


Tags: orandif颜色colrgb像素array
2条回答

我会采取连接元件的标记方法。。虽然剥猫皮的方法很多。。你知道吗

  1. 只提取连接组件掩码的黑线
  2. 找到白色的4连通区域(connected component labeling
  3. 对于每个连接区域,找到最常出现的颜色
  4. 将整个区域设置为一种颜色

实施示例:

import numpy as np
from scipy import ndimage
from scipy import stats

#input array assuming 0 for black 1 for blue and 2 for purple
arr = np.array(...)

labeled, labels = ndimage.measurements.label(arr != 0, #connect non-black regions
                                             structure=[[0,1,0],
                                                        [1,1,1],
                                                        [0,1,0]]) #this is the default, but we'll specify it explicitly anyway...

for labelnum in range(1, labels+1):
    region = arr[np.where(labeled==labelnum)] #get a flat list of the members of that region
    mode = stats.mode(region) #find the most occurring color
    arr[np.where(labeled==labelnum)] = mode #set that color to all pixels in that region

听起来你有两个问题:

  1. 有哪些地区?你知道吗
  2. 每个应该是什么颜色?你知道吗

要查找区域,并用当前最常见的颜色填充每个区域:

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

相关问题 更多 >