python - 查找边界之间错误颜色的像素

标签 python numpy colors

在图像中,我有大量由黑色边界分隔的各种颜色的单元格。然而,边界绘制得并不完美,现在一些单元格有一些颜色错误的像素(每个单元格应该只包含一种颜色)。

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

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

最佳答案

听起来您有两个问题:

  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

关于python - 查找边界之间错误颜色的像素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40156713/

相关文章:

python - 如何阻止 Python Sci-kit 库的 Count Vectorizer 进行任何类型的单词过滤?

Python 和 Plotly : custom colors to pie chart via dictionary

emacs - 如何更改Emacs命令行颜色

python - 强制从可能可迭代对象的列表/数组中创建一维 numpy 数组

python - 将两个 numpy 数组相乘时出错

python - numpy 将 int 解析为位分组

python - 范围值到伪彩色

python - 具有默认的 Mac Python 2.7 和 Anaconda Python 3

Python - PySpark 的 Pickle Spacy

python - Pygame 按键时不退出