【问题标题】:export coordinate from an image to excel format in python在python中将坐标从图像导出为ex​​cel格式
【发布时间】:2021-02-24 07:20:57
【问题描述】:

我有 this 已转换为二进制的图像,我需要的只是所有白色像素的坐标数据 (xy),但是是 Excel 格式(.xlsx 或 .xls),如 this,我该怎么做明白了吗?

以前,我在 Matlab 中尝试过使用bw.boundaries 命令,但我得到的只是边缘的坐标(不是整个区域)。现在我想用python来做,有没有办法做到这一点? 提前谢谢你。

【问题讨论】:

  • 首先,不要将双层图像保存为 JPEG。您的 JPEG 有 240 种灰度,因为 JPEG 是有损的!其次,请更准确地说明您想要什么 - 您想要 CSV 吗?前几行看起来如何?请举个例子。那么“你想要白色区域的坐标数据”是什么意思 - 你的意思是你想要每个白色像素的x,y坐标吗?
  • 顺便说一句,你不需要写任何Python。您可以在终端中使用 ImageMagick 并使用 magick IMAGE.JPG -threshold 50% txt: | awk -F: '/white/{print $1}' > data.csv
  • 很抱歉缺少信息,我已经编辑了问题并更改了图片。
  • 哦,我以前从未使用过 ImageMagick,但我会尝试,谢谢!我试了会通知你..

标签: python-3.x numpy image-processing coordinates


【解决方案1】:
import imageio
from skimage.color import rgb2gray

im = rgb2gray(imageio.imread("img.jpg"))

np.savetxt('img.csv', np.argwhere(img > 0.5), fmt='%d', delimiter=',')

或者如果您的图像存储为uint8

np.savetxt('img.csv', np.argwhere(img > 127), fmt='%d', delimiter=',')

或者如果您的图像存储为二进制数组

np.savetxt('img.csv', np.argwhere(img != 0), fmt='%d', delimiter=',')

编辑: OP 的一个小例子,希望能更好地理解它是如何工作的

import numpy as np
import imageio

# let's load a sample image
cat = imageio.imread('imageio:chelsea.png')   

# let's turn it into b/w by thresholding the read channel
cat_bw = cat[..., 0] > 127

# now we can get the white pixel coordinates
white_coords = np.argwhere(cat_bw)

# you could save them to csv with
np.savetxt('img.csv', white_coords, fmt='%d', delimiter=',')

你会得到类似的东西

$ cat img.csv
0,0
0,1
0,2
0,3
0,4
[...]
299,445
299,446
299,447
299,448
299,449
299,450

不确定您将如何在 Excel 中处理它们,这超出了我的专业知识范围,但假设您想确保坐标是我们想要的。

让我们绘制黑白切尔西 (cat_bw):

现在让我们尝试将原始图像中的坐标转换为另一种颜色,让我们以一种超级愚蠢而不是 numpythonic 的方式将其设为粉红色:

for x, y in white_coords: 
    cat[x, y] = [219, 49, 190]

并绘制它:

所以,在我看来,white_coords 数组和我们保存它们的 csv 文件确实包含我们的白色像素坐标。

【讨论】:

  • 我试过你的代码,但它没有用,坐标形成的盒子不像图像:(
  • @GrisheldaAthallah 你尝试了什么?你怎么能说它是一个盒子?请分享有关您的测试的更多信息以及如何重现它们
  • 我已经使用了您发送的代码。得到csv格式的坐标数据后,我将其转换为xlsx并在excel中制作图表。但是绘图结果形成了一个框(看起来代码只读取了外面的白框,而不是框内的白色像素区域)
  • 我想要的结果是图像中每个白色像素的坐标,这样我就可以使用 Excel 绘制它并产生相同的形状。对不起,如果我一开始让你困惑​​了
猜你喜欢
  • 2016-03-09
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-14
  • 1970-01-01
相关资源
最近更新 更多