将2d数组数据视为定义形状的像素——是否可能创建内部和表面?
Say I have the following array/matrix in python (any language will do, to be honest). A =
假设我在python中有以下数组/矩阵(老实说,任何语言都可以)。一个=
[0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 1 1 1 1 1 0 0 0 0
0 1 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 1 0 0
0 0 1 1 1 1 1 0 0 0
0 0 1 1 1 1 1 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
Is there any easy way to manipulate entries in this array as if they are pixels. Example, would there be any way to create an interior and a surface of arbitrary length, to get, for example:
是否有简单的方法来操作这个数组中的条目,就好像它们是像素一样。例如,是否有任何方法来创建一个任意长度的内部和表面,例如:
[0 0 0 0 0 0 0 0 0 0
0 0 1 1 1 1 0 0 0 0
0 1 1 0 0 1 0 0 0 0
0 1 1 0 0 0 1 0 0 0
0 0 1 0 0 0 1 1 0 0
0 0 1 0 0 0 1 0 0 0
0 0 1 1 0 1 1 0 0 0
0 0 0 1 1 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0]
Perhaps there is some way to recursively solve this, or maybe in a different way, which would rely on the entries around each element. Any thoughts?
也许有某种方法可以递归地解决这个问题,或者可以用另一种方法,这种方法依赖于每个元素周围的条目。任何想法吗?
2 个解决方案
#1
2
You can just check the value and the direct neighborhood of every pixel: Your resulting image is 1 where:
只需检查每个像素的值和直接邻域:得到的图像为1,其中:
- the original value was 1
- 初始值是1
- and at least one neighbor is 0
- 至少有一个邻边是0
It is 0 everywhere else. The image below might help to illustrate what I mean:
其他地方都是0。下面的图片可能有助于说明我的意思:
- The center pixel of th red block will stay 1, since it is 1 in the original image and has a neighboring 0.
- 第一个红色块的中心像素将保持为1,因为在原始图像中它是1,并且它的邻边为0。
- The center pixel of the blue block will stay 0, since it was 0in the original image.
- 蓝色块的中心像素将保持为0,因为在原始图像中它是0。
- The center pixel of the green block is the interesting case: It was 1 in the original image, but all its neighbors are 1 as well. Thus it becomes 0 in the result.
- 绿色方块的中心像素是一个有趣的例子:它在原始图像中是1,但是它所有的邻居都是1。结果是0。
Test these conditions for each array entry. Just be careful at the boundaries.
为每个数组条目测试这些条件。在边界上要小心。
If you have access to an image processing library there is another approach you can take: create a copy of your image, errode it by one pixel and subtract it from the original.
如果您可以访问图像处理库,那么您可以采用另一种方法:创建图像的副本,将其删除一个像素,并从原始图像中删除它。
更多相关文章
- 在Python中显示来自控制台的图像
- Python为8bit深度图像应用color map
- Tensorflow:恢复图形和模型,然后在单个图像上运行评估
- python的PIL绘制多行文本的图像。
- 铯:使用自己的OpenStreetMap服务器。:“未能获得图像块”错误
- 如何将图像加载到PictureBox;基于存储在DataBase中的图像位置
- 使用进度条延迟加载图像会使图像在显示时发生损坏
- Android 修改用户图像笔记
- 背景图像颜色检测与Android油漆。