zoukankan      html  css  js  c++  java
  • 【图像处理与识别实验】—— 高斯模糊处理、图像形态处理、形态学操作处理阈值化图像

     

    主要方法:

    高斯模糊处理、图像形态处理、形态学操作处理阈值化图像。

    图像处理与识别 实验内容:

    (1) 如图1-9 所示,将一幅图像进行高斯模糊处理。随着σ 的增加,绘制出图像轮廓。在绘制出的图中,图像的轮廓有何变化?

    原图如下:

    生成图像:

     

    代码:

    # ch01_fig1-9~.py
    # -*- coding: utf-8 -*-
    from PIL import Image
    from pylab import *
    from scipy.ndimage import filters
    
    # 添加中文字体支持
    from matplotlib.font_manager import FontProperties
    font = FontProperties(fname=r"c:windowsfontsSimSun.ttc", size=14)
    
    #im = array(Image.open('board.jpeg'))
    im = array(Image.open('empire.jpg').convert('L'))
    figure()
    gray()
    axis('off')
    subplot(1, 4, 1)
    axis('off')
    title(u'原图', fontproperties=font)
    imshow(im)
    
    # for bi, blur in enumerate([2, 5, 10]):
    # im2 = zeros(im.shape)
    # im2 = filters.gaussian_filter(im, blur)
    # im2 = np.uint8(im2)
    # imNum=str(blur)
    # subplot(1, 4, 2 + bi)
    # axis('off')
    # title(u'标准差为'+imNum, fontproperties=font)
    # imshow(im2)
    # 如果是彩色图像,则分别对三个通道进行模糊
    
     
    
    
    for bi, blur in enumerate([2, 5, 10]):
     im2 = zeros(im.shape)
     for i in range(3):
       im2[:, :, i] = filters.gaussian_filter(im[:, :, i], blur)
     im2 = np.uint8(im2)
     subplot(1, 4,  2 + bi)
     axis('off')
     imshow(im2)
    
    show()

    (2) 使用图像梯度,编写一个在图像中获得简单物体(例如,白色背景中的矩形、圆形、菱形),用边界框(bounding box)标出物体。

    原图如下:

     

    生成图像:

     

    代码:

    from PIL import Image
    from numpy import *
    from scipy.ndimage import filters
    import matplotlib.pyplot as plt
    
    im = array(Image.open('4.png').convert('L'), 'f')
    # Sobel 导数滤波器
    imx = zeros(im.shape)
    filters.sobel(im, 1, imx)
    imy = zeros(im.shape)
    filters.sobel(im, 0, imy)
    magnitude = sqrt(imx ** 3 + imy ** 3)
    
    plt.figure("imx")
    plt.imshow(imx, cmap='gray')
    
    plt.figure("imy")
    plt.imshow(imy, cmap='gray')
    
    plt.figure("magnitude")
    plt.imshow(magnitude, cmap='gray')
    
    plt.figure("imximx")
    plt.contour(imx, origin='image')
    plt.figure("imyimy")
    plt.contour(imy, origin='image')
    plt.figure("magnitudemagnitude")
    plt.contour(magnitude, origin='image')
    plt.show()

    (3) 使用形态学操作处理阈值化图像,使用morphology 模块里面的center_of_mass() 函数寻找每个物体的中心坐标,将其在图像中绘制出来。

    原图如下:

     

    生成图像:

     

    代码如下:

    from PIL import Image
    from numpy import *
    from pylab import *
    from scipy.ndimage import measurements,morphology,label
    
    im=array(Image.open('empire.png').convert('L'))
    im=1*(im<128)
    
    #载入图像,然后使用阈值化操作,以保证处理的图像为二值图像
    im_open=morphology.binary_opening(im,ones((5,5)),iterations=2)
    
    labels_open,nbr_objects_open=measurements.label(im_open)
    
    #求出每个物体中心点坐标
    a=measurements.center_of_mass(im_open,labels_open,[i+1 for i in range(nbr_objects_open)])
    
    figure()
    gray()
    imshow(im_open)
    
    #在图像中把中心点绘制出来
    plot([p[1] for p in a],[p[0] for p in a],'r*')
    
    show()

     

  • 相关阅读:
    Sublime Text 3插件收集
    Jenkins连接git时出现“Failed to connect to repository : Command ... HEAD" returned status code 128:”的问题解决
    Jenkins错误“to depth infinity with ignoreexternals:true”问题解决
    jeesite导入数据库错误:java.sql.SQLException: Incorrect string value: 'xE4xB8xADxE5x9BxBD' for column 'name' at row 1问题解决
    Maven出现错误No plugin found for prefix 'jetty' in the current project and in the plugin groups的问题解决
    Mac下关闭Sublime Text 3的更新检查
    Ueditor 专题
    Navicat PatchNavicat
    DataSourceBuilder.create().build()
    常用的HTML富文本编译器UEditor、CKEditor、TinyMCE、HTMLArea、eWebEditor、KindEditor简介
  • 原文地址:https://www.cnblogs.com/CoffeeSoul/p/13026325.html
Copyright © 2011-2022 走看看