Deblurring an image using the heat equation

1 Ansicht (letzte 30 Tage)
cee878
cee878 am 10 Dez. 2016
Bearbeitet: Vidhi Agarwal am 24 Sep. 2024
So I am trying to deblur an image using the heat equation, but when I run the code, I get a black image. So I did this by looking at http://www4.ncsu.edu/eos/users/w/white/www/white/ma325/MVlec3.pdf to write the code:
u0=double(imread('clockmodified.tiff'));
[m,n,k] = size(u0); %k=conductivity
%if k==3
%u0 = rgb2gray(u0);
%end;
u0 = double(u0);
subplot(1,2,1);
imagesc(u0);
colormap gray;
title('Original');
dt = .02; %time step
T = 1000; %stopping time
u = u0;
for t = 0:dt:T
u_xx = u(:,[2:n n]) - 2*u + u(:,[1 1:n-1]);
u_yy = u([2:m m], :) - 2*u + u([1 1:m-1],:);
u = u - k*dt*(u_xx+u_yy);
subplot(1,2,2);
imagesc(u);
title(['t=',num2str(t)]);
drawnow;
temp=u;
end;
% imshow(uint8(temp));
imwrite(uint8(temp),'clockdeblur.tiff','TIFF');

Antworten (1)

Vidhi Agarwal
Vidhi Agarwal am 24 Sep. 2024
Bearbeitet: Vidhi Agarwal am 24 Sep. 2024
The issue you're experiencing with getting a black image could be due to several factors in implementation of the heat equation for image deblurring.
  1. Conductivity Coefficient k: In given code, k is being assigned the number of colour channels of the image. This is incorrect for image deblurring. Define “k as a small positive constant that controls the rate of diffusion (e.g., k = 0.1).
  2. Ensure that the pixel values remain within a valid range (0 to 255) during the iterations. If the values exceed this range, they might cause the image to appear completely black or white.
  3. When using imagesc, the scaling might affect the display. Ensure that the image data is correctly scaled for visualization.
Revised code with above mentioned consideration will be:
% Read and preprocess the image
u0=imread(path_to_your_image.jpeg');
% Convert to grayscale if the image is in color
if size(u0, 3) == 3
u0 = rgb2gray(u0);
end
% Convert to double for processing
u0 = double(u0);
% Get the dimensions of the image
[m, n] = size(u0);
% Display the original image
figure;
subplot(1,2,1);
imagesc(u0);
colormap gray;
title('Original');
% Parameters for the heat equation
dt = 0.02; % Time step
T = 100; % Stopping time
k = 0.1; % Conductivity coefficient a small positive constant
% Initialize the image for processing
u = u0;
% Iterative deblurring using the heat equation
for t = 0:dt:T
% Compute second derivatives
u_xx = u(:, [2:n n]) - 2*u + u(:, [1 1:n-1]);
u_yy = u([2:m m], :) - 2*u + u([1 1:m-1], :);
% Update the image
u = u + k*dt*(u_xx + u_yy);
% Clip values to ensure they are within valid range
u = max(min(u, 255), 0);
% Display the updated image
subplot(1,2,2);
imagesc(u, [0 255]); % Ensure the display range is set
colormap gray;
title(['t = ', num2str(t)]);
drawnow;
end
% Save the final deblurred image
imwrite(uint8(u), 'clockdeblur.tiff', 'TIFF');
The output for the following code with sample input is given below:
For better understanding of “imagesc”, refer to the documentation, which you can access by running the following command in MATLAB command window:
  • web(fullfile(docroot, "matlab/ref/imagesc.html"))
Hope that helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by