在numpy.array的布尔映射中计算1s的组数


16

我现在正在通过PIL(Python图像库)处理Python中的某些图像处理。我的主要目的是计算免疫组织化学图像中有色细胞的数量。我知道有关于它的相关程序,库,函数和教程,我检查了几乎所有它们。我的主要目标是尽可能地从头开始编写代码。因此,我试图避免使用许多外部库和函数。我已经编写了大部分程序。因此,这是逐步进行的操作:

程序接收图像文件:例

并对红细胞进行处理(基本上,它会关闭低于某个红色阈值的RGB值): 在此处输入图片说明

并创建它的布尔图,(因为它很大,将粘贴其中的一部分),它基本上将上面遇到的第二个图像中遇到红色像素的位置放1。

22222222222222222222222222222222222222222
20000000111111110000000000000000000000002
20000000111111110000000000000000000000002
20000000111111110000000000000000000000002
20000000011111100000000000000000001100002
20000000001111100000000000000000011111002
20000000000110000000000000000000011111002
20000000000000000000000000000000111111002
20000000000000000000000000000000111111102
20000000000000000000000000000001111111102
20000000000000000000000000000001111111102
20000000000000000000000000000000111111002
20000000000000000000000000000000010000002
20000000000000000000000000000000000000002
22222222222222222222222222222222222222222

我特意在2s的边界上生成了类似框架的东西,以帮助我计算该布尔映射中1s的组数。

我对你们的问题是,我怎么能有效地计算这种布尔映射中的单元数(1s组)?我发现http://en.wikipedia.org/wiki/Connected-component_labeling看起来极为相关和相似,但据我所知,它是在像素级别。我的处于布尔级别。只有1和0。

非常感谢。


连接组件的标签正是您所需要的。我不知道您为什么认为这有所不同,因为Wikipedia文章也提供了以1和0开头的数组的示例。

我知道它看起来相似(或可能相同),因为英语不是我的母语,所以我无法完全掌握整个Wikipedia页面。页面的“顺序算法”部分看起来像处理1和0,但是我仍然看不到其背后的逻辑。外汇,为什么要先检查北方,东北,西北和西方?

这个问题解决了。

如果感兴趣的细胞重叠,该怎么办?您是否不应该专门寻找圆形特征,以便可以区分看起来相连的两个单元,而不仅仅是找到连续的斑点?
Endlith

当您的免疫组织化学图片质量不好时,细胞重叠,分辨率,像素值的标准偏差等等问题变得更加复杂……我一直在尝试编写一个可以正常工作的小程序罚款,这样的目的,但它看起来像输入图像和它的条件是一个完美的结果很重要..
易卜拉欣C.库尔特

Answers:


6

一种蛮力的方法,但是可以通过将问题反转以对像素集合进行索引以找到区域来完成,而不是对数组进行栅格化。

data = """\
000000011111111000000000000000000000000
000000011111111000000000000000000000000
000000011111111000000000000000000000000
000000001111110000000001000000000110000
000000000111110000000011000000001111100
000000000011100000000000100000011111100
000000000000000000000000000000011111100
000000000000000000000000000000011111110
000000000000000000000000000000111111110
000000000000000000000000000000111111110
000000000000000000000000000000011111100
000000000000000000000000000000001000000
000000000000000000000000000000000000000"""

from collections import namedtuple
Point = namedtuple('Point', 'x y')

def points_adjoin(p1, p2):
    # to accept diagonal adjacency, use this form
    #return -1 <= p1.x-p2.x <= 1 and -1 <= p1.y-p2.y <= 1
    return (-1 <= p1.x-p2.x <= 1 and p1.y == p2.y or
             p1.x == p2.x and -1 <= p1.y-p2.y <= 1)

def adjoins(pts, pt):
    return any(points_adjoin(p,pt) for p in pts)

def locate_regions(datastring):
    data = map(list, datastring.splitlines())
    regions = []
    datapts = [Point(x,y) 
                for y,row in enumerate(data) 
                    for x,value in enumerate(row) if value=='1']
    for dp in datapts:
        # find all adjoining regions
        adjregs = [r for r in regions if adjoins(r,dp)]
        if adjregs:
            adjregs[0].add(dp)
            if len(adjregs) > 1:
                # joining more than one reg, merge
                regions[:] = [r for r in regions if r not in adjregs]
                regions.append(reduce(set.union, adjregs))
        else:
            # not adjoining any, start a new region
            regions.append(set([dp]))
    return regions

def region_index(regs, p):
    return next((i for i,reg in enumerate(regs) if p in reg), -1)

def print_regions(regs):
    maxx = max(p.x for r in regs for p in r)
    maxy = max(p.y for r in regs for p in r)
    allregionpts = reduce(set.union, regs)
    for y in range(-1,maxy+2):
        line = []
        for x in range(-1,maxx+2):
            p = Point(x, y)
            if p in allregionpts:
                line.append(str(region_index(regs, p)))
            else:
                line.append('.')
        print ''.join(line)
    print


