Skip to content Skip to sidebar Skip to footer

Correct Method And Python Package That Can Find Width Of An Image's Feature

The input is a spectrum with colorful (sorry) vertical lines on a black background. Given the approximate x coordinate of that band (as marked by X), I want to find the width of t

Solution 1:

I'll give a complete minimal working example (as suggested by sega_sai). I don't have access to your original image, but you'll see it doesn't really matter! The peak distributions found by the code below are:

Mean values at: 26.2840960523 80.8255092125

import Image
from scipy import *
from scipy.optimize import leastsq

# Load the picture with PIL, process if needed
pic         = asarray(Image.open("band2.png"))

# Average the pixel values along vertical axis
pic_avg     = pic.mean(axis=2)
projection  = pic_avg.sum(axis=0)

# Set the min value to zero for a nice fit
projection /= projection.mean()
projection -= projection.min()

# Fit function, two gaussians, adjust as neededdeffitfunc(p,x):
    return p[0]*exp(-(x-p[1])**2/(2.0*p[2]**2)) + \
        p[3]*exp(-(x-p[4])**2/(2.0*p[5]**2))
errfunc = lambda p, x, y: fitfunc(p,x)-y

# Use scipy to fit, p0 is inital guess
p0 = array([0,20,1,0,75,10])
X  = xrange(len(projection))
p1, success = leastsq(errfunc, p0, args=(X,projection))
Y = fitfunc(p1,X)

# Output the resultprint"Mean values at: ", p1[1], p1[4]

# Plot the resultfrom pylab import *
subplot(211)
imshow(pic)
subplot(223)
plot(projection)
subplot(224)
plot(X,Y,'r',lw=5)
show()

enter image description here

Solution 2:

Below is a simple thresholding method to find the lines and their width, it should work quite reliably for any number of lines. The yellow and black image below was processed using this script, the red/black plot illustrates the found lines using parameters of threshold = 0.3, min_line_width = 5)

enter image description here

The script averages the rows of an image, and then determines the basic start and end positions of each line based on a threshold (which you can set between 0 and 1), and a minimum line width (in pixels). By using thresholding and minimum line width you can easily filter your input images to get the lines out of them. The first function find_lines returns all the lines in an image as a list of tuples containing the start, end, center, and width of each line. The second function find_closest_band_width is called with the specified x_position, and returns the width of the closest line to this position (assuming you want distance to centre for each line). As the lines are saturated (255 cut-off per channel), their cross-sections are not far from a uniform distribution, so I don't believe trying to fit any kind of distribution is really going to help too much, just unnecessarily complicates.

import Image, ImageStat

def find_lines(image_file, threshold, min_line_width):
    im = Image.open(image_file)
    width, height = im.size
    hist = []
    lines = []
    start=end=0for x in xrange(width):
        column= im.crop((x, 0, x +1, height))
        stat = ImageStat.Stat(column)
        ## normalises by2*255asin your example the colour is yellow
        ## if your images startusing white lines change this to3*255
        hist.append(sum(stat.sum) / (height *2*255)) 

    for index, valuein enumerate(hist):
        if value> threshold andend>=start:
            start= index
        if value< threshold andend<start:
            if index -start< min_line_width:
                start=0else:
                end= index
                center =start+ (end-start) /2.0
                width =end-start
                lines.append((start, end, center, width))
    return lines

def find_closest_band_width(x_position, lines):
    distances = [((value[2] - x_position) **2) forvaluein lines]
    index = distances.index(min(distances))
    return lines[index][3]

## set your threshold, and min_line_width for finding lines
lines = find_lines("8IxWA_sample.png", 0.7, 4)
## sets x_position to59th pixel
print 'width of nearest line:', find_closest_band_width(59, lines)

Solution 3:

I don't think that you need anything fancy for you particular task.

I would just use PIL + scipy. That should be enough.

Because you essentially need to take your image, make a 1D-projection of it and then fit a Gaussian or something like that to it. The information about the approximate location of the band should be used a first guess for the fitter.

Post a Comment for "Correct Method And Python Package That Can Find Width Of An Image's Feature"