image processing - Local thresholding in MATLAB -
i trying implement local thresholding
in matlab 7.7
. original image
looks like:
as seen the word test
covered in black. image png
image having dimensions 919x551
. want apply local thresholding image can word test
visible clearly.
i have implemented following code works dividing entire image sub images of 60*60 blocks.
however, when doing so, not getting desired output.
my code:
clc; clear all; close all; im = imread('c:\samples\test100.png'); subplot(3,3,1); imshow(im); title('original image'); im = rgb2gray(im); im = double(im); subplot(3,3,2); imshow(im); title('gray scale image'); [row col] = size(im); max_im = max(max(im)); h = zeros(1,max_im+1); !1st block n = 1:1:60 m = 1:1:60 a(n,m) = im(n,m); end end = a+1; n = 1:1:60 m = 1:1:60 t = a(n,m); h(t) = h(t)+1; end end subplot(3,3,3); bar(h) [x,y] = ginput(1); n = 1:1:60 m = 1:1:60 if a(n,m)<x a(n,m) = 0; else a(n,m) = 255; end end end subplot(3,3,4); imshow(uint8(a)) title('1st block image'); !2nd block n = 1:1:60 m = 61:1:60 b(n,m-60) = im(n,m) end end b = b+1; n = 1:1:60 m = 1:1:60 t = b(n,m); h(t) = h(t)+1; end end figure(2) bar(h) [x,y] = ginput(1); n = 1:1:60 if b(n,m)<x b(n,m) = 0; else b(n,m) = 255; end end imshow(uint8(b)) !3rd block n = 61:1:120 m = 1:1:60 c(n-60,m) = im(n,m); end end c = c+1; n = 1:1:60 m = 1:1:60 t = c(n,m); h(t) = h(t)+1; end end figure(3) bar(h) [x,y] = ginput(1); n = 1:1:60 m = 1:1:60 if c(n,m)< x c(n,m) = 0; else c(n,m) = 255; end end end imshow(uint8(c)) !final block n = 1:1:row m = 61:1:col d(n-60,m-60) = im(n,m); end end d = d+1; n = 1:1:60 m = 1:1:60 t = d(n,m); h(t) = h(t)+1; end end figure(4); bar(h); [x,y] = ginput(1); n = 1:1:60 m = 1:1:60 if d(n,m)<x d(n,m) = 0; else d(n,m) = 255; end end end imshow(uint8(d)) s = [a b;c d]; figure(5); imshow(uint(s))
when try run entire code error as:
??? undefined function or method 'local' input arguments of type 'char'
however, when run code 1st block following output.
how word test
visible creating sub-images , merging them together?
you can scan greyscale image horizontally , find position of non-zero (or above threshold) values , set interval filled white (256 or 1 if using im2double).
for j=1:551 row = im(:,j) test = 0; im2=zeros(size(im)) i=0; %left black area while (test == 0 && i<919) im2(i,j)=0; if row(i)>threshold test=1; end; i=i+1; end; %white inner area while (test == 1 && i<919) im2(i,j)=1 if row(i)>threshold test=0; end; i=i+1; end; %left black area while (i<919) im2(i,j)=0; i=i+1; end;
this doesn't work letters have empty area (such 'p'), can modify code little that.
Comments
Post a Comment