Added matlab binarization file
This commit is contained in:
26
binarization/binarizer.m
Normal file
26
binarization/binarizer.m
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
function out_im = binarizer(in_im, v, h)
|
||||||
|
|
||||||
|
%This programs splits an image into 'v' blocks in vertical direction
|
||||||
|
% and into 'h' blocks in horizontal direction. All the bloacks are equal.
|
||||||
|
|
||||||
|
[x, y] = size(in_im); %size of the input image
|
||||||
|
out_im = []; %output image
|
||||||
|
|
||||||
|
for ii = 1:(v)
|
||||||
|
temp = [];
|
||||||
|
ranx = ((ii-1)*ceil(x/v)+1):(ii*ceil(x/v)); %horizontal range
|
||||||
|
if ii == v
|
||||||
|
ranx = (((ii-1)*ceil(x/v)+1):x);
|
||||||
|
end
|
||||||
|
for jj = 1:(h)
|
||||||
|
rany = ((jj-1)*ceil(y/h)+1):(jj*ceil(y/h)); %horizontal range
|
||||||
|
if jj == h
|
||||||
|
rany = (((jj-1)*ceil(y/h)+1):y);
|
||||||
|
end
|
||||||
|
t = graythresh(in_im(ranx,rany));
|
||||||
|
temp = cat(2,temp,imbinarize(in_im(ranx,rany),t));
|
||||||
|
end
|
||||||
|
out_im = cat(1,out_im,temp);
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
82
binarization/main.m
Normal file
82
binarization/main.m
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
clear
|
||||||
|
close all
|
||||||
|
clc
|
||||||
|
format compact
|
||||||
|
|
||||||
|
%Loading 1st image
|
||||||
|
im = imread('rice.tif');
|
||||||
|
t = graythresh(im); %threshold for binarize
|
||||||
|
|
||||||
|
[m, n] = imhist(im); %we need 'm' for threshold marker on the histogram
|
||||||
|
|
||||||
|
figure
|
||||||
|
subplot(1,2,1); imshow(im)
|
||||||
|
title('Original image')
|
||||||
|
subplot(1,2,2); imhist(im)
|
||||||
|
line([round(255*t), round(255*t)], [0, max(m)], 'Color', 'r', 'LineWidth', 1) %threshold marker
|
||||||
|
title({'Image histogram', ['Red line - threshold (', num2str(t * 255, 3),')']})
|
||||||
|
|
||||||
|
%binarizing image
|
||||||
|
im_bin = imbinarize(im, t);
|
||||||
|
figure
|
||||||
|
subplot(1,2,1); imshow(im_bin)
|
||||||
|
title('Whole image binarization')
|
||||||
|
|
||||||
|
%amount of blocks
|
||||||
|
vert = 1; %vertical amount
|
||||||
|
hor = 10; %horizontal amount
|
||||||
|
|
||||||
|
%binarizing using blocks
|
||||||
|
im_bin_block = binarizer(im, vert, hor);
|
||||||
|
subplot(1,2,2); imshow(im_bin_block)
|
||||||
|
title({['Block binarization: '], [num2str(vert),' x ',num2str(hor),' blocks']})
|
||||||
|
|
||||||
|
%loading 2nd image
|
||||||
|
|
||||||
|
%amount of blocks
|
||||||
|
vert = 10; %vertical amount
|
||||||
|
hor = 1; %horizontal amount
|
||||||
|
|
||||||
|
im2 = imread('figure.png');
|
||||||
|
t = graythresh(im2); %threshold for binarize
|
||||||
|
im2_bin = imbinarize(im2, t);
|
||||||
|
im2_bin_block = binarizer(im2, vert, hor);
|
||||||
|
|
||||||
|
[m, ~] = imhist(im2); %we need 'm' for threshold marker on the histogram
|
||||||
|
|
||||||
|
figure
|
||||||
|
subplot(1,2,1); imshow(im2)
|
||||||
|
title('Original image')
|
||||||
|
subplot(1,2,2); imhist(im2)
|
||||||
|
line([round(255*t), round(255*t)], [0, max(m)], 'Color', 'r', 'LineWidth',...
|
||||||
|
1)
|
||||||
|
title({'Image histogram', ['Red line - threshold (',num2str(t*255, 3),')']})
|
||||||
|
|
||||||
|
figure
|
||||||
|
subplot(1,2,1); imshow(im2_bin)
|
||||||
|
title('Whole image binarization')
|
||||||
|
subplot(1,2,2); imshow(im2_bin_block)
|
||||||
|
title({['Block binarization: '],[num2str(vert),' x ',num2str(hor),' blocks']})
|
||||||
|
|
||||||
|
%trying to increase contrast
|
||||||
|
im3 = imadjust(im2,[],[],0.5);
|
||||||
|
t = graythresh(im3); %threshold for binarize
|
||||||
|
im3_bin = imbinarize(im3, t);
|
||||||
|
im3_bin_block = binarizer(im3, 10, 10);
|
||||||
|
|
||||||
|
[m,n] = imhist(im3); %we need 'm' for threshold marker on the histogram
|
||||||
|
|
||||||
|
figure
|
||||||
|
subplot(1,2,1); imshow(im3)
|
||||||
|
title('Increased contrast')
|
||||||
|
subplot(1,2,2); imhist(im3)
|
||||||
|
line([round(255*t), round(255*t)], [0, max(m)], 'Color', 'r', 'LineWidth',...
|
||||||
|
1)
|
||||||
|
title({'Image histogram', ['Red line - threshold (', num2str(round(255*t)),')']})
|
||||||
|
|
||||||
|
figure
|
||||||
|
subplot(1,2,1); imshow(im3_bin)
|
||||||
|
title('Whole image binarization')
|
||||||
|
subplot(1,2,2); imshow(im3_bin_block)
|
||||||
|
title({['Block binarization: '],[num2str(vert),' x ',num2str(hor),' blocks']})
|
||||||
|
|
||||||
Reference in New Issue
Block a user