首页 > 代码库 > 《数字图像处理原理与实践(MATLAB版)》一书之代码Part4

《数字图像处理原理与实践(MATLAB版)》一书之代码Part4

本文系《数字图像处理原理与实践(MATLAB版)》一书之代码系列的Part4,辑录该书第135至第183页之代码,供有需要读者下载研究使用。代码执行结果请参见原书配图,建议下载代码前阅读下文:

关于《数字图像处理原理与实践(MATLAB版)》一书代码发布的说明

http://blog.csdn.net/baimafujinji/article/details/40987807

P139

original = imread(‘snowflakes.png‘);
figure, imshow(original);
se = strel(‘disk‘,5);
afterOpening = imopen(original,se);
figure, imshow(afterOpening,[]);

P140

originalBW = imread(‘circles.png‘);
imshow(originalBW);
se = strel(‘disk‘,10);
closeBW = imclose(originalBW,se);
figure, imshow(closeBW)

P144

bw = imread(‘bw.bmp‘);
shape1 = [0 0 0 0 1
            0 0 0 1 1
            0 0 1 1 1
            0 1 1 1 1
            1 1 1 1 1];
shape2 = [1 1 0 0 0
            1 0 0 0 0
            0 0 0 0 0
            0 0 0 0 0
            0 0 0 0 0];
bw2 = bwhitmiss(bw, shape1, shape2);
imshow(bw2)

P146-1

I = imread(‘letter2.jpg‘);
I = im2bw(I);
I1 = bwmorph(I, ‘thin‘,inf);
figure(1), imshow(I1);

P146-2

I = imread(‘letter2.jpg‘);
I = im2bw(I);
I2 = bwmorph(I, ‘skel‘,inf);
figure(2), imshow(I2);

P153

I = imread(‘lena.jpg‘);
I = rgb2gray(I);
BW1 = edge(I, ‘roberts‘);
BW2 = edge(I, ‘sobel‘);
BW3 = edge(I, ‘prewitt‘);
figure
subplot(2,2,1),imshow(I),title(‘original‘)
subplot(2,2,2),imshow(BW1),title(‘roberts‘)
subplot(2,2,3),imshow(BW2),title(‘sobel‘)
subplot(2,2,4),imshow(BW3),title(‘prewitt‘)

P157

I = imread(‘einstein.bmp‘);
I = rgb2gray(I);
N = [1, 2, 1
     0, 0, 0
     -1,-2,-1];
edge_n = imfilter(I,N,‘symmetric‘,‘conv‘);
imwrite(edge_n, ‘edge_n.jpg‘);

P160

I = rgb2gray(imread(‘lena.jpg‘));
M = [1,1,1
        1,-8,1
        1,1,1];
img=imfilter(I,M);
[x,y]=size(I);
img2 = img;
for i = 2:x-1
        for j = 2:y-1
            a = [img(i,j+1),img(i,j-1),img(i+1,j+1),img(i+1,j-1), ...
                 img(i-1,j+1),img(i-1,j-1),img(i+1,j),img(i-1,j)];
            if ( (max(a)-min(a))>64 && max(a)>img(i,j) && min(a)<img(i,j))
                img2(i,j)=255;
            else
                img2(i,j)=0;
            end
        end
end

P165

I = imread(‘lena.jpg‘);
IMG = rgb2gray(I);
Edge_LoG = edge(IMG, ‘log‘);
imshow(Edge_LoG);
figure
subplot(1,2,1), imshow(IMG);
subplot(1,2,2), imshow(Edge_LoG);

P167

I = double(rgb2gray(imread(‘lena.jpg‘)));
figure, imshow(uint8(I))
DoG=fspecial(‘gaussian‘,5,0.8)-fspecial(‘gaussian‘,5,0.6);
ImageDoG=imfilter(I,DoG,‘symmetric‘,‘conv‘);
figure, imshow(ImageDoG)
% threshold = 2
proc_Img1 = ImageDoG;
proc_Img1(find(proc_Img1 < 2))=0;
figure, imshow(proc_Img1)
% threshold = 3
proc_Img2 = ImageDoG;
proc_Img2(find(proc_Img2 < 3))=0;
figure, imshow(proc_Img2)

P172

img = edge(I, ‘canny‘,[0.032,0.08], 3);

P183

RGB= imread(‘building.jpg‘);
I = rgb2gray(RGB);
BW = edge(I, ‘canny‘);

[H, T, R]=hough(BW, ‘RhoResolution‘,0.5,‘ThetaResolution‘,0.5);
figure, imshow(imadjust(mat2gray(H)), ‘XData‘, T, ...
‘YData‘, R, ‘InitialMagnification‘, ‘fit‘);
xlabel(‘\theta‘), ylabel(‘\rho‘);
axis on; axis normal; hold on;
colormap(hot);
peaks = houghpeaks(H, 15);
figure, imshow(BW);
hold on;
lines = houghlines(BW, T, R, peaks, ‘FillGap‘,25, ‘MinLength‘,15);

max_len = 0;
for k=1:length(lines)
xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),‘LineWidth‘,3,‘Color‘,‘b‘);
   plot(xy(1,1),xy(1,2),‘x‘,‘LineWidth‘,3,‘Color‘,‘yellow‘);
   plot(xy(2,1),xy(2,2),‘x‘,‘LineWidth‘,3,‘Color‘,‘red‘);

   len = norm(lines(k).point1 - lines(k).point2);
   if ( len > max_len)
      max_len = len;
      xy_long = xy;
   end
end



(代码发布未完,请待后续...)

《数字图像处理原理与实践(MATLAB版)》一书之代码Part4