how to converts a gray scale image (All pixels from 0 to 255) to a black and white image (All pixels either 0 or 1)?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
this my given code, and I want to complete it by C-means clustering :
please read the code, and give me the complementary code.
thanx in advance..
% read image matrix from file
Img=imread('C:\Users\sony\Desktop\fp.png');
% convert to gray scale (remove color info)
Img = rgb2gray(Img);
% show the fingerprint image in a window
figure(1); imshow(Img);
% convert image data to double precision format
ImgD=double(Img);
% convert matrix data to single column vector format
% namely arrange all numbers in the matrix as a single column vector
grades=ImgD(:);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DO YOUR CLUSTERING HERE !
% For illustrative purposes, I compute the median, and use it to decide pass
% or fail. But you should do clustering and decide about the threshold !
t = median(grades);
% This selection will not result a good B&W image at the end.
% Your method should result a better B&W image !
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate a new image matrix in memory
newImg = zeros(size(Img));
for row=1:size(Img,1)
for col=1:size(Img,2)
if (ImgD(row,col) < t)
newImg(row,col) = 0; % below threshold fails
else
newImg(row,col) = 1; % above threshold passes
end
end
end
figure(2); imshow(newImg);
1 Kommentar
dpb
am 2 Dez. 2017
Bearbeitet: dpb
am 2 Dez. 2017
If all you're going to do is fixed threshold there's no need for looping...
newImg=ones(size(ImgD); % start with all white
ix=(ImgD<t); % 2D logical array
newImg(ix)=0; % below threshold fails
Or, of course, since the result of logic test is 0,1 already, then even more concise is
newImg=uint8(ImgD>=t); % cast to whatever data type desired
Antworten (1)
Image Analyst
am 2 Dez. 2017
You can either do thresholding like dpb showed above in his comment, or use some other kind of algorithm, like imbinarize() or similar. If you want to do a statistical clustering, see my attached kmeans demo.
Generally kmeans does a bad job of color segmentation. I don't have the Fuzzy Toolbox so I can't help you with fuzzy c-means classification.
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!