MATLAB Answers

filter2() returns darker image

1 view (last 30 days)
David S
David S on 18 Mar 2020
Answered: Prabhan Purwar on 24 Mar 2020
Hi everyone. My goal is to interpolate an image using fspecial() and filter2(). Since filter2() uses conv2(), i thought about using it with every RGB component (filtering first R, then G and B )
I get my image bigger but at the same time it is darker. Here is my code:
clear all
close all
clc
foto=imread('david','JPG');
[filas,columnas,colores]=size(foto);
k=3; %columns*k
z=3; %rows*z
newfilas=filas*z; %new number of rows
newcolumnas=columnas*k; %new number of columns
newfoto=zeros(newfilas,newcolumnas,3); %new vector to store new values
fila_aux=1;
columna_aux=1;
for fila=1:z:newfilas %I study the rows every z
for columna=1:k:newcolumnas %I studi the columns evey k
%I store my new values in newfoto
newfoto(fila,columna,:)=foto(fila_aux,columna_aux,:);
columna_aux=columna_aux+1;
end
fila_aux=fila_aux+1;
columna_aux=1;
end
newfoto=newfoto/255;
% I create R,G and B variables to store the three components of the new photo
R = newfoto(:,:,1);
G = newfoto(:,:,2);
B = newfoto(:,:,3);
H = fspecial('average',[z k]);
%I apply filter2() to R,G and B
YR = filter2(H,R);
YG = filter2(H,G);
YB = filter2(H,B);
Y = YR + YG + YB; %I concatenate R,G and B to create the new filtered matrix of colors
figure;imshow(foto); %I show the original pic
title('Original');
figure;imshow(Y); %I show the edited one (IT IS LARGER AS EXPECTED BUT ITS ALSO DARKER)
title('New one (edited)');

  0 Comments

Sign in to comment.

Answers (1)

Prabhan Purwar
Prabhan Purwar on 24 Mar 2020
Hi,
Following code illustrates the interpolation using filter2 and fspecial
clear
close all
clc
foto=imread('office_3.jpg'); and
filter_coef= fspecial('average',[3 3]);
Y=interp(foto,filter_coef);
imshow(Y);
function Y = interp(A, filter_coef)
if (length(size(A)) == 3)
for i=1:3
Y(:,:,i) = interp(A(:,:,i),filter_coef);
end
else
[m,n] = size(A);
A_ = []; Y = [];
% columns interpolation
A_col= filter2(filter_coef,A);
for i=1:n
A_ = [A_ A(:,i) A_col(:,i)];
end
A_(:,end) = [];
% rows interpolation
A_rows = filter2(filter_coef,A_')';
for i=1:m
Y = [Y; A_(i,:); A_rows(i,:)];
end
Y(end,:) = [];
end
end
Output:

  0 Comments

Sign in to comment.


Translated by