1
votes

I am trying to use the PDE heat equation and apply it to images using Matlab. The problem i am having is that the image isn't blurring , it is just going white. Also, I am getting different results from the rest of the class who is using Maple.

Here is the code:

% George Lees Jr. 
% Heat equation

clear,clc;
dx = 1;
dy = 1;
dt = .025;
%dt/(dx*dx)

t = 0;
time = 3;
T_old = imread('tulipgray.jpg');
T_temp=T_old;
[m,n,k] = size(T_temp);
%colormap gray;
%imagesc(T_temp);
%imshow(T_old);
T_new = T_temp;
T_new=ind2gray(T_new,colormap);
%T_new(:,50)=0;
%T_old(1,70)
%imagesc(T_new);
%diff_x = dt/(dx*dx)
%diff_y = dt/ (dy*dy)
%time = 0;
while t < time
    for i = 2:1:m-1
        for j = 2:1:n-1
                T_new(i,j) = T_temp(i,j) + dt*(T_temp(i+1,j) -2*T_temp(i,j) + T_temp(i-1,j)) + dt*(T_temp(i,j+1)-2*T_temp(i,j) + T_temp(i,j-1));

        t = t+dt;
        T_temp(i,j) = T_new(i,j);


        end
    end
end

figure
imshow(T_new)

Yeah the image just gets whiter

1
Not sure what's wrong, but I Googled it and found this: math.ucla.edu/~wittman/reu2008/matlabIP.pdf The code on page 8 seems to work, maybe you could use compare it to your code to find the problem. - Rafael Monteiro

1 Answers

2
votes

There's 2 issues with your code:

1) you're incrementing the time counter after each individual pixel instead of after doing the whole image

2) you need to do the calculations on floating points values, not integers. dt is small, so the values from the RHS of the equation are <1

Fixed code should look something like this

clear,clc;

dt = 0.025;
time = 3;
T_old = imread('rice.png');
T_temp=double(T_old);
[m,n,k] = size(T_temp);

T_new = double(T_temp);
T_new=ind2gray(T_new,colormap);

while t < time
    for i = 2:1:m-1
        for j = 2:1:n-1
            T_new(i,j) = T_temp(i,j) + dt*(T_temp(i+1,j) -2*T_temp(i,j) + T_temp(i-1,j)) + dt*(T_temp(i,j+1)-2*T_temp(i,j) + T_temp(i,j-1));
        end
    end
   T_temp = T_new;
   t = t+dt;
   imshow(uint8(T_new))
   getframe;
end