数字图像处理实验

Posted 数学小学霸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数字图像处理实验相关的知识,希望对你有一定的参考价值。

数图实验

2021-05-23

实验一 图像变换

实验目的

1:了解二维fourier变换的原理
2:掌握二维fourier变换的性质
3:掌握离散余弦变换的原理

实验内容

1.绘制一个二值,并将其傅里叶函数可视化**。**

close all;
clear;
f = zeros(40, 40);
f(10:30, 10:30) = 1;
subplot(1, 3, 1);
imshow(f);
F = fft2(f);
subplot(1, 3, 2);
imshow(F);
D = log(1 + abs(F));
subplot(1, 3, 3);
imshow(D);

image-20210523223147442

2.对给定的一副图像saturn2.tif进行傅里叶变换

load imdemos saturn2; % 系统的
subplot(1, 2, 1);
imshow(saturn2);
I = fftshift(fft2(saturn2));
subplot(1, 2, 2);
imshow(log(abs(I)), []), colormap(jet(64)), colorbar

image-20210523223430069

3.在图像text,tif中定位字母a

w = imread('text.png');
a = w(33:45, 88:98);
figure;
imshow(w);
figure;
imshow(a);
C = real(ifft2(fft2(w) .* fft2(rot90(a, 2), 256, 256)));
figure;
imshow(C, []);
max(C(:));
thresh = 60;
figure;
imshow(C > thresh);

image-20210523223636215

4.对一幅图进行离散余弦变换和反变换

RGB = imread('autumn.png');
figure(1), imshow(RGB);
I = rgb2gray(RGB);
figure(2), imshow(I);
J = dct2(I);
figure(3), imshow(log(abs(J)), []), colormap(jet(64)), colorbar;
J(abs(J) < 10) = 0;
K = idct2(J) / 255;
figure(4), imshow(K);

image-20210523224045070

思考题

1.编写M程序文件验证二维傅里叶变换的平移性,旋转性

clear all;
image1 = zeros(128, 128);
image1(30:93, 52:67) = 1;
subplot(2, 2, 1);
imshow(image1, []);
title('原始图像');
image2 = abs(fft2(image1));
subplot(2, 2, 2);
imshow(log10(1 + image2), []);
title('频谱');
image3 = image1;
i = size(image1, 1);
j = size(image2, 2);
for k = 1:i
    for r = 1:j
        image3(k, r) = (-1)^(k + r) .* image1(k, r);
    end
end
image4 = abs(fft2(image3));
subplot(2, 2, 3);
imshow(image3, [])
title('中心化后的图像');
subplot(2, 2, 4);
imshow(log10(1 + image4), []);
title('中心化后的频谱');
image5 = zeros(i, j);
i = round(i / 2)
j = round(j / 2)
for u = -i:i
    for v = -j:j
        m = cos(pi / 4) * u - sin(pi / 4) * v;
        n = sin(pi / 4) * u + cos(pi / 4) * v;
        m = round(m);
        n = round(n);
        if abs(m) < i && abs(n) < j
            image5(u + i + 1, v + j + 1) = image1(m + i, n + j);
        end
    end
end
figure
subplot(2, 1, 1);
imshow(image5, []);
image6 = abs(fft2(image5));
subplot(2, 1, 2);
imshow(log10(1 + image6), []);

image-20210523224214398

实验二 图像的空间变换增强

实验目的

1:掌握图像的直方图均衡化的原理与应用
2:掌握灰度变换的原理与应用
3:掌握伪彩色图像处理增强的方法

实验内容

1.MATLAB提供的直方图修正函数

I = imread('pout.tif');
subplot(221);
imshow(I);
subplot(222);
imhist(I);
J = histeq(I, 64);
subplot(223);
imshow(J);
subplot(224);
imhist(J);

image-20210523224546571

2.通过灰度变换调整图像对比度

