Candace Adams
Candace Adams

Reputation: 61

Finding coordinates of brightest pixel in an image and entering them into an array

I have been asked to write a program to find 'stars' in an image by converting the image file to a numpy array and generating an array of the coordinates of the brightest pixels in the image above a specified threshold (representing background interference). Once I have located the brightest pixel in the image I must record its x,y coordinates, and set the value of that pixel and surrounding 10X10 pixel area to zero, effectively removing the star from the image. I already have a helper code which converts the image to an array, and have attempted to tackle the problem as follows;

I have defined a variable

    Max = array.max()

and used a while loop;

    while Max >= threshold
        coordinates = numpy.where(array == Max) # find the maximum value

however I want this to loop over the whole array for all of the coordinates,not just find the first maximum, and also remove each maximum when found and setting the surrounding 10X10 area to zero. I have thought about using a for loop to do this but am unsure how I should use it since I am new to Python.

I would appreciate any suggestions, Thanks

Upvotes: 3

Views: 10085

Answers (2)

Joe Kington
Joe Kington

Reputation: 284602

There are a number of different ways to do it with just numpy, etc.

There's the "brute force" way:

import Image
import numpy as np

im = Image.open('test.bmp')
data = np.array(im)
threshold = 200
window = 5 # This is the "half" window...
ni, nj = data.shape 
new_value = 0

for i, j in zip(*np.where(data > threshold)):
    istart, istop = max(0, i-window), min(ni, i+window+1)
    jstart, jstop = max(0, j-window), min(nj, j+window+1)
    data[istart:istop, jstart:jstop] = new_value

Or the faster approach...

import Image
import numpy as np
import scipy.ndimage

im = Image.open('test.bmp')
data = np.array(im)
threshold = 200
window = 10 # This is the "full" window...
new_value = 0

mask = data > threshold
mask = scipy.ndimage.uniform_filter(mask.astype(np.float), size=window)
mask = mask > 0
data[mask] = new_value

Upvotes: 3

Steven Rumbalski
Steven Rumbalski

Reputation: 45542

Astronomy.net will do this for you:

If you have astronomical imaging of the sky with celestial coordinates you do not know—or do not trust—then Astrometry.net is for you. Input an image and we'll give you back astrometric calibration meta-data, plus lists of known objects falling inside the field of view.

We have built this astrometric calibration service to create correct, standards-compliant astrometric meta-data for every useful astronomical image ever taken, past and future, in any state of archival disarray. We hope this will help organize, annotate and make searchable all the world's astronomical information.

You don't even have to upload the images to their website. You can download the source. It is licensed under the GPL and uses NumPy, so you can muck around with it if you need to.

Note that you will need to first convert your bitmap to one of the following: JPEG, GIF, PNG, or FITS image.

Upvotes: 2

Related Questions