Clear Filters
Clear Filters

Convolution giving decimal point in the result

1 view (last 30 days)
Hi,
Im trying to learn to look on the result from the kernel after convolution. Even i followed youtube for the double on the data but yet has a problem with the result.
Here is the code and the result, i don't know what else i had to change..
I = imread('C:\\MEKH\\LENA.jpg');
figure, imshow(I);
J=rgb2gray(I);
figure, imshow(J)
J=double(J)
A = [1,1,1;1,1,1;1,1,1]/9;
B = conv2(J,A,'same')
B=double(B)
figure, imshow(B)
Result:
Input
J =
222 208 255 255 253
105 141 82 108 169
121 252 4 1 124
89 124 79 195 227
253 252 196 221 250
B =
75.1111 112.5556 116.5556 124.6667 87.2222
116.5556 154.4444 145.1111 139.0000 101.1111
92.4444 110.7778 109.5556 109.8889 91.5556
121.2222 152.2222 147.1111 144.1111 113.1111
79.7778 110.3333 118.5556 129.7778 99.2222

Accepted Answer

Ameer Hamza
Ameer Hamza on 13 Jun 2020
Edited: Ameer Hamza on 13 Jun 2020
the output of conv2() is expected to be a fractional number. To use imshow, first convert it to uint8(). There are two alternatives
Convert the output of conv to uint8
I = imread('C:\\MEKH\\LENA.jpg');
figure, imshow(I);
J = rgb2gray(I);
figure, imshow(J)
J = double(J)
A = [1,1,1;1,1,1;1,1,1]/9;
B = conv2(J,A,'same')
B = uint8(B)
figure, imshow(B)
Or, use im2double() to convert the original image to double datatype
I = im2double(imread('C:\\MEKH\\LENA.jpg'));
figure, imshow(I);
J=rgb2gray(I);
figure, imshow(J)
A = [1,1,1;1,1,1;1,1,1]/9;
B = conv2(J,A,'same')
figure, imshow(B)
  2 Comments
Zamani Md Sani
Zamani Md Sani on 13 Jun 2020
thanks for sharing your knowledge to me.. just a month learning matlab .. thanks

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!