ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

计算机视觉--使用pycharm进行基础图像处理

2020-02-22 20:08:04  阅读:761  来源: 互联网

标签:视觉 图像 直方图 图像处理 im import pycharm font axis


文章目录

前言

这是第一次使用python所以也是第一次安装与python相关的软件,前后装了3天时间,其中艰辛会写在另外一篇博客中。

1.绘制图像轮廓以及图像(灰度)直方图

1.1基本原理

绘制图像轮廓首先将图像灰度化,然后对每个坐标[x,y]像素值施加同一阈值
调用的方法(部分)
convert():将图像转为灰度图像
contour(Z) 创建一个包含矩阵Z的等值线的等高线图
axis(‘equal’) :表示x轴和y轴的单位长度相同
axis(‘off’) :关闭所有坐标轴线、刻度标记和标签
flatten():在绘制图像直方图之前对图像进行过压平处理
hist():绘制灰度图像直方图

1.2代码实现

# encoding:utf-8

from PIL import Image
from pylab import *
mpl.rcParams['font.sans-serif'] = ['SimHei']


im = array(Image.open('C:\Python\Pictrue\head.jpg').convert('L'))

figure()
subplot(121)
gray()
contour(im, origin='image')
axis('equal')
axis('off')
title(u'灰度图像')

subplot(122)
hist(im.flatten(), 128)#一维数组
title(u'图像直方图')
plt.xlim([0, 260])
plt.ylim([0, 11000])

show()

1.3结果展示

在这里插入图片描述

2.高斯滤波

2.1基本原理

图像模糊就是将(灰度)图像 I 和一个高斯核进行卷积操作。
高斯模糊通常是其他图像处理操作的一部分,在这里用来作为使用ROF模型去噪的操作步骤之一。
高斯滤波中sigma(高斯核的标准偏差)值越大,图像越模糊(见下图红框数字)
在这里插入图片描述
在高斯滤波中,多维滤波器被实现为一维卷积滤波器的序列。中间数组以与输出相同的数据类型存储。因此,对于精度有限的输出类型,结果可能不精确,因为中间结果可能存储的精度不足。
ROF模型的优点在于去除噪声的同时较好的保持了边缘,同时,ROF的峰值信噪比较高。

2.2代码实现

# -- coding: utf-8 --
from PIL import Image
from pylab import *
from numpy import *
from numpy import random
from scipy.ndimage import filters
from scipy.misc import imsave
from PCV.tools import rof

from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

im = array(Image.open('C:\Python\Pictrue\head.jpg').convert('L'))

U,T = rof.denoise(im,im)
G = filters.gaussian_filter(im,10) # save the result
#imsave(‘synth_original.pdf’,im)
#imsave(‘synth_rof.pdf’,U)
#imsave(‘synth_gaussian.pdf’,G) # plot
figure()
gray()

subplot(1,3,1)
imshow(im)
#axis(‘equal’)
axis('off')
title(u'原噪声图像', fontproperties=font)

subplot(1,3,2)
imshow(G)
#axis(‘equal’)
axis('off')
title(u'高斯模糊后的图像', fontproperties=font)

subplot(1,3,3)
imshow(U)
#axis(‘equal’)
axis('off')
title(u'ROF降噪后的图像', fontproperties=font)

show()

2.3结果展示

在这里插入图片描述

3.直方图均衡化

3.1基本原理

直方图均衡化是将一幅图像的灰度直方图扁平,使变换后的图像中的每个灰度值的分布概率相同。直方图均衡化可以增强图像的对比度。
histeq():用于增强灰度值动态范围偏小的图像的对比度。基本思想是把原始图像的灰度统计直方图变换成为均匀分布的形式,增加了像素灰度值的动态范围,从而达到增强图像整体对比度的效果。

3.2代码实现

在绘制matplotlib图的时候,出现了中文乱码的情况,在查找后发现需要添加以下两句语句

from matplotlib.font_manager import FontProperties  
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=12)

并在title()中添加以下语句

title(u'原始图像',fontproperties=font)

以下是完整代码段


```javascript
# encoding:utf-8
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\SimSun.ttc", size=14)

im = array(Image.open('C:\Python\Pictrue\head.jpg').convert('L'))
im2, cdf = imtools.histeq(im)

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

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

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

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

show()

3.3结果展示

在这里插入图片描述

lzydelyc 发布了11 篇原创文章 · 获赞 5 · 访问量 980 私信 关注

标签:视觉,图像,直方图,图像处理,im,import,pycharm,font,axis
来源: https://blog.csdn.net/lzydelyc/article/details/104400372

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有