# test against data set
regs = locate_regions(data)
print len(regs)
print_regions(regs)

印刷品:

4
........................................
........00000000........................
........00000000........................
........00000000........................
.........000000.........1.........33....
..........00000........11........33333..
...........000...........2......333333..
................................333333..
................................3333333.
...............................33333333.
...............................33333333.
................................333333..
.................................3......
........................................

哇...我不知道该怎么说。很酷。并且完全有效。谢谢保罗。

如果已经有一个功能可以完成很多工作,那么Scipy它可能也会更快。^但是无论如何这可能是一个很好的练习,它显示了一般的操作方法。那我投票。
Zelphir Kaltstahl '16

13

您可以使用ndimage.label,这是一种很好的方法。它返回一个新的数组,每个要素都有唯一的值以及要素的数量。您还可以指定连接元素。

import scipy
from scipy import ndimage
import matplotlib.pyplot as plt

#flatten to make greyscale, using your second red-black image as input.
im = scipy.misc.imread('blobs.jpg',flatten=1)
#smooth and threshold as image has compression artifacts (jpg)
im = ndimage.gaussian_filter(im, 2)
im[im<10]=0
blobs, number_of_blobs = ndimage.label(im)
print 'Number of blobls:', number_of_blobs

plt.imshow(blobs)
plt.show()

#Output is:
Number of blobls: 30

在此处输入图片说明


感谢fraxel。这完全可以作为一种快速而肮脏的解决方案,但是也许我应该提高图像的质量,因为您可以看到有很多合并的单元格。答案应该是30个单元格。再次非常感谢。(编辑:我尝试提高图像的分辨率质量,然后用您的代码将其抹去,但它仍合并了许多单元。它一定是关于flatten = 1或未读工作的方法吗?)

1
@Ibrahim C. Kurt-我已更新以更正此错误(我只是注意到了!)。问题是上传的图像是jpg,因此有大量的伪像。少量的平滑和阈值处理可以解决此问题。应该对png图像完全有效(我认为...)

无需平滑和阈值化。

完全可以。你是对的。无需任何进一步的修改,只需将其从jpg更改为png就足够了。非常感谢。我现在有两个以上的完美答案。我不知道该说些什么:D

@Ibrahim C. Kurt-幸运的你;)

6

这是一个算法O(像素总数+单元像素总数)。我们只需要扫描图片中的像素即可,当找到像素时,我们将其充满以将其擦除。

在Common Lisp中实现,但是您可以轻松地将其转换为Python。

(defun flood-fill (picture i j target-color replacement-color)
  ;; http://en.wikipedia.org/wiki/Flood_fill
  (when (= (aref picture i j) target-color)
    (setf (aref picture i j) replacement-color)
    (when (plusp i)
      (flood-fill picture (1- i) j target-color replacement-color))
    (when (< (1+ i) (array-dimension picture 0))
      (flood-fill picture (1+ i) j target-color replacement-color))
    (when (plusp j)
      (flood-fill picture i (1- j) target-color replacement-color))
    (when (< (1+ j) (array-dimension picture 1))
      (flood-fill picture i (1+ j) target-color replacement-color)))
  picture)


(defun count-cells (picture)
  (loop
    :with cell-count = 0
    :for i :from 0 :below (array-dimension picture 0)
    :do (loop
          :for j :from 0 :below (array-dimension picture 1)
          :unless (zerop (aref picture i j))
          :do (progn (incf cell-count)
                     (flood-fill picture i j 1 0)))
    :finally (return cell-count)))




