Matlab laplacian filter doesn't work properly

6 views (last 30 days)
Hey guys! I am trying to apply a Laplacian filter on a picture but it just doesn't look like what it's supposed to be even if I followed all the steps (I found some questions here, but they still didn't help).
Here is my code and the input and output image and the image I am supposed to have.
my_img= imread ('D:\343a.tif');
[rows, columns, nBands] = size(my_img)
K4 = [ -1,-1,-1; -1,8, -1; -1,-1,-1];
filteredImage= conv2(double(my_img), K4,"same");
minR = min(filteredImage(:));
maxR = max(filteredImage(:));
filteredImage = (filteredImage - minR) / (maxR - minR);
figure 1
imshow(filteredImage, []);

Answers (1)

Image Analyst
Image Analyst on 6 Apr 2018
It's because you're not scaling your positive and negative values equally so the zero level is not at zero. Try this:
grayImage = imread ('D:\Matlab\work\Tests\hhhh.PNG');
[rows, columns, numberOfColorBands] = size(grayImage)
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
% Crop image to chop off white stuff and image on the right.
grayImage = grayImage(24:328, 1:160);
% Display the image.
subplot(1, 2, 1);
imshow(grayImage, []);
title('Original Image', 'FontSize', 20);
% Do the convolution.
kernel = [-1,-1,-1; -1,8,-1; -1,-1,-1];
filteredImage= conv2(double(grayImage), kernel, 'same');
% minR = min(filteredImage(:))
% maxR = max(filteredImage(:))
% Find the max value, regardless if it's positive or negative.
peakValue = max(abs(filteredImage(:)))
% Display the image with equal range in both the positive and negative direction.
subplot(1, 2, 2);
imshow(filteredImage, [-peakValue, peakValue]);
title('Filtered Image', 'FontSize', 20);

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!