f = imread('fly.jpg');
g1 = imadjust(f, [0 1], [1, 0], 1);
g2 = imadjust(f, [0.5 0.75], [0, 1], 1);
g3 = imadjust(f, [], [], 2);
subplot(221);
imshow(f);
subplot(222);
imshow(g1);
subplot(223);
imshow(g2);
subplot(224);
imshow(g3);

image-20210523224727787

思考题

1.对pout.tif的灰度图像,进行如图所示的分段线性变换处理

f = imread('pout.tif');

[row, col] = size(f);
g = zeros(row, col);

for x = 1:row
    for y = 1:col
        if (f(x, y) < 32)
            g(x, y) = f(x, y) * 2;
        elseif (f(x, y) > 192)
            g(x, y) = f(x, y) * 1.5 - 128;
        else
            g(x, y) = f(x, y) * 0.6 + 44.8;
        end
    end
end
subplot(1, 2, 1);
imshow(f);
subplot(1, 2, 2);
imshow(g, []);

image-20210523224824123

2.分别对图像cameraman.tif进行求反和对数变换处理

I = imread('cameraman.tif');
subplot(131);
imshow(I);
I = double(I);
[M, N] = size(I);
for i = 1:M
    for j = 1:N
        J(i, j) = 255 - I(i, j);
    end
end
subplot(132);
imshow(mat2gray(J));
for i = 1:M
    for j = 1:N
        H(i, j) = log(I(i, j) + 1);
    end
end
subplot(133);
imshow(mat2gray(H));

image-20210523224912469

实验三 图像的滤波增强处理

实验目的

1.了解空域增强的基本原理
2.掌握平滑滤波器和锐化滤波器的使用
3.掌握图像中值滤波增强的结果

实验内容

1.对一副图像进行不同大小的模板的均值滤波,并比较结果

I = imread('eight.tif');
J = imnoise(I, 'salt & pepper', 0.02);
subplot(2, 2, 1);
imshow(J);
title('原始图像');
K1 = medfilt2(J, [3 3]);
K2 = medfilt2(J, [5 5]);
K3 = medfilt2(J, [7 7]);
subplot(2, 2, 2);
imshow(K1);
title('3*3模板均值滤波');
subplot(2, 2, 3);
imshow(K2);
title('5*5模板均值滤波');
subplot(2, 2, 4);
imshow(K3);
title('7*7模板均值滤波');

image-20210523225022693

2.对一副图像实现不同模板的中值滤波,并比较结果

I = imread('eight.tif');
J = imnoise(I, 'salt & pepper', 0.02);
imshow(I);
title('原始图像');
subplot(2, 2, 1);
imshow(J);
title('噪声图像');
k1 = medfilt2(J, [3 3]);
k2 = medfilt2(J, [5 5]);
k3 = medfilt2(J, [7 7]);
subplot(2, 2, 2); imshow(k1);
title('3*3模板中值滤波');
subplot(2, 2, 3); imshow(k1);
title('5*5模板中值滤波');
subplot(2, 2, 4); imshow(k1);
title('7*7模板中值滤波');

3.使用filter2函数和fspecial函数来实现边缘锐化处理

f = imread('moon.tif');
w4 = fspecial('laplacian', 0);
w8 = [1 1 1; 1 -8 1; 1 1 1];
g4 = imfilter(f, w4, 'replicate');
g8 = imfilter(f, w8, 'replicate');
G4 = f - g4;
G8 = f - g8;
subplot(1, 3, 1); imshow(f);
subplot(1, 3, 2); imshow(G4);
subplot(1, 3, 3); imshow(G8);

image-20210523225233111

4.用sobel算子prewit算子log算子对图进行锐化处理

