如何将黑白图像转换为坐标? [英] How to convert a black and white image into coordinates?

查看:49
本文介绍了如何将黑白图像转换为坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设计一个垂直绘图仪,它必须是无线的.

I'm trying to design a vertical plotter and it has to be wireless.

我的主要想法是拍摄一个简单的黑白图像并将其转换为坐标.然后这些可以通过我将编写的程序来控制绘图仪.

My main idea was to take a simple black and white image and convert it into coordinates. These could then be passed through a program which I will write to control the plotter.

但是,我似乎找不到任何可以转换的程序或网站.我希望的是一个程序或网站,它可以拍摄图像并将每个黑色部分转换为一系列或点,这些点将作为坐标(左下角为 (0,0)).

However, I can't seem to find any program or site which can convert. What I'm hoping for is a program or site which can take the image and convert every black part into a series or points which would be taken as a coordinates (with the bottom left corner as (0,0)).

如果有什么事情请告诉我.如果您对如何做到这一点有任何其他想法,请向我提出建议,我会在要求进一步澄清之前尝试自己解决.谢谢.

If there is anything out there please let me know. If you have any other idea of how to do this please suggest them to me and I'll try to work it out myself before asking for further clarification. Thank you.

推荐答案

我建议使用 ImageMagick,它包含在大多数 Linux 发行版中,可用于 macOS 和 Windows.

I would suggest ImageMagick which is included in most Linux distros and is available for macOS and Windows.

让我们从这张 6x2 的图像开始——我已经放大了它并在边缘周围画了一个红色的笔划,这样你就可以在 StackOverflow 的白色背景上看到它和它的范围:

Let's start with this image, which is 6x2 - I have enlarged it and put a red stroke around the edge so you can see it and its extent on StackOverflow's white background:

所以,不管是 JPEG、BMP、TIFF、PNG、GIF,ImageMagick 都可以读取所有内容.如果我将其阈值设置为 50% 以确保它仅由纯黑白组成并使输出为 8 位和文本,则您可以轻松看到黑色像素及其坐标(在第一列中):

So, it doesn't matter if it is JPEG, BMP, TIFF, PNG, GIF as ImageMagick can read everything. If I threshold it at 50% to make sure it consists only of pure black and white and make the output 8-bit and text, you can easily see the black pixels and their coordinates (in the first column):

convert image.png -threshold 50% -depth 8 txt:

样本输出

# ImageMagick pixel enumeration: 6,2,65535,srgb
0,0: (0,0,0)  #000000  black
1,0: (65535,65535,65535)  #FFFFFF  white
2,0: (65535,65535,65535)  #FFFFFF  white
3,0: (0,0,0)  #000000  black
4,0: (65535,65535,65535)  #FFFFFF  white
5,0: (65535,65535,65535)  #FFFFFF  white
0,1: (65535,65535,65535)  #FFFFFF  white
1,1: (0,0,0)  #000000  black
2,1: (0,0,0)  #000000  black
3,1: (65535,65535,65535)  #FFFFFF  white
4,1: (65535,65535,65535)  #FFFFFF  white
5,1: (0,0,0)  #000000  black

因此,为了更轻松地查看黑色像素,请去掉所有其他像素:

So, for an easy way to see just the black pixels, strip everything else out:

convert start.png -threshold 50% -depth 8 txt: | grep black

样本输出

0,0: (0,0,0)  #000000  black
3,0: (0,0,0)  #000000  black
1,1: (0,0,0)  #000000  black
2,1: (0,0,0)  #000000  black
5,1: (0,0,0)  #000000  black

您现在可能会看到 0,0 坐标位于左上角,而不是您要求的左下角,所以让我们先翻转图像:

You can now probably see that the 0,0 coordinate is top-left, rather than the bottom-left you asked for, so let's flip the image first:

convert start.png -threshold 50% -depth 8 -flip txt: | grep black

样本输出

1,0: (0,0,0)  #000000  black
2,0: (0,0,0)  #000000  black
5,0: (0,0,0)  #000000  black
0,1: (0,0,0)  #000000  black
3,1: (0,0,0)  #000000  black

因此,如果您只想要坐标,请使用 awk 进行过滤和整理:

So, if you just want the coordinates, filter and tidy up with awk:

convert start.png -threshold 50% -depth 8 -flip txt: | awk -F: '/black/{print $1}'

样本输出

1,0
2,0
5,0
0,1
3,1

这篇关于如何将黑白图像转换为坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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