gray scale image is not showed without divide by 255
1 view (last 30 days)
Show older comments
Maria Hernandez
on 30 Nov 2020
Answered: Image Analyst
on 3 Dec 2020
We are taking color images noised with a known noise and converting them to gray scale images using the next code. We also resize the images for the main procedure:
img5 = imread('moon.jpg');
img5= rgb2gray(img5);
img5= imresize(img5,[256,256]);
dimg5= double (img5)
Afterthat the image is treated substracting to every column its mean. After being centered the covariance matrix is calculated and after all pcacov is appplied.
Next only a defined number of principal components is taken to recreate the new cleaned image mutliplying the coeficients of the PCA by the data and later the reconstructed cleaned image is showed using imshow.
Output = im2uint8(Output);
imshow(Output);
imshow(img);
title('Original limpia');
I need to caculate Frobenius norm to between the original image and the one resultant of PCA. The problem raises because MATLAB shows the original image correctly but the cleaned one is showed like a black and white void image with some black traces. Even though, the Frobenius norm is calculated and I obtain a value not to high just normal, similar to the one obtained by the same algoritm in Python. Trying to fix that behavior I found that dividing the dimg5= double (img5)/255, before of the treatment returns a gray scale image as I expected. However, the Frobenius norm returns values ten times bigger than not dividing. Then I dont know which is the correct distance between the two images and which is the correct image resultant od the predure.
0 Comments
Accepted Answer
Walter Roberson
on 1 Dec 2020
dimg5= double (img5)
That converts datatype but leaves the range alone. You are working with the floating point range 0 to 255.
Output = im2uint8(Output);
im2uint8() is only suitable with double precision inputs when the inputs are intended to be in the range 0 to 1.
im2uint8() is the opposite (so to speak) of im2double(), which would be the proper routine to use instead of using double(img5)
More Answers (1)
Image Analyst
on 3 Dec 2020
Try using [] to automatically scale the min to 0 and the max to 255 for display:
imshow(img, []);
No casting is needed if you just want to display it and see it.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!