Octave/Matlab High Boost filtering - matlab

I have to use a Gaussian lowpass filter for the blurring step and then I have to improve the sharpness of the result using high-boost filtering.
Here is what I have so far:
I=imread('blurry-moon.tif');
A = fft2(double(I));
Ashift=fftshift(A);
[m n]=size(A);
R=10;
X=0:n-1;
Y=0:m-1;
[X Y]=meshgrid(X,Y);
Cx=0.5*n;
Cy=0.5*m;
LoF=exp(-((X-Cx).^2+(Y-Cy).^2)./(2*R).^2);
Gauss=Ashift.*LoF;
GaussShift=ifftshift(Gauss);
InverseGauss=ifft2(GaussShift);
%High boost
f = double(InverseGauss);
[m n]=size(f);
J0 = f;
for i=3:m-2
for j=3:n-2
J0(i,j) = (-8*f(i,j))+(1*f(i-1,j))+(1*f(i+1,j))+(1*f(i,j-1))+(1*f(i,j+1))...
+(1*f(i-1,j-1))+(1*f(i+1,j+1))+(1*f(i-1,j+1))+(1*f(i+1,j-1));
end
end
%----visualizing the results----------------------------------------------
figure(1)
imshow(I);colormap gray
title('original image','fontsize',14)
figure(2)
imshow(abs(Ashift),[-12 300000]), colormap gray
title('fft of original image','fontsize',14)
figure(3)
imshow(abs(InverseGauss),[12 290]), colormap gray
title('low pass filtered image','fontsize',14)
figure(4)
imshow(abs(J0),[12 290]), colormap gray
title('final image','fontsize',14)
I think I do something wrong in the high boost. But I think I am doing the gaussian filter right?
Can someone help out with the high boost filter?
Best regards!

img = imread('moon.tif');
% create gaussian filter
h = fspecial('gaussian',5,2.5);
% blur the image
blurred_img = imfilter(img,h);
% subtract blurred image from original
diff_img = img - blurred_img;
% add difference to the original image
highboost_img = img + 3*diff_img;
subplot 221
imshow(img,[]);
title('Original Image')
subplot 222
imshow(blurred_img,[]);
title('Blurred Image')
subplot 223
imshow(diff_img,[]);
title('Difference Image')
subplot 224
imshow(highboost_img,[]);
title('HighBoosted Image')

Related

How to find peaks in an image using matlab?

I am trying to outline all peaks in an image. The brightest lines are the peaks. I am using Matlab. This is what I have so far....
Any help will be greatly appreciated. Here is the image.
a = imread('duneLiDARs.png');
%b = imregionalmax(a);
%a = rgb2gray(a);
c = edge(a,'Sobel');
b = edge(a,'log',.0006);
d = edge(a,'log');
c= imfuse(a,d);
d= d-b;
subplot(2,2,1), imshow(a)
subplot(2,2,2), imshow(b)
subplot(2,2,3), imshow(c)
subplot(2,2,4), imshow(d)
%imshow(b);
%c = imadd(a,b);
%imshow(b);
you need to define what do you consider as peaks - what is the desired output for your image.
however, there are some general 2D peaks finding function, the following code uses FEX's extrema2:
% load image and remove extreme noise
im = medfilt2( im2double(imread('dune.png')));
% find peaks using extrema2
[XMAX,IMAX,XMIN,IMIN] = extrema2(im);
% eliminate peaks under minimum threshold
underThresh = XMAX < 0.15;
IMAX(underThresh) = [];
XMAX(underThresh) = [];
% plotting
subplot(121);
surf(im,'EdgeColor','none');
hold on;
[y,x] = ind2sub(size(im),IMAX);
scatter3(x,y,XMAX,'r','filled');
axis square
subplot(122);
imshow(im,[]);
hold on;
scatter(x,y,'r','filled');

How can I achieve a difference of gaussian high pass filter?

Seeing this answer, is this already an high-pass filter?
You need to use the Fourier transform and zero the low frequencies. Something like this:
I = imread('cameraman.tif');
%fourier transform
If = fftshift(fft2(I));
%create the filter as a mask with circle of radius 50
rx = (1:size(I,1)) - size(I,1)/2;
ry = (1:size(I,2)) - size(I,2)/2;
[X,Y] = meshgrid(rx,ry);
R = sqrt(X.^2 + Y.^2);
Ifhw = If;
Ifhw(R < 25) = 0; % we kill the low frequencies
%get the inversed fourier transform
Ihw = abs(ifft2(Iflw));
%use the abs log for visualization purposes
abslog = #(X)(log(abs(X)+1));
figure
subplot(2,2,3)
imshow(abslog(If), [])
title('fft in the original image')
subplot(2,2,4)
imshow(abslog(Iflw), [])
title('fft low pass filter image')
subplot(2,2,1)
imshow(I, [])
title('original image')
subplot(2,2,2)
imshow(Ilw, [])
title('low pass filter image')