I = imread('cameraman.tif');
k1 = fspecial('sobel');
S = imfilter(I, k1, 'replicate');
k2 = fspecial('prewitt');
P = imfilter(I, k2, 'replicate');
k3 = fspecial('log');
L = imfilter(I, k3, 'replicate');
subplot(2, 2, 1); imshow(I); title('原图');
subplot(2, 2, 2); imshow(S); title('sobel算子');
subplot(2, 2, 3); imshow(P); title('prewitt算子');
subplot(2, 2, 4); imshow(L); title('log算子');

image-20210523225334442

5.对一副图像加入椒盐噪声后,实现butterworth低通滤波

I1 = imread('saturn.png');
subplot(2, 2, 1);
imshow(I1);
title('原始图像');
I2 = imnoise(I1, 'salt & pepper');
subplot(2, 2, 2);
imshow(I2);
title('噪声图像');
f = double(I2);
g = fft2(f);
g = fftshift(g);
[N1, N2] = size(g);
n = 2;
d0 = 50;
n1 = fix(N1 / 2);
n2 = fix(N2 / 2);
for i = 1:N1
    for j = 2:N2
        d = sqrt((i - n1)^2 + (j - n2)^2);
        h = 1 / (1 + 0.414 * (d / d0)^(2 * n)); %butterworth
        result1(i, j) = h * g(i, j);
        if (g(i, j) > 50)
            result2(i, j) = 0;
        else
            result2(i, j) = g(i, j);
        end
    end
end
result1 = ifftshift(result1);
result2 = ifftshift(result2);
X2 = ifft2(result1);
X3 = uint8(real(X2));
subplot(2, 2, 3);
imshow(X3); title('butterworth滤波图像');
X4 = ifft2(result2);

X5 = uint8(real(X4));
subplot(2, 2, 4);
imshow(uint8(X5)); title('理想低通滤波图像');

image-20210523225551409

6.对一副图像moon.tif实现butterworth高通滤波

I1 = imread('moon.tif'); f = double(I1);
subplot(1, 2, 1); imshow(I1); title('原始图像');
g = fftshift(fft2(f));
[N1, N2] = size(g);
n = 2;
d0 = 50;
n1 = fix(N1 / 2);
n2 = fix(N2 / 2);
for i = 1:N1
    for j = 2:N2
        d = sqrt((i - n1)^2 + (j - n2)^2);
        h = 1 - 1 / (1 + 0.414 * (d / d0)^(2 * n)); %butterworth
        result1(i, j) = h * g(i, j);
    end
end
result1 = ifftshift(result1);
X2 = ifft2(result1);
X3 = uint8(real(X2));
subplot(1, 2, 2);
imshow(X3); title('butterworth高通滤波图像');

image-20210523225633525

思考题

1.用合适的方法分别对图A所示的花粉图像和图B所示的电路板图像进行增强处理。

I = imread('pollen.tif');
subplot(1, 2, 1);
imshow(I);
title('原始图像');
J = histeq(I);
subplot(1, 2, 2); imshow(J);
title('用直方图均衡化增强后');

image-20210523225736432

clear
I = imread('board.tif');
J = imnoise(I, 'salt & pepper', 0.2);
subplot(1, 2, 1);
imshow(J);
title('原始图像');
k1 = medfilt2(J, [3 3]);
subplot(1, 2, 2); imshow(k1);
title('3*3模板中值滤波');

2.已知一同态滤波器的传输特性,该滤波器的传递函数,用该滤波器对图像进行处理

I = imread('eight.tif');
subplot(121);
imshow(I);
title('原始图像');
f = double(I);
g = fft2(f);
g = fftshift(g);
[N1 N2] = size(g);
d0 = 0.25;
c = 10;
n1 = fix(N1 / 2);
n2 = fix(N2 / 2);
for i = 1:N1
    for j = 2:N2
        d = sqrt((i - n1)^2 + (j - n2)^2);
        h = 1.75 * (1 - exp(-2 * (d^2 / d0^2))) + 0.25;
        result(i, j) = h * g(i, j);
    end
end
result = ifftshift(result);
X2 = ifft2(result);
X3 = uint8(real(X2));
subplot(122);
imshow(X3);
title('滤波后图像');

