寻找图像中最大值的坐标 [英] Finding the Coordinates of Maxima in an Image

查看:796
本文介绍了寻找图像中最大值的坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:
我刚开始使用Python的PIL进行照片处理,并且最近发现在现有程序中需要基本的照片处理功能。我的程序目前导入的图像(实际上是夜空的高分辨率),其中有很大比例的黑色空间(显然)和几个明亮的白色最大值。


问题:
在导入的图像中找到每个最大值的坐标(相对于画布坐标,如果可能)的最佳方法是什么?


我查看了PIL文档,并找到了获取某种颜色像素数的方法,但当然这不符合我的要求。正如我所说的,我是Python的PIL / Photo-manipulation的新手,所以对此的任何帮助都会很棒。

提前感谢! :)

Background:
I'm new to using Python's PIL for photo manipulation, and have very recently found the need for a basic photo processing function within an existing program. My program currently imports an image (effectively a high res shot of the night sky) in which there is a large proportion of black space (obviously) and several bright white maxima.

Question:
What is the best way of finding the coordinates (relative to the canvas coordinates if possible) of every maxima in the imported image?

I have looked through the PIL documentation, and have found ways to obtain the number of pixels of a certain colour, but of course this doesn't fulfil my requirements. As I say, I'm new to PIL/Photo-manipulation with Python, so any help on this would be fantastic.

Thanks in advance! :)

推荐答案

有一个 getextrema()方法返回每个频段使用的最低和最高图像数据。因此,为了找到最亮的像素,您需要首先获得图像的灰度副本。然后,您可以迭代图像中的每个像素,并检查每个像素是否具有最高灰度值:

There is a getextrema() method that returns the lowest and highest image data used for every band. So in order to find the brightest pixels, you need a grayscale copy of your image first. Then you can iterate over every pixel in the image and check each pixel whether it has the highest grayscale value:

grayscale = image.convert('L')
minima, maxima = grayscale.getextrema()

for width in image.size[0]:
   for height in image.size[1]:
       if grayscale.getpixel((width, height)) == maxima:
            # So here we have one pixel with the highest brightness.

然而,根据您实际尝试实现的目标,可能会有更简单,更有效的方法。例如,当您想要在黑色背景上粘贴所有最亮的像素时,您可以这样做:

However dependent on what you are actually try to achieve there might be simpler and more efficient ways of doing it. For example when you want to paste all the brightest pixels on a black background, you could do it like that:

grayscale = image.convert('L')
minima, maxima = grayscale.getextrema()
mask = image.point([0] * maxima + [255] * (256 - maxima))

new_image = PIL.Image.new('RGB', image.size)
new_image.paste(image, mask=mask)

这篇关于寻找图像中最大值的坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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