(count-cells
  (make-array '(128 171) :element-type 'bit
              :initial-contents
              #(#171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111000000000000000000001110000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110000000000000000000011111100
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111000000000000000000000011111100
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111100
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111110
                #171*000000000001110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111110
                #171*000000000011111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111110
                #171*000000000011111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100
                #171*000000000011111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
                #171*000000000001111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
                #171*000000000001111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011
                #171*000000000000011110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011
                #171*000000000000011000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111000000000000000000011
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111100000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111100000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111110000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111000000000000000000000111111110000000000000000000
                #171*000000000000000000111111000000000000000000000000000000000000000000000000000000111110000000000000000000000000000000000001111111100001111000000000011111110000000000000000000
                #171*000000000000000000111111100000000000000000000000000000000000000000000000000011111110000000000000000000000000000000000111111111100011111100000000011111100000000000000000000
                #171*000000000000000000111111100000000000000000000000000000000000000000000000000011111110000000000000000000000000000000000111111111110111111110000000000100000000000000000000000
                #171*000000000000000000111111100000000000000000000000000000000000000000000000000011111100000000000000000000000000000000000111111111110111111110000000000000000000000000000000000
                #171*000000000000000000111111100000000000000000000000000000000000000000000000000011111100000000000000000000000000000000000011111111100111111110000000000000000000000000000000000
                #171*000000000000000000111111100000000000000000000000000000000000000000000000000011111100000000000000000000000000000000000001111111100111111110000000000000000000000000000000000
                #171*000000000000000000011111100000000000000000000000000000000000000000000000000011111001110000000000000000000000000000000000011111000011111110000000000000000000000000000000000
                #171*000000000000000000010110000000000000000000000000000000000000000000000000000000000011111000000000000000000000000000000000000000000000111010000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111000000000000000000000000000000000000000000000000000000111100000000000000000000000
                #171*000000000000000000000000000000000000000000000111100000000000000000000000000000000000111100000000000000000000000000000000000000000000000000000011111111100000000000000000000
                #171*100000000000000000000000000000000000000000001111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100000000000000000000
                #171*111100000000000000000000000000000000000000011111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100000000000000000000
                #171*111100000000000000000000000000000000000000011111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100000000000000000000
                #171*111100000000000000000000000000000000000000111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100000000000000000011
                #171*111100000000000000000000000000000000000000111111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011110000000000000000000111
                #171*111100000000000000000000000000000000000000011111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111
                #171*000000000000000000000000000000000000000000011111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011
                #171*000000000000000000000000000000000000000000000001100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111100000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111100000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000001111111100000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111100000000000000000000000000000000000000000111111000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111100000000000000000000000000000000000000111110000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111100000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111110000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000110000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000011111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000011100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000011000000000000000000000000000000000000000000000000000001111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000001111100000000000000000000000000000000000000000000000000000111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000011111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000111111110000000000000000000000000000000000000000011111100000000000000000000000000000000000000000000000000111111000000000000000000000000000000000000000000000
                #171*000000000000000111111111000000000000000000000000000000000000000011111100000000000000000000000000000000000000000000000001111111100000000000000000000000000000000000000000000
                #171*000000000000000111111110000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000001111111100000000000000000000000000000000000000000000
                #171*000000000000000111111110000000000000000000000000000000000000001111111110000000000000000000000000000000000000000000000001111111110000000000000000001111111000000000000000000
                #171*000000000000000011111110000000000000000000000000000000000000001111111110000000000000000000000000000000000000000000000001111111110000000000000000011111111100000000000000000
                #171*000000000000000001110000000000000000000000000000000000000000000111111110000000000000000000000000000000000000000000000000011111100000000000000000011111111100000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000111111100000000000000000000000000000000000000000000000000001111000000000000000000011111111100000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000011111000000000000000000000000000000000000000000000000000000000000000000000000000011111111100000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111111100000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111000000000000000000000000000000000000000000001111111000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111111100000000000000000000000000000000000000000000100010000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000001111100000000000000000111111110000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111110000000000000011111111110000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111110000000000000011111111110000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111110000000000000011111111110000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111111000000000000011111111110000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000011111110000000000000011111111100000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000001111100000000000000010111111100000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011111000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000001111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000011111111110000000000000000000000000000000000000011111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000011111111110000000000000000000000000000000000000111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000011111111110000000000000000000000000000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000001111111110000000000000000000000000000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000001111111000000000000000000000000000000000000001111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000111111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000000000000000000000000000000000011111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000011111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000011111100000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000001111111110000000000000110000000000000000000000001111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000001111111110000000000001111100000000000000000000011111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000001111111110000000000111111111000000000000000000011111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000111111110000000000111111111000000000000000000111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000111111111000000000111111111100000000000000000111111111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000011111100001111000111111111100000000000000000111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000111111100111111111100000000000000000011111100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000000111111100011111111100000000000000000001110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
                #171*000000000000000000000000001111111100011111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)))
--> 30

嗨,帕斯卡尔,非常感谢您的答复。我看到您的程序非常干净地找到了正确的答案。问题在于我对Common Lisp语言一无所知,但我将尽力弄清楚这一点,并用Python编写类似的脚本。

3

扩展评论多于答案:

正如@interjay所暗示的,在二进制图像(即仅存在两种颜色的二进制图像)中,像素取值为1或0。在您使用的图像表示格式中,这可能是正确的,也可能不是正确的,但这是正确的在图像的“概念”表示中;不要让实施细节使您在此问题上感到困惑。这些实现细节之一是您在图像边界周围使用了2s,这是识别图像周围死区的一种非常明智的方法,但不会对图像的二进制性质产生定性影响。

关于N,NE,NW和W像素的检查:这与像素形成中像素的连通性有关。每个像素(有特殊情况的边界除外)有8个邻居(N,S,E,W,NE,NW,SE,SW),但是哪些像素可以包含在同一组件中?有时仅在拐角处相交的组件(NE,NW,SE,SW)不被视为已连接,而有时却被视为已连接。

您必须决定什么适合您的应用程序。我建议您手工计算顺序算法的一些操作,检查每个像素的不同邻居,以了解发生了什么。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.