How to improve the thresholding result ?
1 次查看(过去 30 天)
显示 更早的评论
Hi, I faced problem in getting higher segmentation ratio. I used uppercase letter A at the center(white color) with the black background.I try to get the white pixels after the Sauvola thresholding method but it seems like the thresholding didn't work well or maybe there is something wrong with my code.Here is my code below.
%create blank image
w = 200;
h = 200;
blankImage= 255*ones(w,h,3,'uint8');
%position of the letter in the empty cell
position_x = (w+1)/2;
position_y = (h+1)/2;
% varying the font size, start from 10 to 16
font_start = 120;
font_end = 160;
num_fonts = font_start:10:font_end;
% get the number of fonts
numImages = length(num_fonts);
% create a cell array with number of fonts to fill in the image in next step
Output_collection = cell(1, numImages);
Orig_grayImage = cell(1, numImages);
alphabet_segmented = cell(5,5,5);
check_foreground = cell(5,5,5);
alphabet_Uppercase = cell(1,26);
alphabet_Uppercase = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
Int_image_array = cell(1, numImages);
A = cell(1, numImages);
Binaryimg{1,i} = cell(1, numImages);
alphabet_orginal = cell(1, numImages);
% alphabet_Image = cell(26,5);
noise_start = 0.01;
noise_end = 0.05;
num_noiseimg = noise_start:0.01:noise_end;
each_noise = length(num_noiseimg);
alphabetNoiseimg_collection = cell(5,5,5);
alphabetSauvImage_collection = cell(5,5,5);
true = 0;
noise_pos = 1;
Int_image = cell(1, numImages);
IntimageFig_pos =1:2:9;
NoiseimgFig_pos =2:2:10;
intensityDifference = 20:20:100;
each_intensityDifference=length(intensityDifference);
zeta = cell(5,5,5);
% for loop to create letter 'A'
% grayscale
% store into the cell array
% for all_alphabet=1:
for i=1:numImages
for font_size = num_fonts(i)
Orgimage= insertText(blankImage,[position_x position_y],'A','Font','Times New Roman','FontSize',font_size,'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
grayImage= rgb2gray(Orgimage);
BWImage = imcomplement(grayImage);
A{1,i} = BWImage;
Orig_grayImage{1,i} = A{1,i};
Binaryimg{1,i} = imbinarize(Orig_grayImage{1,i});
alphabet_orginal{1,i} = sum(Binaryimg{1,i}(:));
% figure(i);
% imshow(A{1,i});
% impixelinfo;
% background = BWImage == 0;
% foreground = ~background;
% Newforegnd = foreground;
b=insertText(blankImage,[position_x position_y],'b','Font','Times New Roman', 'FontSize', font_size, 'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
o=insertText(blankImage,[position_x position_y],'o','Font','Times New Roman Bold', 'FontSize', font_size, 'TextColor','black','BoxColor','w','BoxOpacity',0,'AnchorPoint','Center');
BWImage_bin = imbinarize(BWImage);
out = repmat('b',size(BWImage_bin));
BoldO = '<strong>o</strong>';
out(BWImage_bin) = 'o';
display(out);
Output_collection{1,i} = out;
% subplot(5,1,i);
% imshow(BWImage);
% axis on;
% title(sprintf('Font size of %d',font_size));
for IntDiff_no=1:each_intensityDifference
for IntDiff_percentage= intensityDifference(IntDiff_no)
% backgroundGrayLevel = IntDiff_percentage;
% Int_image = A{1,i};
% background_range = A{1,i} ==0;
% Int_image(background_range)= backgroundGrayLevel;
% foregroundGrayLevel = backgroundGrayLevel + ((backgroundGrayLevel/100)*255);
% foreground_range = A{1,i} ==255;
% Int_image(foreground_range) = foregroundGrayLevel;
% Int_image_array{i,IntDiff_no} = Int_image;
highIntensity = (IntDiff_percentage/100)* 255; % 20%
Int_image = uint8(rescale(A{1,i}, 0, highIntensity));
Int_image_array{i,IntDiff_no} = Int_image;
figure('Name',sprintf('Font size of %d for Gaussian noise',font_size),'NumberTitle','off');
for Noise_no =1:each_noise
for noise= num_noiseimg(Noise_no)
% Apply Gaussiannoise on the alphabet,
%using 0.01 to 0.05 (increament of 0.01 per step)standard deviation
NoiseImg = imnoise(Int_image_array{i,IntDiff_no}, 'gaussian',0, noise);
alphabetNoiseimg_collection{i,IntDiff_no,Noise_no}=NoiseImg;
%apply sauvola's thresholding
%m is mean value
%k is a constant between 0.2 to 0.5%
%s is standard deviation
%R is gray level
%figure('Name',sprintf('Image of alphabet with font size %d ',font_size),'NumberTitle','off');
%S = m* (1 + k * (s/R - 1));
%R = 128;
%k = 0.2;
sauvImage = sauvola(NoiseImg,[150 150]);
alphabetSauvImage_collection{i,IntDiff_no,Noise_no} = sauvImage;
%for checking total image
fprintf('i is%d',i);
% imshow(sauvImage);
% figure('Name','Sauvola thresholding bef and after','NumberTitle','off');
Image_segmented = nnz(sauvImage)
alphabet_segmented{i,IntDiff_no,Noise_no}= Image_segmented;
check_foreground{i,IntDiff_no,Noise_no} = sum(sauvImage(:))
zeta{i,IntDiff_no,Noise_no} = alphabet_orginal{1,i}/alphabet_segmented{i,IntDiff_no,Noise_no};
if alphabet_segmented{i,IntDiff_no,Noise_no} == check_foreground{i,IntDiff_no,Noise_no}
true = true +1;
end
subplot(5,1,Noise_no);
imshow(alphabetNoiseimg_collection{i,IntDiff_no,Noise_no});
axis on;
title(sprintf('guassian noise std dev is %f and intensity difference %f',noise,IntDiff_percentage));
end
end
end
end
figure('Name',sprintf('Font size of %d for Intensity Difference',font_size),'NumberTitle','off');
for int_no =1:IntDiff_no
subplot(5,1,int_no);
imshow(Int_image_array{i,int_no});
axis on;
title(sprintf('Intensity difference %d percent for alphabet',intensityDifference(int_no)));
end
end
end
fprintf('%d of sauvola image',true);
figure('Name','Image with different font_size','NumberTitle','off');
for font_no =1:numImages
for each_font = num_fonts(font_no)
subplot(5,1,font_no);
imshow(Orig_grayImage{1,font_no});
axis on;
title(sprintf('Image with %d font size',each_font));
end
end
figure('Name','Sauvola thresholding','NumberTitle','off');
for font_size = num_fonts(i)
for IntDiff_no=1:each_intensityDifference
for IntDiff_percentage= intensityDifference(IntDiff_no)
for Noise_no =1:each_noise
for noise= num_noiseimg(Noise_no)
subplot(5,1,Noise_no);
imshow(alphabetSauvImage_collection{i,IntDiff_no,Noise_no});
title(sprintf('Image with %d font size,%f intensity difference,%d noise',font_size,IntDiff_percentage,noise));
end
end
end
end
end
Here are some of the images produced,in sequence(font size increment in 10,intensity difference is fixed at 20%,gaussian noise with variance starts from 0.01 to 0.05(increment by 0.01 per step).Where should I place the code to output the thresholding image?Sorry, because the code is mixed with three parameters, which are font size, intensity difference in percentage and gaussian noise at the end.
My highest segmentation ratio that is for proving the thresholding is working, is only around 26% at font size of 120.
Should I choose smaller font size? Or I am wrong in code to extract the white pixel from the image after thresholding?Hope someone can guide. Thank you.
0 个评论
回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!