在Python中将图像划分为矩形信息 [英] Divide image into rectangles information in Python

查看:228
本文介绍了在Python中将图像划分为矩形信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列图像,无非就是黑色背景中的一系列彩色矩形 示例:.我如何(在Python 3.7中使用PIL)制作一个可以识别这些矩形(xy位置,颜色和大小)的程序?效率并不是很重要,因此嵌套for是可以接受的.到目前为止,我一直想出的每一种算法都有很多缺陷,并且可能以许多不同的方式出错,或者过于复杂而根本不值得

I have a series of images that are nothing more than a series of colored rectangles in a black background Example:. How could I (using PIL in Python 3.7) make a program that would identify these rectangles (xy position, color and size)? Efficiency is not really important, so nested fors are acceptable. Every algorithm I've been able to come up with until now have a lot of flaws and can go wrong in many different ways, or are just WAY too complex and not at all worth it

推荐答案

您根本不需要编写任何代码,可以使用安装在大多数Linux发行版和适用于macOS和Windows.

You don't need to write any code at all, you can do that with ImageMagick which is installed on most Linux distros and is available for macOS and Windows.

只需在终端(Windows上为命令提示符)中,即可运行:

Just in the Terminal (Command Prompt on Windows), you can run:

magick convert image.png                            \
   -define connected-components:verbose=true        \
   -define connected-components:area-threshold=100  \
   -connected-components 4 -auto-level output.png

示例输出

Objects (id: bounding-box centroid area mean-color):
  0: 1200x714+0+0 651.2,369.3 703177 srgb(0,0,0)
  164: 1200x86+0+714 599.5,756.5 103200 srgb(255,21,0)
  2: 363x155+80+60 261.0,137.0 56265 srgb(255,255,255)
  26: 127x323+60+302 122.6,463.2 39668 srgb(255,255,255)
  54: 308x109+352+373 505.5,427.0 33572 srgb(255,255,255)
  1: 102x159+641+47 691.5,126.0 16218 srgb(255,255,255)
  53: 79x100+977+371 1016.0,420.5 7900 srgb(0,17,255)

因此,查看以0:开头的行,有一个尺寸为1200x714的矩形,从0,0(左上角)开始,颜色为黑色,即srgb(0,0,0).

So, looking at the line starting 0: there is a rectangle measuring 1200x714 starting at 0,0 (top-left corner) with colour black, i.e. srgb(0,0,0).

从下一行看,有一个矩形,尺寸为1200x86,从左上角开始向下714个像素,颜色为红色,即srgb(255,21,0).

Looking at the next line, there is a rectangle measuring 1200x86 starting 714 pixels down from the top-left corner with colour red, i.e. srgb(255,21,0).

以此类推.

最后一行是一个矩形79x100,位于[977,31],颜色为蓝色,即srgb(0,17,255).

The last line is a rectangle 79x100 positioned at [977,31] with colour blue, i.e. srgb(0,17,255).

这篇关于在Python中将图像划分为矩形信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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