My code doest work well in loop,but works fine while doing manually.please find the error
10 views (last 30 days)
Show older comments
The code runs well with first image but while adding the second image in the folder the DWT filter part doesnt do well with it and gives out the error as shown below.Basically my code shoule be reading a image1 from a folder and process it and wait until the second image is uploaded in the folder and while it detects image2 in the folder it should process and so on.Please may I know where the error is.
The code is
clc;
close all;
clear ;
waitMax = 20;
k = 1;
while k < 6
jpgFilename = sprintf('image%d.jpg', k);
fullFileName = fullfile('C:\Users\New User1\Desktop\Project Matlab\', jpgFilename);
if exist(fullFileName, 'file') %if the image exist in the folder
% rgbImage = imread(fullFileName);
% figure, imshow(rgbImage)
myimg = imread(fullFileName);
J = im2gray(myimg);
mygrayimg = imresize(J,[256 256],'nearest');
New2 = im2double(mygrayimg);
subplot(2,3,1);
imshow(New2);
title('Original Image');
%Adding Gaussian noise with mean 0 and variance 0.01
gau = imnoise(mygrayimg, 'gaussian', 0, 0.01);
subplot(2,3,2);
imshow(gau);
New = im2double(gau);
title('Gaussian Noise Image');
% Perform Coiflet wavelet denoising
% Set the denoising parameters
waveletType = 'coif5'; % Coiflet wavelet type
level =4; % Wavelet decomposition level
threshold = 's'; % Thresholding method
thrSettings = 1.231; % Thresholding settings
% Perform wavelet decomposition
[C, S] = wavedec2(New, level, waveletType);
% Compute the noise estimate using universal thresholding
sigma = median(abs(C)) / 0.6745;
% Apply the thresholding
D = wthresh(C, threshold, sigma * thrSettings);
% Perform inverse wavelet transform to obtain the denoised image
denoisedImage = waverec2(D, S, waveletType);
% Display the denoised image
subplot(2,3,6);
imshow(denoisedImage);
title('DWT Coif Denoised Image');
mse_coif = immse(denoisedImage , New2)
psnr_coif = psnr(denoisedImage , New2)
else
for count=1:waitMax
%wait 20 seconds to the image
pause(1)
if exist(fullFileName, 'file')
k=k-1;
break; %Exit this loop (return to the main loop)
end
end
end
k=k+1;
end
3 Comments
Walter Roberson
on 7 May 2023
After you have run and reached the error, please show
which abs median
I suspect that there are additional lines in your code that you did not show us
Answers (1)
Aniket
on 14 Nov 2024 at 5:57
I tried to reproduce the issue by uploading 2 images into the folder, but the given code ran properly without any errors.
As mentioned by Walter, the error message you are encountering suggests that "abs" or "median" must be assigning itself to some variable and is being treated as an array instead of built-in function.
You can check what function or file is MATLAB referring to using the below command.
which abs median
Additionally, I recommend including the following checks to ensure there are no issues with the input image and algorithm:
- Show image in figure window using “imshow” function just after reading it.
- Before proceeding with the wavelet decomposition, you can check if the image contains any ‘NaN’ or ‘Inf’ values.
- Ensure that the wavelet decomposition is performed correctly. You can check if the wavelet decomposition vector returned is empty as shown below:
[C, S] = wavedec2(New, level, waveletType);
if isempty(C)
error('Wavelet decomposition failed.');
end
You may add try/catch blocks if any of the above checks fail.
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!