I'm trying to implement Otsu binarization technique on document images such as the one shown:
Could someone please tell me how to implement the code in MATLAB?
Taken from Otsu's method on Wikipedia
I = imread('cameraman.tif');
Step 1. Compute histogram and probabilities of each intensity level.
nbins = 256; % Number of bins
counts = imhist(I,nbins); % Each intensity increments the histogram from 0 to 255
p = counts / sum(counts); % Probabilities
Step 2. Set up initial omega_i(0) and mu_i(0)
omega1 = 0;
omega2 = 1;
mu1 = 0;
mu2 = mean(I(:));
Step 3. Step through all possible thresholds from 0 to maximum intensity (255)
Step 3.1 Update omega_i and mu_i
Step 3.2 Compute sigma_b_squared
for t = 1:nbins
omega1(t) = sum(p(1:t));
omega2(t) = sum(p(t+1:end));
mu1(t) = sum(p(1:t).*(1:t)');
mu2(t) = sum(p(t+1:end).*(t+1:nbins)');
end
sigma_b_squared_wiki = omega1 .* omega2 .* (mu2-mu1).^2; % Eq. (14)
sigma_b_squared_otsu = (mu1(end) .* omega1-mu1) .^2 ./(omega1 .* (1-omega1)); % Eq. (18)
Step 4 Desired threshold corresponds to the location of maximum of sigma_b_squared
[~,thres_level_wiki] = max(sigma_b_squared_wiki);
[~,thres_level_otsu] = max(sigma_b_squared_otsu);
There are some differences between the wiki-version eq. (14) in Otsu and the eq. (18), and I don't why. But the thres_level_otsu correspond to the MATLAB's implementation graythresh(I)
Since the function graythresh in Matlab implements the Otsu method, what you have to do is convert your image to grayscale and then use the im2bw function to binarize the image using the threhsold level returned by graythresh.
To convert your image I to grayscale you can use the following code:
I = im2uint8(I);
if size(I,3) ~= 1
I = rgb2gray(I);
end;
To get the binary image Ib using the Otsu's method, use the following code:
Ib = im2bw(I, graythresh(I));
You should get the following result:
Starting out with what your initial question was implementing the OTSU thresolding its true that MATLAB's graythresh function is based on that method
The OTSU's method considers the threshold value as the valley between two peaks that is one of the foreground pixels and the other of the background pixels
Pertaining to your image which seems like a historical manuscript found this paper that compares all the methods that could be used for thresholding document images
You can also download and read up sauvola thresholding from here
Good luck with its implementation =)
Corrected MATLAB Implementation (for 2d matrix)
function [T] = myotsu(I,N);
% create histogram
nbins = N;
[x,h] = hist(I(:),nbins);
% calculate probabilities
p = x./sum(x);
% initialisation
om1 = 0;
om2 = 1;
mu1 = 0;
mu2 = mode(I(:));
for t = 1:nbins,
om1(t) = sum(p(1:t));
om2(t) = sum(p(t+1:nbins));
mu1(t) = sum(p(1:t).*[1:t]);
mu2(t) = sum(p(t+1:nbins).*[t+1:nbins]);
end
sigma = (mu1(nbins).*om1-mu1).^2./(om1.*(1-om1));
idx = find(sigma == max(sigma));
T = h(idx(1));
Related
I have tried to extract the dark line inside very noisy images without success. Some tips?
My current steps for the first example:
1) Clahe: with clip_limit = 10 and grid_size = (8,8)
2) Box Filter: with size = (5,5)
3) Inverted Image: 255 - image
4) Threshold: when inverted_image < 64
UPDATE
I have performed some preprocessing steps to improve the quality of tested images. I adjusted my ROI mask to crop top and down (because they are low intensities) and added a illumination correction to see better the line. Follow below the current images:
Even though the images are noisy, you are only looking for straight lines towards the north of image. So, why don't use some kind of matched filter with morphological operations?
EDIT: I have modified it.
1) Use median filter along the x and y axis, and normalize the images.
2) Matched filter with all possible orientations of lines.
% im=imread('JwXON.png');
% im=imread('Fiy72.png');
% im=imread('Ya9AN.png');
im=imread('OcgaIt8.png');
imOrig=im;
matchesx = fl(im, 1);
matchesy = fl(im, 0);
matches = matchesx + matchesy;
[x, y] = find(matches);
figure(1);
imagesc(imOrig), axis image
hold on, plot(y, x, 'r.', 'MarkerSize',5)
colormap gray
%----------
function matches = fl(im, direc)
if size(im,3)~=1
im = double(rgb2gray(im));
else
im=double(im);
end
[n, m] = size(im);
mask = bwmorph(imfill(im>0,'holes'),'thin',10);
indNaN=find(im==0); im=255-im; im(indNaN)=0;
N = n - numel(find(im(:,ceil(m/2))==0));
N = ceil(N*0.8); % possible line length
% Normalize the image with median filter
if direc
background= medfilt2(im,[1,30],'symmetric');
thetas = 31:149;
else
background= medfilt2(im,[30,1],'symmetric');
thetas = [1:30 150:179];
end
normIm = im - background;
normIm(normIm<0)=0;
% initialize matched filter result
matches=im*0;
% search for different angles of lines
for theta=thetas
normIm2 = imclose(normIm>0,strel('line',5,theta));
normIm3 = imopen(normIm2>0,strel('line',N,theta));
matches = matches + normIm3;
end
% eliminate false alarms
matches = imclose(matches,strel('disk',2));
matches = matches>3 & mask;
matches = bwareaopen(matches,100);
I am working on palm print identification by palm texture and geometry. i want to binarized hand image in preprocessing step for extracting geometry features like palm width and finger width.
i have used Gaussian filter for reduced noise and Otsu method for thresholding but I could not reach Optimal image! i was wondering if someone help me!
my database downloaded from "IIT Delhi Touch-less Palm print "
I=imread('hand.jpg');
h= fspecial('gaussian', 15,5);
s=imfilter(I,h,'symmetric');
q=graythresh(I)
BW=im2bw(I,q);
I have tried the following code and getting some promising result on your dropbox images. you can try it and share your results for further approach.
clc
clear all
close all
impath = 'E:\GoogleDrive\Mathworks\irisDEt\HandSeg';
[name,path] = uigetfile({'*.jpg';'*.png'},'mytitle',impath);
im =imread([path,name]);
im = imresize(im,0.5);
gms = 15;
red = im(:,:,1);
redmed = medfilt2(red,[gms,gms],'symmetric');
redmedbw = im2bw(redmed,0.9*graythresh(redmed));
redmedbw = bwareaopen(redmedbw,1500);
redmedbw = imclose(redmedbw,strel('disk',5));
figure,imshow(im,[])
figure,imshow(redmed,[])
figure,imshow(redmedbw,[])
My results are:
Code for signature estimation of the structure and extraction on critical(peaks and vallys) from the structure:
function [sig,xysamp,idx]= signature(bw,prec)
boundry = bwboundaries(bw);
xy = boundry{1};
x = xy(:,1);
y = xy(:,2);
len = length(x);
res = (len/prec);
re = rem(res,2);
if re
res = ceil(res);
end
indexes = 1:res:len;
xnew = x(indexes);
ynew = y(indexes);
xysamp = [xnew,ynew] ;
cx = round(mean(xnew));
cy = round(mean(ynew));
xn = abs(xnew-cx);
yn = abs(ynew-cy);
% ang = atand(yn./xn);
sig = (xn.^2+yn.^2);
sig = sig/max(sig);
% Critical Points in Signatures.
diffsig = diff(sig);
% pos = zeros(length(diffsig),1);
idx = 1;
for i = 2:length(diffsig)
if diffsig(i-1)*diffsig(i) <0
idx = [idx,i];
end
end
idx = [idx,i];
Here idx are the indexes of xysamp which gives actual boundry location in the image. the location of peaks and vallys may not be exact as i m doing sampling of boundry and it is a very simple way to approach the structural based problems.
Thank You
Result of critical point extraction:
I'm trying to implement the Sauvola & Pietaksinen method to perform a binarization in an image via local thresholding.
The method defines the threshold of each pixel (x,y) as T(x,y) = mean(x,y)*[1+k(std(x,y)/R-1)], as in the arcticle ”Adaptive Document Image Binarization”. The mean and the standard deviation are calculated in a neighbourhood of (x,y). k and R are suggested to be 0.5 and 128, respectively.
This is what my code looks like:
filtered = colfilt(image, [n n], "sliding", #(x) (mean(x).*(1+0.5*(std(x)/128 - 1))));
image(image < filtered) = 0;
image(image >= filtered) = 255;
However, for all images I tested, the result is a entirely blank image, which is obviously incorrect. I think I must be misusing some element in the colfilt function, but I'm too newbie at Octave and couldn't find it until now.
Could someone please give me a hand?
Thanks in advance.
I can't see a problem. You really should include your source and perhaps also your input image and parameter for n. Btw, you shouldn't overwrite function names (like image in your case).
Input image:
pkg load image
img = imread ("lenna256.jpg");
k = 0.5;
R = 128;
n = 5;
filtered = colfilt(img, [n n], "sliding", #(x) (mean(x).*(1+0.5*(std(x)/128 - 1))));
img(img < filtered) = 0;
img(img >= filtered) = 255;
image (img)
imwrite (img, "lenna_out.png")
which creates
I have calculated 8 Gabor filters with Theta rotation m*PI/8.
Parameters of the Gabor kernel given as input to OpenCv cv2.getGaborKernel:
ksize = 11, theta = m*PI/8 lambd = 16/3 sigma = (5.09030 * 8.0) / (3.0 * PI) gamma = 0.5890 psi = 0
kernel = cv2.getGaborKernel(ksize = (ksize,ksize), sigma = sigma,
theta = theta, lambd = lambd,
gamma = gamma, psi = psi)
The parameters are designed according to "Features Extraction using a Gabor filter family", Zhen, Zhao, Wang.
The formula adopted is the one of the third family of Gabor filters.
The 8 filters obtained are:
The original image is:
The images obtained by filtering the images are:
They are calculated with cv2.filter2D
fimg = cv2.filter2D(img, cv2.CV_64F, kernel)
Why the gabor filters with theta = 0 and theta = PI / 2.0 have a really different continuous component compared to the others?
It does not really make sense to me.
The reason was the PSI param that I set to 0. The problem is immediatly fixed is psi is kept at the default value PI/2.
I have a visualization output of gabor filter with 12 different orientations.I want to superimpose the vizualization image on my image of retina for vessel extraction.How do i do it?I have tried the below method.is there any other method to perform superimposition of images in matlab.
here is my code
I = getimage();
I=I(:,:,2);
lambda = 8;
theta = 0;
psi = [0 pi/2];
gamma = 0.5;
bw = 1;
N = 2;
img_in = im2double(I);
%img_in(:,:,2:3) = []; % discard redundant channels, it's gray anyway
img_out = zeros(size(img_in,1), size(img_in,2), N);
for n=1:N
gb = gabor_fn(bw,gamma,psi(1),lambda,theta)...
+ 1i * gabor_fn(bw,gamma,psi(2),lambda,theta);
% gb is the n-th gabor filter
img_out(:,:,n) = imfilter(img_in, gb, 'symmetric');
% filter output to the n-th channel
%theta = theta + 2*pi/N
%figure;
%imshow(img_out(:,:,n));
imshow(img_in); hold on;
h = imagesc(img_out(:,:,n)); % here i am getting error saying CDATA must be size[M*N]
set( h, 'AlphaData', .5 ); % .5 transparency
figure;
imshow(h);
theta = 15 * n; % next orientation
end
this is my original image
this is my visualized image got by gabor filter using orientation
this is the kind/type of image i have to get with respect to visualisation .i.e i have to impose visualized image on my original image and i have to get this type of image
With the information you have provided, my understanding is you want the third/final image to be an overlay on top of the first/initial image. I do things like this when using segmentation to detect hemorrhaging in MRI images of the brain.
First, let's set up some defintions:
I_src = source/original image
I_out = output/final image
Now, make a copy of I_src and make it a color image rather than grayscale.
I_hybrid = I_src
colorIm = gray2rgb(I_src)
Let's assume both I_src and I_out are the same visual dimensions (ie: width, height), and that I_out is strictly black-and-white (ie: monochrome). Now, we can use I_out as a mask template for alpha channel adjustments in the resulting image. This is where it gets fun.
BLACK=0;
WHITE=1;
[length width] = size(I_out);
for i = 1:1:length
for j = 1:1:width
if (I_out(i,j) == WHITE)
I_hybrid(i,j) = I_hybrid(i,j) + [0.25 0 0]a;
end
end
This will result in you getting your original image with the blood vessels in the eye being slightly brighter and tinted red. You now have a beautiful composite of your original image with the desired features highlighted, but not overwritten (ie: you can undo the highlighting by subtracting the original color vector).
I will include an example of what the output would look like, but it's noisy because I had to create it in GIMP as I don't have Matlab installed right now. The results will be similar, but yours would be much cleaner and prettier.
Please let me know how this goes.
References
"Converting Images from Grayscale to Color" http://blogs.mathworks.com/pick/2012/11/25/converting-images-from-grayscale-to-color/