image-20210523230029744

实验四 图像复原处理技术

实验目的

1.了解图像降质退化的原因
2.理解反向滤波图像复原原理
3.理解维纳滤波图像复原原理

实验内容

1.产生一模糊图像,采用维纳滤波图像复原的方法对图像进行处理

d = 15;
h = zeros(2 * d + 1, 2 * d + 1);
h(d + 1, 1:2 * d + 1) = 1 / (2 * d);
f = imread('lena.tif');
[m, n] = size(f);
fe = zeros(m + 2 * d, n + 2 * d);
fe(1:m, 1:n) = f;
he = zeros(m + 2 * d, n + 2 * d);
he(1:2 * d + 1, 1:2 * d + 1) = h;
F = fft2(fe);
H = fft2(he);
ns = 5 * rand(m + 2 * d, n + 2 * d);
g = ifft2(F .* H) + ns;
G = fft2(g);
K = 0;
F_est = ((H.^2) ./ (H.^2 + K)) .* G ./ H;
f_est = real(ifft2(F_est));
imshow(f); title('原图');
figure;
imshow(g(d + 1:m + d, d + 1:n + d), [min(g(:)) max(g(:))]); title('模糊图');
figure;
imshow(f_est(1:m, 1:n), [min(f_est(:)) max(f_est(:))]);
title('复原图');

image-20210523230251418

2.逆滤波与维纳滤波的比较

F = checkerboard(8);
subplot(1, 5, 1); imshow(F, []); title('a');
PSF = fspecial('motion', 7, 45);
MF = imfilter(F, PSF, 'circular');
noise = imnoise(zeros(size(F)), 'gaussian', 0, 0.01);
MFN = MF + noise;
subplot(1, 5, 2); imshow(MFN, []); title('b');
NSR = sum(noise(:).^2) / sum(MFN(:).^2);
subplot(1, 5, 3);
imshow(deconvwnr(MFN, PSF), []); title('c');
subplot(1, 5, 4);
imshow(deconvwnr(MFN, PSF, NSR), []); title('d');
Sn = abs(fft2(noise)).^2;
Sf = abs(fft2(F)).^2;
NCORR = fftshift(real(ifft2(Sn)));
ICORR = fftshift(real(ifft2(Sf)));
subplot(1, 5, 5);
imshow(deconvwnr(MFN, PSF, NCORR, ICORR), []); title('e');

image-20210523230345142

思考题

设计一个二维线形运动滤波器PSF对图像文件进行运动模糊处理,然后分别用逆滤波与维纳滤波的方法进行复原

F = imread('cameraman.tif');
subplot(2, 2, 1); imshow(F, []); title('原图');
PSF = fspecial('motion', 6, 45);
MFN = imfilter(F, PSF);
subplot(2, 2, 2);
imshow(MFN, []); title('模糊图像');
subplot(2, 2, 3);
imshow(deconvwnr(MFN, PSF), []); title('逆滤波复原');
NSR = 0.1;
subplot(2, 2, 4);
imshow(deconvwnr(MFN, PSF, NSR), []); title('维纳滤波复原');

image-20210523230429865

实验五 图像分割

实验目的

1.掌握边缘提取的原理
2.掌握边缘提取理论的离散化实验过程
3.掌握几种常用边缘算子的提取方法

实验内容

1.调用sobel,prewitt,robert,laplacian,过零点方法和candy方法等边缘检测算子,提取一副灰度图像的边缘,并比较不同算子的检测结果

I = imread('moon.tif');
BW1 = edge(I, 'sobel');
BW2 = edge(I, 'prewitt');
% BW3 = edge(I, 'robert');
BW4 = edge(I, 'log');
BW5 = edge(I, 'zerocross');
BW6 = edge(I, 'canny');
subplot(2, 3, 1); imshow(BW1); title('sobel');
subplot(2, 3, 2); imshow(BW2); title('prewitt');
% subplot(2, 3, 3); imshow(BW3); title('robert');
subplot(2, 3, 4); imshow(BW4); title('log');
subplot(2, 3, 5); imshow(BW5); title('zerocross');
subplot(2, 3, 6); imshow(BW6); title('canny');

