您的位置:首页 > 编程语言 > Python开发

实现图像轮廓和图像直方图(Python)

2013-12-13 16:21 309 查看
Let's look at example of special plots: image contours and image histograms.Visualizing image ISO-contours of other 2D functions can be very useful.This needs gray scale images, because the contours need to be taken on a single value for every coordinate[x,y]. here's how to do it.
from PIL import Image
from pylab import *

im = array(Image.open('test.jpg').convert('L')) # read image to arry
figure() # create a new figure
gray() # don't use colors
#show contours with orgin upper left corner
contour(im,origin ='image')
axis('equal')
axis('off')

figure()
hist(im.flatten(),128)
show()[align=center]As before,the PIL method convert() does convection to gray-scale.An image histogram is a plot showing the distribution of pixel values.A number of bins is specified for the span of values and each bin gets a count of how many pixels have values in the bin's range.The visualization of the image histogram is done using the hist() function.The second argument specifies the number of bins to use.
Noe that the image needs to be flattened first,because hist() takes a 1-D array as input.The method flatten() converts any array to a 1-D array with values taken row-wise.figure shows the contour and histogram plot.
[/align]




[align=left]==========================================================================
转载请注明出处:http://blog.csdn.net/songzitea/article/details/17303547
==========================================================================[/align]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息