ImageMagick-修剪/裁剪到连续的对象 [英] ImageMagick - Trim / Crop to contiguous objects

查看:71
本文介绍了ImageMagick-修剪/裁剪到连续的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您如何在Photoshop中完成此步骤.

  convert shapes.png-颜色空间灰色-取反-阈值10%\-define connected-components:verbose = true \-connected-components 8 -normalize output.png 

示例输出

 对象(id:边界框形心区域均值颜色):0:416x310 + 0 + 0 212.3,145.2 76702 srgb(0,0,0)1:141x215 + 20 + 31 90.0,146.2 26129 srgb(255,255,255)2:141x215 + 241 + 75 311.0,190.2 26129 srgb(255,255,255) 

请注意如何标记" 或使用其自己独特的颜色(灰色阴影)来标识每个斑点或连续对象.

因此有一个标题行告诉您哪些字段后跟3个blob,即每行输出一个.第一行是整个图像,用处不大.第二个是从左上角的+ 20 + 31开始,宽141像素,高215像素.第三个是相同的大小(因为我复制了形状),并且从左上角开始为+ 241 + 75.

现在在最后指示的矩形周围加红色描边-请记住, rectangle 使用左上角和右下角,而不是左上角加上宽度和高度.

  convertshapes.png-描边红色-不填充-绘制矩形241,75 382,​​290" z.png 

并裁剪它:

  convertshapes.png-作物141x215 + 241 + 75 z.png 

这是提取的部分:


如果要泛化,只需将 ImageMagick 输出通过管道传输到 awk 并选择几何字段:

 <代码>转换shapes.png-彩色空间灰色-取反-阈值10%-定义连接组件:详细= true-连接组件8-规范化output.png |awk'NR> 2 {print $ 2}' 

示例输出

  141x215 + 20 + 31141x215 + 241 + 75 

How do you do the equivalent of this step in Photoshop.

https://gyazo.com/180a507c0f3c9b342fe33ce218cd512e

Supposed there are two contiguous objects in an image, and you want to create exact sized crops around each one and output as two files. (Generalize to N files)

解决方案

You can do that with "Connected Component Analysis" to find the contiguous blobs.

Start Image

convert shapes.png -colorspace gray -negate -threshold 10%  \
   -define connected-components:verbose=true                \
   -connected-components 8 -normalize output.png

Sample Output

Objects (id: bounding-box centroid area mean-color):
  0: 416x310+0+0 212.3,145.2 76702 srgb(0,0,0)
  1: 141x215+20+31 90.0,146.2 26129 srgb(255,255,255)
  2: 141x215+241+75 311.0,190.2 26129 srgb(255,255,255)

Notice how each blob, or contiguous object, is "labelled" or identified with its own unique colour (shade of grey).

So there is a header line telling you what the fields are followed by 3 blobs, i.e. one per line of output. The first line is the entire image and not much use. The second one is 141 px wide and 215 px tall starting at +20+31 from the top-left corner. The third one is the same size (because I copied the shape) and starts as +241+75 from the top-left corner.

Now stroke red around the final indicated rectangle - bearing in mind that rectangle takes top-left and bottom-right corners rather than top-left corner plus width and height.

convert shapes.png -stroke red -fill none -draw "rectangle 241,75 382,290" z.png

And crop it:

convert shapes.png -crop 141x215+241+75 z.png

And here is the extracted part:


If you want to generalise, you can just pipe the ImageMagick output into awk and pick out the geometry field:

convert shapes.png -colorspace gray -negate -threshold 10%  -define connected-components:verbose=true  -connected-components 8 -normalize output.png | awk 'NR>2{print $2}'

Sample Output

141x215+20+31
141x215+241+75

这篇关于ImageMagick-修剪/裁剪到连续的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