ImageMagick按背景色分割图像

ImageMagick按背景色分割图像,imagemagick,Imagemagick,我有很高的形象 如何通过透明(或其他)背景将此图像分割为小图像 图像不在网格中,因此代码应该找到图像中每个图片的位置。如果运行此命令,它会将所有像素向上挤压成一个只有一个像素宽且具有图像原始高度的图像。然后,您将看到所有带有单词none的线条对应于所有位置,在这些位置上,您可以在图像上画一条水平线,而不会与任何电路板背景相交: convert boards.png -resize 1x! txt: | more # ImageMagick pixel enumeration: 1,4667,25

我有很高的形象

如何通过透明(或其他)背景将此图像分割为小图像


图像不在网格中,因此代码应该找到图像中每个图片的位置。如果运行此命令,它会将所有像素向上挤压成一个只有一个像素宽且具有图像原始高度的图像。然后,您将看到所有带有单词
none
的线条对应于所有位置,在这些位置上,您可以在图像上画一条水平线,而不会与任何电路板背景相交:

convert boards.png -resize 1x! txt: | more
# ImageMagick pixel enumeration: 1,4667,255,srgba
0,0: (0,0,0,0)  #00000000  none
0,1: (0,0,0,0)  #00000000  none
0,2: (0,0,0,0)  #00000000  none
...
...
...
0,547: (0,0,0,0)  #00000000  none
0,548: (0,0,0,0)  #00000000  none
0,549: (0,0,0,0)  #00000000  none
0,550: (0,0,0,0)  #00000000  none
0,551: (0,0,0,0)  #00000000  none
0,552: (0,0,0,0)  #00000000  none
0,553: (0,0,0,0)  #00000000  none
0,554: (0,0,0,0)  #00000000  none
0,555: (39038,36633,35446,0)  #988F8A00  srgba(152,143,138,0)
0,556: (38889,36248,34984,12)  #978D8800  srgba(151,141,136,0.000183108)
0,557: (38348,35253,33873,75)  #95898400  srgba(149,137,132,0.00114443)
0,558: (35061,31775,30664,98)  #887C7700  srgba(136,124,119,0.00149538)
0,559: (33894,30508,29428,164)  #84777301  srgba(132,119,115,0.00250248)
0,560: (34379,30968,29853,243)  #86787401  srgba(134,120,116,0.00370794)
现在你可以看到,从水平线中没有东西到有东西的第一个地方是在第555行。因此,基本上,您希望检测从
none
到某物的变化

这可以通过
awk
轻松实现,如下所示:

convert boards.png -resize 1x! txt: | awk 'inside && /none/{inside=0;print;next} !inside && ! /none/{inside=1;print}'
# ImageMagick pixel enumeration: 1,4667,255,srgba
0,0: (0,0,0,0)  #00000000  none
0,555: (39038,36633,35446,0)  #988F8A00  srgba(152,143,138,0)
0,911: (0,0,0,0)  #00000000  none
0,1082: (36701,34188,35001,0)  #8F858800  srgba(143,133,136,0)
0,1433: (0,0,0,0)  #00000000  none
0,1570: (33489,32153,32838,4)  #827D8000  srgba(130,125,128,6.10361e-05)
0,1937: (0,0,0,0)  #00000000  none
0,2135: (29884,28945,29339,4)  #74717200  srgba(116,113,114,6.10361e-05)
0,2486: (0,0,0,0)  #00000000  none
0,2621: (28668,27300,25241,5)  #706A6200  srgba(112,106,98,7.62951e-05)
0,2972: (0,0,0,0)  #00000000  none
0,3216: (35066,32529,28581,2)  #887F6F00  srgba(136,127,111,3.0518e-05)
0,3612: (0,0,0,0)  #00000000  none
convert boards.png -stroke red -draw "line 0,555 12000,555" -draw "line 0,911 12000,911" -draw "line 0,1082 12000,1082" result.jpg
convert boards.png -crop x911+0+0 row1.jpg
现在我们知道,我们可以在第555行、第911行、第1082行、第1433行等处横切图片

我将这样画前3条这样的线:

convert boards.png -resize 1x! txt: | awk 'inside && /none/{inside=0;print;next} !inside && ! /none/{inside=1;print}'
# ImageMagick pixel enumeration: 1,4667,255,srgba
0,0: (0,0,0,0)  #00000000  none
0,555: (39038,36633,35446,0)  #988F8A00  srgba(152,143,138,0)
0,911: (0,0,0,0)  #00000000  none
0,1082: (36701,34188,35001,0)  #8F858800  srgba(143,133,136,0)
0,1433: (0,0,0,0)  #00000000  none
0,1570: (33489,32153,32838,4)  #827D8000  srgba(130,125,128,6.10361e-05)
0,1937: (0,0,0,0)  #00000000  none
0,2135: (29884,28945,29339,4)  #74717200  srgba(116,113,114,6.10361e-05)
0,2486: (0,0,0,0)  #00000000  none
0,2621: (28668,27300,25241,5)  #706A6200  srgba(112,106,98,7.62951e-05)
0,2972: (0,0,0,0)  #00000000  none
0,3216: (35066,32529,28581,2)  #887F6F00  srgba(136,127,111,3.0518e-05)
0,3612: (0,0,0,0)  #00000000  none
convert boards.png -stroke red -draw "line 0,555 12000,555" -draw "line 0,911 12000,911" -draw "line 0,1082 12000,1082" result.jpg
convert boards.png -crop x911+0+0 row1.jpg

现在,沿着这些线切割-如下所示:

convert boards.png -resize 1x! txt: | awk 'inside && /none/{inside=0;print;next} !inside && ! /none/{inside=1;print}'
# ImageMagick pixel enumeration: 1,4667,255,srgba
0,0: (0,0,0,0)  #00000000  none
0,555: (39038,36633,35446,0)  #988F8A00  srgba(152,143,138,0)
0,911: (0,0,0,0)  #00000000  none
0,1082: (36701,34188,35001,0)  #8F858800  srgba(143,133,136,0)
0,1433: (0,0,0,0)  #00000000  none
0,1570: (33489,32153,32838,4)  #827D8000  srgba(130,125,128,6.10361e-05)
0,1937: (0,0,0,0)  #00000000  none
0,2135: (29884,28945,29339,4)  #74717200  srgba(116,113,114,6.10361e-05)
0,2486: (0,0,0,0)  #00000000  none
0,2621: (28668,27300,25241,5)  #706A6200  srgba(112,106,98,7.62951e-05)
0,2972: (0,0,0,0)  #00000000  none
0,3216: (35066,32529,28581,2)  #887F6F00  srgba(136,127,111,3.0518e-05)
0,3612: (0,0,0,0)  #00000000  none
convert boards.png -stroke red -draw "line 0,555 12000,555" -draw "line 0,911 12000,911" -draw "line 0,1082 12000,1082" result.jpg
convert boards.png -crop x911+0+0 row1.jpg


然后在垂直方向上应用完全相同的步骤,替换
-resize1X带有
-调整x1的大小

这个问题已经在这里被问到并得到了回答:@ErickG.Hagstrom实际上这个问题是不同的,更简单。这个问题图片中的元素不在一个规则的网格上-它们不在彼此的上下垂直对齐,所以答案要复杂得多-我想。谢谢,我晚上会试试