2.1 Intro_Matlab
1) Dado A = [1 2 3;4 5 6;7 8 9];
a) Mostrar a primeira coluna de A
b) Mostrar a segunda linha de A
c) Mostrar o tamanho de A
1
1
%com for
img = imread('linux.jpg');
img2 = imread('Xadrez.jpg');
img = rgb2gray(img)
img2 = rgb2gray(img2)
tic;
for i=1:256
for j=1:256
img3(i,j)=((img(i,j)+img2(i,j))/2);
end
end
toc;
%sem for
img = imread('linux.jpg');
img2 = imread('Xadrez.jpg');
img = rgb2gray(img);
img2 = rgb2gray(img2);
tic;
toc;
5) Dada uma imagem colorida, construa uma funo que retorne cada
banda desta imagem.
function [R,G,B]=banda(im)
img = imread(im)
R = img(:,:,1);
G = img(:,:,2);
B = img(:,:,3);
figure(1)
imshow(R);
figure(2)
imshow(G);
figure(3)
imshow(B);
end
Red
Green
Blue
Imshow(A,mapa)
A = [1 1 2 1 3;1 1 2 3 1;2 2 3 2 2;1 3 2 1 1];
I = mat2gray(A);
mapa=[.4 .4 .4;0 .6 1;1 0 0];
subplot(121);
imshow(A,mapa)
MAPA
0.4000
0
1.0000
0.4000
0.6000
0
0.4000
1.0000
0
A
1
1
2
1
1
1
2
3
2
2
3
2
1
3
2
1
3
1
2
1
0.0667
0.0667
0.0667
0.1333
0.1333
0.1333
map
128x3
3072 double
13) Converter a imagem trees para imagem de intensidade e exibi-la com 128 nveis de
cinza.
load trees
J = ind2gray(X,map);
imshow(J)
load trees
[R,G,B] = ind2rgb(X,map);
J = [R,G,B];
imshow(J);
18) Testar a exibio de mltiplas imagens com o mesmo mapa de cores, com a
sequncia de passos a seguir;
clear all;
load trees;
subplot(121);
imshow(X,map);
title('Antes de Rotacionar');
subplot(122);
imshow(imrotate(X,35,'crop'),map);
title('Depois')
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
load clown
I=ind2gray(X,map);
subplot(211);
imhist(I);
subplot(212);
imshow(I);
load trees
I=ind2gray(X,map);
subplot(211);
imhist(I);
subplot(212);
imshow(I);
2000
1000
0
0
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
Z =x y
load trees
I=ind2gray(X,map);
J = imadjust(I,[],[],.5);
subplot(221);
imshow(I);
subplot(223);
imshow(J);
subplot(222);
imhist(I);
subplot(224);
imhist(J);
2000
1000
0
0
0.5
0.5
2000
1000
0
2000
1000
0
0
0.5
0.5
2000
1000
0
load trees
I=ind2gray(X,map);
J = imadjust(I,[],
[],1.5);
subplot(221);
imshow(I);
subplot(223);
imshow(J);
subplot(222);
imhist(I);
subplot(224);
imhist(J);
Com gamma > 1, a imagem fica mais escura como pode ser visto no
histograma.
6) Equalizar o histograma da imagem trees para 128 nveis de cinza.
load trees
I=ind2gray(X,map);
J =histeq(I,128);
subplot(221);
imshow(I);
subplot(223);
imshow(J);
subplot(222);
imhist(I);
subplot(224);
imhist(J);
2000
1000
0
0
0.5
0.5
2000
1000
0
2000
1000
0
0
0.5
0.5
3000
2000
1000
0
J recebe uma imagem que mapeada com 128 nveis em uma imagem mapeada com 16
nveis.
8) Repetir os passos 6 e 7 para a imagem clown.
2000
1500
1000
500
0
0
0.5
0.5
3000
2000
1000
0
load clown;
I=ind2gray(X,map);
J =histeq(I,16);
subplot(221);
imshow(I);
subplot(223);
imshow(J);
subplot(222);
imhist(I);
subplot(224);
imhist(J);
3
2
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
4000
3000
2000
1000
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
6000
4000
2000
0
load trees
I = ind2gray(X,map);
J=imadjust(I,[0 0.5],[],[]);
subplot(222);
imhist(I,128);
subplot(221);
imshow(I);
subplot(224);
imhist(J,128);
subplot(223);
imshow(J);
0.2
0.4
0.6
0.8
0.2
0.4
0.6
0.8
3000
2000
1000
2.4 PDI_2
function out=filtro(i,j,im)
%gera a mscara em forma de vetor
masc(1:(i*j)) = 1/(i*j);
%faz a leitura da imagem e converte em escala de cinza
img = imread(im);
img = rgb2gray(img);
%encontra o tamanho da imagem
[l,c] = size(img);
%percorre a imagem levando em conta o tamanho da mscara
for(x=ceil(i/2):l-floor(i/2))
for(y=ceil(j/2):c-floor(j/2))
a=0;
pos=1; %controla os ndices da mscara
for(t=(x-floor(i/2)):(x+floor(i/2))) %percorre a mscara na
imagem
for(p=(y-floor(j/2)):(y+floor(j/2)))
a = (img(t,p)*masc(pos)) + a; %acumula as multiplicaes
pos=pos+1;
end
end
z(x,y) = a; %salva o valor na nova imagem
end
end
imshow(z)
out = z;
A funo filtro recebe como parmetros o nmero de linhas e colunas da
mscara e a imagem onde o filtro ser aplicado.
1) Implemente uma funo no matlab para correo de rudos aleatrios em imagens. Tal
funo dever permitir variar as dimenses da mscara de entrada.
Na imagem abaixo foi aplicado o filtro com mscara 5x5. Perceba que os pontos escuros na barriga do
pinguim comeam a sumir. Isso acontece pois ao redor deles a maioria dos pixels de cor branca.
2.5 Roteiro_PDI_3
1) Iniciar o matlab
2) Criar a matriz h para filtragem passa-baixa pela tcnica de filtragem de mdia.
3) Carregar a imagem trees, convert-la para imagem monocromtica e filtr-la
usando a tcnica de filtragem pela mdia. Exibir na tela as duas imagens e
concluir a respeito.
Imagem filtrada
load trees;
I = ind2gray(X,map);
J=imnoise(I,'salt &
pepper');
B=filter2(h,I);
subplot(221); imshow(I);
title('Original');
subplot(222); imshow(J);
title('Rudo Sal e
Pimenta');
subplot(223); imshow(B);
title('Imagem filtrada');
A funo imnoise adiciona rudo sal e pimenta. A funo filter2 realiza a convoluo de
h por I e retira o rudo.
load trees;
I = ind2gray(X,map);
K=medfilt2(I,[3 3]);
subplot(121); imshow(I);
subplot(122); imshow(K);
A funo medfilt2 realiza a convoluo da imagem I por um filtro 3x3.
O filtro aplicado da mediana.
6) Repetir o passo anterior para verso da imagem trees corrompida com o rudo
sale pimenta.
load trees;
I = ind2gray(X,map);
J=imnoise(I,'salt & pepper');
K=medfilt2(J,[3 3]);
subplot(221); imshow(I);
subplot(222); imshow(J);
subplot(223); imshow(K);
O resultado mostrou que o filtro da mediana apresenta um bom resultado
quando aplicado em imagens com rudo sal e pimenta.
load trees;
I = ind2gray(X,map);
J1=imnoise(I,'salt & pepper');
J2=imnoise(I,'salt & pepper');
J3=imnoise(I,'salt & pepper');
J4=imnoise(I,'salt & pepper');
J = (J1 + J2 + J3 + J4)/4;
subplot(231); imshow(I);
subplot(232); imshow(J);
subplot(233); imshow(J1);
subplot(234); imshow(J2);
subplot(235); imshow(J3);
subplot(236); imshow(J4);
Neste experimento 4 imagens foram geradas a partir da distoro de uma
imagem original com rudo sal e pimenta. A mdia das quatro imagens
com rudo gerou uma imagem resultante menos ruidosa.
8) Efeito emboss
load trees;
clf
I=ind2gray(X,map);
h=fspecial('sobel');
A=filter2(h,I);
imshow(mat2gray(A));
9) Realce de imagens
clear
A=[0 -1 0;-1 5 -1;0 -1 0];
load trees;
N=255;
J=ind2gray(X,map);
K=round(J*N);
L=filter2(A,K);
[lin,col]=size(L);
for i=1:lin
for j=1:col
if L(i,j)<0
L(i,j)=0;
end
end
end
for i=1:lin
for j=1:col
if L(i,j)>N
L(i,j)=N;
end
end
end
L = L/N;
subplot(121); imshow(J);
subplot(122); imshow(L);
A linha round(J*N) faz a converso de um mapa de intensidades variando
entre 0 e 1 para um mapa variando entre 0 e 255. A funo filter2
realiza a convoluo da imagem com o filtro passa altas. Os laos
subsequentes servem para impedir que aps a convoluo, alguns valores
fiquem abaixo de zero ou acima de 255. A linha L = L/N, faz com que a
imagem retorne novamente ao mapa de intensidades entre 0 e 1.
10)
Unsharp masking
clear
load trees;
J=ind2gray(X,map);
h=fspecial('unsharp');
clf
N=255;
K=round(J*N);
L=filter2(h,K);
[lin,col]=size(L);
for i=1:lin
for j=1:col
if L(i,j)<0
L(i,j)=0;
end
end
end
for i=1:lin
for j=1:col
if L(i,j)>N
L(i,j)=N;
end
end
end
L = L/N;
subplot(121); imshow(J);
subplot(122); imshow(L);
Neste exerccio foi utilizado o filtro unsharp que serve para realar
o contraste. Essa funo permite outros parmetros como:
11)
High boost
clear
load trees;
J=ind2gray(X,map);
p1=1.1;
p2=1.15;
p3=1.2;
w1=9*p1-1;
w2=9*p2-1;
w3=9*p3-1;
m1=[-1 -1 -1; -1 w1 -1; -1 -1 -1];
m2=[-1 -1 -1; -1 w2 -1; -1 -1 -1];
m3=[-1 -1 -1; -1 w3 -1; -1 -1 -1];
clf
N=255;
K=round(J*N);
L1=filter2(m1,K);
[lin,col]=size(L1);
for i=1:lin
for j=1:col
if L1(i,j)<0
L1(i,j)=0;
end
end
end
for i=1:lin
for j=1:col
if L1(i,j)>N
L1(i,j)=N;
end
end
end
L1=L1/N;
L2=filter2(m2,K);
[lin,col]=size(L2);
for i=1:lin
for j=1:col
if L2(i,j)<0
L2(i,j)=0;
end
end
end
for i=1:lin
for j=1:col
if L2(i,j)>N
L2(i,j)=N;
end
end
end
L2=L2/N;
L3=filter2(m3,K);
[lin,col]=size(L3);
for i=1:lin
for j=1:col
if L3(i,j)<0
L3(i,j)=0;
end
end
end
for i=1:lin
for j=1:col
if L3(i,j)>N
L3(i,j)=N;
end
end
end
L3=L3/N;
subplot(221);
subplot(222);
subplot(223);
subplot(224);
imshow(J);
imshow(L1);
imshow(L2);
imshow(L3);