python计算机视觉编程(二)

图像轮廓和直方图

通过contour绘制图像轮廓以及list函数绘制图像直方图

from PIL import Image
from pylab import *

#读取图像到数组中
im = array(Image.open('D:\\11.jpg').convert('L'))

#新建一个图像
figure()
#不使用颜色信息
gray()
#在原点的左上角显示轮廓图像
contour(im,origin='image')
axis('equal')
axis('off')

#绘制图像轮廓和直方图
figure()
hist(im.flatten(),128)
show()

结果如下

直方图均衡化

直方图均衡化是指将一幅图像的灰度直方图变平,使变换后的图像中每个灰度值的分布概率相同,是图像处理领域中利用图像直方图对对比度进行调整的方法。

from PIL import Image
from numpy import *
from pylab import *
import cv2 as cv

def histeq(im,nbr_bins=256):
    """对一幅灰度图像进行直方图均衡化"""

    #计算图像的直方图
    imhist,bins = histogram(im.flatten(),nbr_bins)
    cdf = imhist.cumsum()#累积分布函数
    cdf = 255 * cdf / cdf[-1] #归一化
    #使用累计函数的线性插值,计算新的像素值
    im2 = interp(im.flatten(),bins[:-1],cdf)
    
    return im2.reshape(im.shape),cdf


im = array(Image.open('D:\\11.jpg').convert('L'))
figure()
subplot(2,3,1)
gray()
imshow(im)

subplot(2,3,4)
hist(im.flatten(),256)

im2,cdf = histeq(im)

subplot(2,3,3)
gray()
imshow(im2)

subplot(2,3,6)
hist(im2.flatten(),256)
show()

在这里插入图片描述

高斯滤波

高斯模糊是图像处理软件中广泛使用的处理效果,通常用它来减少图像噪声以及降低细节层次。这种模糊技术生成的图像,其视觉效果就像是经过一个半透明屏幕在观察图像。高斯模糊也用于计算机视觉算法中的预先处理阶段,以增强图像在不同比例大小下的图像效果。

from PIL import Image
from numpy import *
from pylab import *
from scipy.ndimage import filters

im = array(Image.open("D:\\11.jpg").convert("L"))
im2 = filters.gaussian_filter(im,5)

figure()
subplot(2,3,1)
gray()
imshow(im)

subplot(2,3,4)
gray()
imshow(im2)

img = array(Image.open("D:\\11.jpg"))
img2 = zeros(img.shape)
for i in range(3):
    img2[:,:,i] = filters.gaussian_filter(img[:,:,i],5)
img2 = uint8(img2)

subplot(2,3,3)
imshow(img)

subplot(2,3,6)
imshow(img2)
show()

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41409331/article/details/88135930