image-20210523230714088

2.用全局阈值法对图像进行分割处理

f = imread('lena.tif');
subplot(1, 2, 1); imshow(f); title('原图');
T = 0.5 * (double(min(f(:))) + double(max(f(:))));
done = 0;
while ~done
    g = f >= T;
    Tnext = 0.5 * (mean(f(g)) + mean(f(~g)));
    done = abs(T - Tnext) < 0.5;
    T = Tnext;
end
T = T / 256
f = im2bw(f, T);
subplot(1, 2, 2); imshow(f);
title('全局阈值法分割');

image-20210523230815224

3.用otsu法对图像进行分割处理

I = imread('lena.tif');
subplot(1, 2, 1); imshow(I); title('原图');
T = graythresh(I)
B = im2bw(I, T);
subplot(1, 2, 2); imshow(B);
title('Ostu法分割');

image-20210523230854530

思考题

1.统计图像灰度直方图,选取合适阈值对一副图像进行分割

I1 = imread('cameraman.tif');
subplot(221);
imshow(I1);
title('原始图像');
[m, n] = size(I1);
GP = zeros(1, 256);
for k = 0:255
    GP(k + 1) = length(find(I1 == k)) / (m * n);
end
subplot(222), bar(0:255, GP, 'g');
title('灰度直方图');
xlabel('灰度值');
ylabel('出现概率');
I2 = im2bw(I1, 100/255);
subplot(223), imshow(I2);
title('阈值100的分割图像');
I3 = im2bw(I1, 150/255);
subplot(224), imshow(I3);
title('阈值150的分割图像');

image-20210523231044693

2.编写区域生长分割图像的M程序

A = imread('cameraman.tif');
seed = [100, 220];
thresh = 15;
A = imadjust(A, [min(min(double(A))) / 255, max(max(double(A))) / 255], []);
A = double(A);
B = A;
[r, c] = size(B);
n = r * c;
pixel_seed = A(seed(1), seed(2));
q = [seed(1) seed(2)];
top = 1;
M = zeros(r, c);
M(seed(1), seed(2)) = 1;
count = 1;
while top ~= 0
    r1 = q(1, 1);
    c1 = q(1, 2);
    p = A(r1, c1);
    dge = 0;
    for i = -1:1
        for j = -1:1
            if r1 + i <= r & r1 + i > 0 & c1 + j <= c & c1 + j > 0
                if abs(A(r1 + i, c1 + j) - p) <= thresh & M(r1 + i, c1 + j) ~= 1
                    top = top + 1;
                    q(top, :) = [r1 + i c1 + j];
                    M(r1 + i, c1 + j) = 1;
                    count = count + 1;
                    B(r1 + i, c1 + j) = 1;
                end
                if M(r1 + i, c1 + j) == 0;
                    dge = 1;
                end
            else
                dge = 1;
            end
        end
    end
    if dge ~= 1
        B(r1, c1) = A(seed(1), seed(2));
    end
    if count >= n
        top = 1;
    end
    q = q(2:top, :);
    top = top - 1;
end
subplot(1, 2, 1), imshow(A, []); title('原始图像');
subplot(1, 2, 2), imshow(B, []); title('区域生长图像')

image-20210523231100624

以上是关于数字图像处理实验的主要内容,如果未能解决你的问题,请参考以下文章

数字信号处理实验集合

201555332盛照宗—网络对抗实验1—逆向与bof基础

数字图像处理实验

山东大学数字图像处理实验

20155201 李卓雯 《网络对抗技术》实验一 逆向及Bof基础

JavaScript 有用的代码片段和 trick