简体   繁体   English

如何选择图像中的特定水平像素线以在 Python 上进行分析?

[英]How to select a specific horizontal line of pixels in an image to analyze on Python?

I'm beginner on Python and started trying to program a code to analyze a spray image and plot a 'gray scale value' to see the spray pattern.我是 Python 初学者,开始尝试编写代码来分析喷涂图像并绘制“灰度值”以查看喷涂图案。

For a while I have this code:有一段时间我有这个代码:

from PIL import Image
import numpy as np
    
import matplotlib.pyplot as plt
    
filepath = "flat2.jpg"
          
img = Image.open(filepath).convert('L')
    
    
WIDTH, HEIGHT = img.size
pix = img.load()
    
data = np.asarray(img.getdata())
data = data.reshape((HEIGHT,WIDTH))
    
fig,ax = plt.subplots()
reduced_data = data.mean(axis=0)
    
ax.plot(reduced_data)
    
plt.show()

However, this code analyze the entire image and I need just a specific line, like the line 329 or something.然而,这段代码分析了整个图像,我只需要一个特定的行,比如 329 行或其他东西。 As a mitigation I tried crop the image too, but was unsucessfully.作为缓解措施,我也尝试裁剪图像,但未成功。

I'm trying to do a code like the tool "plot profile" on Image J.我正在尝试在 Image J 上执行类似工具“绘图配置文件”的代码。

Obsviously I just "made" this code with a help from some users here.显然,我只是在一些用户的帮助下“制作”了这段代码。

Flat fan spray image.扁平扇形喷雾图像。

The line and imageJ plot profile线和 imageJ 绘图配置文件

Since after several hours nobody supplied us with a sample of a correct efficient solution, here is the crop mitigation:由于几个小时后没有人向我们提供正确有效解决方案的样本,这里是作物缓解:

from PIL import Image

filepath = 'flat2.jpg'
img = Image.open(filepath).convert('L')

WIDTH, HEIGHT = img.size
LINE = 329         # <---- put here your desired line number

img = img.crop( (0, LINE, WIDTH, LINE+1 ) ).save('crop.png')
img = Image.open('crop.png')

# do stuff

# WIDTH, HEIGHT = img.size
# pix = img.load()
# etc

It should work but it's not exactly a correct and efficient solution at all.它应该可以工作,但它根本不是一个正确和有效的解决方案。 I believe it should be done via numpy etc.我相信它应该通过numpy等来完成。

As for the crop() function, it's a very simple thing.至于crop()函数,是很简单的事情。 It just takes some rectangular area (box) from a given image.它只需要从给定图像中提取一些矩形区域(框)。 The four numbers inside the brackets (x1, y1, x2, y2) are tuple of coordinates of this box.括号内的四个数字 (x1, y1, x2, y2) 是这个盒子的坐标元组。 Top-left corner (x1, y1) bottom-right corner (x2, y2).左上角 (x1, y1) 右下角 (x2, y2)。 The only possible trouble is if any of this coordinates fall outside of image size.唯一可能的问题是这些坐标中的任何一个是否超出图像大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM