图像分割和图像恢复

  • 实验目的

1、熟悉并掌握MATLAB图像处理工具箱的使用;

2、理解并掌握常用的图像的恢复和分割技术。

二、实验环境

硬件:一般PC机

操作系统:WindowsXP

编程平台:MATLAB或高级语言

三、实验原理

介绍图像分割的基本方法、原理和命令调用格式,例如下。

1 deconvwnr

维纳滤波

用法:J = deconvwnr(I,PSF,NSR) 用维纳滤波算法对图片I进行图像恢复后返回图像J。I是一个N维数组。PSF是点扩展函数的卷积。NSP是加性噪声的噪声对信号的功率比。

如:

I = im2double(imread('cameraman.tif'));

imshow(I);

title('Original Image ');

%模拟运动模糊

LEN = 21;

THETA = 11;

PSF = fspecial('motion', LEN, THETA);

blurred = imfilter(I, PSF, 'conv', 'circular');

figure, imshow(blurred)

%恢复图像

wnr2 = deconvwnr(blurred_noisy, PSF);

figure, imshow(wnr2)

title('Restoration of Blurred')

2 edge

检测灰度或者二值图像的边缘,返回一个二值图像,1像素是检测到的边缘,0像素是非边缘。

用法:BW=edge(I,'sobel',thresh,direction),

I为检测对象;边缘检测算子可用sobel,roberts,prewitt,zerocross,log,canny;

thresh指定阈值,检测时忽略所有小于阈值的边缘,默认自动选择阈值:direction方向,在所指定的方向direction上,用算子进行边缘检测horizontal(水平方向)、vertical(垂直方向)或both(两个方向)。

如:I=imread('circuit.tif');

BW1 edge(I,'prewitt');

imshow(BW1);

产生一幅运动模糊图像,运用维纳滤波进行图像恢复,显示结果。

I = im2double(imread('E:\hg.jpg'));
grayImage=rgb2gray(I );
LEN = 21;
THETA = 11;
PSF = fspecial('motion',LEN,THETA);
blurred = imfilter(grayImage,PSF,'conv','circular');

subplot(1,2,1);imshow(blurred);
title('运动模糊图像')

wnr2 = deconvwnr(blurred,PSF);
subplot(1,2,2);imshow(wnr2);
title('恢复图像')

采用三种不同算子(prewitt、.zerocross、canny算子)检测图像边缘,显示结果。

srcImage=imread('E:\hg.jpg');
srcImageInfo=imfinfo('E:\hg.jpg');
 
%将图像转化为灰度图
grayImage=rgb2gray(srcImage);
 
%分别使用Roberts\Sobel\Prewitt\Canny算子进行边缘检测
imageRoberts=edge(grayImage,'roberts');
imageZerocross=edge(grayImage,'zerocross');
imagePrewitt=edge(grayImage,'prewitt');
imageCanny=edge(grayImage,'canny');
 
%显示检测图像
subplot(2,2,1);
imshow(grayImage);
title('原图像');

subplot(2,2,2);
imshow(imagePrewitt);
title('prewitt边缘图');
 
subplot(2,2,3);
imshow(imageZerocross);
title('zerocross边缘图');

subplot(2,2,4);
imshow(imageCanny);
title('canny边缘图');
  1. 梯度算子检测边缘:对一幅图像,采用梯度算子进行边缘检测。
    grayImage = imread('E:\photo\image3.jpg');
    I =rgb2gray(grayImage);
    threshold=graythresh(I);%计算将灰度图像转化为二值图像所需的门限
    BW=im2bw(I, threshold);%将灰度图像转化为二值图像
    % Roberts算子边缘检测
    RobertsEdges=edge(I,'roberts');
    % Sobel算子边缘检测
    SobelEdges=edge(I,'sobel');
    % Prewitt算子边缘检测
    PrewittEdges=edge(I,'prewitt');
    subplot(2,3,1);imshow(grayImage);title('原图像');
    grid on;
    axis on;
    subplot(2,3,2);imshow(BW);title('二值图像');
    grid on;
    axis on;
    subplot(2,3,3);imshow(RobertsEdges,[]);title('roberts算子分割结果');
    grid on;
    axis on;
    subplot(2,3,4);imshow(SobelEdges,[]);title('sobel算子分割结果');
    grid on;
    axis on;
    subplot(2,3,5);imshow(PrewittEdges,[]);title('prewitt算子分割结果');
    grid on;
    axis on;
  2. LOG算子检测边缘:对一幅图像,采用LOG算子进行边缘检测。
    % 读取原始图像
    originalImage = imread('E:\photo\image3.jpg');
     
    % 将图像转换为灰度图像
    grayImage = rgb2gray(originalImage);
     
    % 显示原始图像
    figure;
    subplot(2,2,1);
    imshow(originalImage);
    title('原始图像');
    % 显示二值图像
    subplot(2,2,2);
    imshow(grayImage);
    title('灰度图像');
     
    % 高斯滤波器平滑图像
    sigma = 1; % 高斯核的标准差,根据需要调整
    sizeSigma = ceil(6 * sigma); % 高斯核的大小
    gaussianKernel = fspecial('gaussian', sizeSigma, sigma);
    smoothedImage = imfilter(grayImage, gaussianKernel, 'replicate');
     
    % 应用拉普拉斯算子
    laplacianKernel = fspecial('laplacian');
    logImage = imfilter(smoothedImage, laplacianKernel, 'replicate');
     
    % 阈值化来得到二值边缘图像
    threshold = 0.05; % 阈值,根据需要调整
    binaryEdges = im2bw(logImage, threshold);
     
    % 显示LoG边缘检测结果
    subplot(2,2,3);
    imshow(binaryEdges);
    title('LoG算子分割结果');

  3. Canny算子检测边缘:对一幅图像,采用 Canny算子进行边缘检测。
    % 读取原始图像
    originalImage = imread('E:\photo\image3.jpg');
     
    % 将图像转换为灰度图像
    grayImage = rgb2gray(originalImage);
     
    % 创建一个图形窗口,用于显示图像
    figure;
     
    % 显示原始图像
    subplot(1, 3, 1); % 创建一个1行3列的子图布局,并定位在第1个子图
    imshow(originalImage);
    title('原始图像');
     
    % 显示灰度图像
    subplot(1, 3, 2); % 定位到第2个子图
    imshow(grayImage);
    title('灰度图像');
     
    % 使用Canny算子进行边缘检测
    % 应用高斯滤波器进行平滑
    % 指定高斯核的大小,例如,使用一个5x5的高斯核
    gaussianKernelSize = 5;
    gaussianKernel = fspecial('gaussian', [gaussianKernelSize, gaussianKernelSize]);
    smoothedImage = imfilter(grayImage, gaussianKernel, 'replicate');
     
    % 使用Canny算子检测边缘
    % 可以调整这两个阈值来控制边缘检测的灵敏度
    lowThreshold = 0.05;
    highThreshold = 0.15;
    cannyEdges = edge(smoothedImage, 'canny', lowThreshold, highThreshold);
     
    % 显示Canny算子的边缘检测结果
    subplot(1, 3, 3); % 定位到第3个子图
    imshow(cannyEdges);
    title('Canny算子分割结果');
     
    % 调整子图间距
    subplot(1, 3, 1);
    axis off;
    subplot(1, 3, 2);
    axis off;
    subplot(1, 3, 3);
    axis off;
  4. 边界跟踪(bwtraceboundary函数):对一幅图像,对其内部目标采用bwtraceboundary函数进行边界跟踪。
    clear all;
    clc;
    close all;
    I=imread('E:\photo\image3.jpg'); %读取当前路径下的图片
    I1=rgb2gray(I); %将彩色图像转化灰度图像
    threshold=graythresh(I1);%计算将灰度图像转化为二值图像所需的门限
    BW=im2bw(I1, threshold);%将灰度图像转化为二值图像
    % 显示原始图像、二值图像 
    figure;  
    subplot(1, 3, 1); imshow(I); title('原始图像');  
    subplot(1, 3, 2); imshow(BW); title('二值图像');  
    dim=size(BW);
    col=round(dim(2)/2)-90; %计算起始点列坐标
    row=find(BW(:,col),1);%计算起始点行坐标
    connectivity=8;
    num_points=180;
    contour=bwtraceboundary(BW,[row,col],'N',connectivity,num_points);
    %提取边界
    subplot(1,3,3);
    imshow(I1);
    hold on;
    plot(contour(:,2),contour(:,1), 'g','LineWidth' ,2);
    title('边界跟踪图像');
  5. 直方图阈值法:对一幅图像,采用直方图阈值法(利用直方图计算阈值)进行边缘检测。
    I=imread('E:\photo\image3.jpg');%读取当前路径下的图片
    I1=rgb2gray(I);%转换为灰度图像:使用rgb2gray函数将读取的RGB图像转换为灰度图像。
    figure;
    subplot(2,2,1);
    imshow(I1);
    title(' 灰度图像')
    grid on;
    axis on;
    %计算灰度图像的直方图
    [m,n]=size(I1);
    GK=zeros(1,256);
    for k=0:255
         GK(k+1)=length(find(I1==k))/(m*n);
    end
    subplot(2,2,2),bar(0:255,GK,'g')
    title('灰度直方图')
    xlabel('灰度值')
    ylabel(' 出现概率')
    %使用固定阈值进行图像二值化
    I2=im2bw(I,150/255);  
    subplot(2,2,3),imshow(I2);
    title('阈值150的分割图像')
    grid on;
    axis on;
    I3=im2bw(I,200/255);
    subplot(2,2,4),imshow(I3);
    title('阈值200的分割图像')
    grid on;
    axis on;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值