python 图像宽度,正确的方法和Python包,可以找到图像功能的宽度

8IxWA.png

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 that band.

I am unfamiliar with image processing. Please direct me to the correct method of image processing and a Python image processing package that can do the same.

I am thinking PIL, OpenCV gave me an impression of being overkill for this particular application.

What if I want to make this an expert system that can classify them in the future?

解决方案

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 needed

def fitfunc(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 result

print "Mean values at: ", p1[1], p1[4]

# Plot the result

from pylab import *

subplot(211)

imshow(pic)

subplot(223)

plot(projection)

subplot(224)

plot(X,Y,'r',lw=5)

show()

tSeRt.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值