计算机视觉 ch1基本的图像操作和处理

环境准备

Opencv、PIL、Matplotlib、PCV 、SciPy
下载教程(PIL、PCV、Matplotlib等库的安装
环境配置 python+PyCharm+OpenCV配置

1.灰度图

1)使用convert()方法实现图像的颜色转换

from PIL import Image
from pylab import *
#提供中文字体
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\msyh.ttc", size=14)

figure()
pil_im = Image.open('0.jpg')
subplot(121)
title('原图',fontproperties=font)
axis('off')
imshow(pil_im)

pil_im = Image.open('0.jpg').convert('L')
#不使用颜色信息
gray() 
subplot(122)
title('灰度图',fontproperties=font)
axis('off')
imshow(pil_im)
show()

2) cvtColor(input_image,type)函数
灰度图的转换使用 cv2.COLOR_BGR2GRAY

import cv2
img1=cv2.imread('0.jpg',1)
img2=cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
cv2.imshow('gary',img2)
cv2.waitKey(0)

3)使用imread()函数对图像进行灰度处理
将第二个参数设置为’0’即可实现灰度变换

import cv2
img = cv2.imread('0.jpg',0)
cv2.imshow('gary',img)
cv2.waitKey(0)

2.图像轮廓和直方图

因为绘制轮廓需要对每个坐标[x,y]的像素值设置一个阈值,因此要先将图像灰度化。
图像的直方图是用来表示该图像像素值的分布情况,用一定数目的小区间来指定表征像素值的范围。使用Matplotlib中的hist()函数绘制,其中第二个参数指定小区间的数目。
因为hist()只能接受一维数组作为其输入,因此在绘制之前需要对图像进行压平处理。flatten()方法可以将任意数组按照行优先准则转换成一维数组。

from PIL import Image
from pylab import *
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\msyh.ttc", size=14)

img = array(Image.open('0.jpg').convert('L'))
figure()
subplot(121)
gray()
contour(img,origin='image')
axis('equal')
axis('off')
title('图像轮廓',fontproperties=font)
subplot(122)
hist(img.flatten(),128)
title('直方图',fontproperties=font)
show()

3.直方图均衡化

直方图均衡化是指将一幅图像的灰度直方图变平,使变换后的图像中每个灰度值的分布概率都相同。

from PIL import Image
from pylab import *
from PCV.tools import imtools
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\msyh.ttc", size=14)

img = array(Image.open('0.jpg').convert('L'))
img2, cdf = imtools.histeq(img)

figure()
subplot(2, 2, 1)
axis('off')
gray()
title(u'原始图像', fontproperties=font)
imshow(img)

subplot(2, 2, 2)
axis('off')
title(u'直方图均衡化后的图像', fontproperties=font)
imshow(img2)

subplot(2, 2, 3)
axis('off')
title(u'原始直方图', fontproperties=font)
hist(img.flatten(), 128, normed=True)

subplot(2, 2, 4)
axis('off')
title(u'均衡化后的直方图', fontproperties=font)
hist(img2.flatten(), 128, normed=True)
show()

4.图像模糊

图像高斯模糊本质上就是将灰度图像和一个高斯核进行卷积操作

from PIL import Image
from pylab import *
from scipy.ndimage import filters
from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\msyh.ttc", size=14)

img = array(Image.open('0.jpg').convert('L'))
gray()
figure('1')
subplot(221)
title(u'原图', fontproperties=font)
axis('off')
imshow(img)

for i,j in enumerate([2,5,10]):
    img2 = filters.gaussian_filter(img,j)
    subplot(2,2,2+i)
    imgNum = str(j)
    title(u'α='+imgNum, fontproperties=font)
    axis('off')
    imshow(img2)
show()

#如果是模糊一幅彩色图片,对每一个颜色通道进行高斯模糊;
figure('2')
im = array(Image.open('0.jpg'))
im2 = zeros(im.shape)
for i in range(3):
    im2[:,:,i] = filters.gaussian_filter(im[:,:,i],5)
im2 =uint8(im2)
axis('off')
imshow(im2)
show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值