MATLAB: Apply a low-pass filter to an image

I am trying to implement a simple low-pass filter using "ones" function as a filter and "conv2" to compute the convolution of both matrices (the original image and the filter), which is the filtered image I want to get, but the result of imshow(filteredImage) is just an empty white image instead of a filtered image.
I have checked the matrice of the filtered image, it is a 256x256 double, but I don't know the reason why it isn't displayed properly.
I = imread('cameraman.tif');
filteredImage = conv2(double(I), double(ones(3,3)), 'same');
figure; subplot(1,2,1); imshow(filteredImage);title('filtered');
subplot(1,2,2); imshow(I); title('original');
EDIT:
I have also tried converting it to double first before calculating the convolution as it was exceeding 1, but it didn't give a low-pass filter effect, but the image's contrast got increased instead.
I = imread('cameraman.tif');
I1 = im2double(I);
filteredImage = conv2(I1, ones(2,2), 'same');
figure; subplot(1,2,1); imshow(filteredImage);title('filtered');
subplot(1,2,2); imshow(I1); title('original');
The following solution has fixed the range issue, the other solutions that were given were about a specific type of low-pass filters which is an averaging filte :
Img1 = imread('cameraman.tif');
Im=im2double(Img1);
filteredImage = conv2(Im, ones(3,3));
figure; subplot(1,2,1); imshow(filteredImage, []);title('filtered');
subplot(1,2,2); imshow(Im); title('original');
Instead of dividing by the kernel, I've used imshow(filteredImage, []).

edge detection via wavelet transform(dwt2)

I want to detect edges of an image via dwt2. In fact I am going to simulate this article.
The first step of edge detection is based on replacing of all approximation coefficients with zeros. But when I replace the approximation coefficients with zero, the edges aren't similar to results seen in the article.
Here is my code:
clc;
clear all,close all
img=imread('2.png');
img=img(:,:,1);
imshow(img);
L = medfilt2(img,[3 3]);
L=im2double(L);
[A,H,V,D]=dwt2(L,'haar');
A=zeros(size(A));
Q1 = idwt2(A,H,V,D,'haar');
figure;
subplot(1,2,1);
imshow(img);
subplot(1,2,2);
imshow(Q1);`
enter code here
clc;
clear all,close all
img=imread('2.png');
img=rgb2gray(img);
L = medfilt2(img,[3 3]);
t=graythresh(L);
b=im2bw(L,t);
[A,H,V,D]=dwt2(b,'haar');
A1=zeros(size(A));
Q1 = idwt2(A1,H,V,D,'haar');
figure;
subplot(1,2,1);
imshow(img);
subplot(1,2,2);
imshow(Q1);

What kind of noise is present in this image and how do I remove it?

My task was to deblur a image. I used Weiner Filter and got this kind of image. Is it possible to improve it further?
Here is my code:
I = im2double(imread('Demo4_b.jpg'));
imshow(I);
title('Original Image');
LEN = 21;
THETA = 11;
PSF = fspecial('motion', LEN, THETA);
estimated_nsr = 0;
wnr2 = deconvwnr(I, PSF, estimated_nsr);
figure, imshow(wnr2)
title('Restoration of Blurred, Noisy Image Using NSR = 0')
estimated_nsr = noise_var / var(I(:));
wnr3 = deconvwnr(I, PSF, estimated_nsr);
figure, imshow(wnr3)
title('Restoration of Blurred, Noisy Image Using Estimated NSR');
I am getting same output in both with NSR and without NSR cases. Here is my original image:
You use the motion kernel from the matlab example. The image, however, looks more like it was smoothed with a gaussian kernel. That is the reason you're getting the wobbly lines.
Try this:
I = im2double(imread('a.jpg'));
imshow(I);
title('Original Image');
PSF = fspecial('gaussian', [51 51], 5);
wnr2 = deconvwnr(blurred, PSF, 0.0003 / var(I(:)));
figure, imshow(wnr2)
title('Restoration of Blurred, Noisy Image Using NSR = 0')
You can still tune it with the two parameters (5 and 0.0003)