I'm trying to find Ending and Bifurcation points of a binary edge image produced from an ear image.
I'm writing the code based on the algorithm defined in [this paper] (http://elcvia.cvc.uab.es/article/view/108). I've implemented the part that introduced the formula to find endings and bifurcation. However, I don't get the correct result for different pictures, It's like this formula can't find some of the bifurcations and endings.
this is my code :
stats = regionprops(BW4 , 'PixelList');
[numofcomp,x] = size(stats);
BEcell = cell(numofcomp,2);
Ne = zeros(numofcomp,1);
Nb = zeros(numofcomp,1);
for i=1:1:numofcomp
contour = stats(i).PixelList;
S = size(contour);
b=0;
ee=0;
for j=1:1:S(1)
J = contour(j,1);
I = contour(j,2);
g1 = BW4(I,J+1) - 1;
g2 = BW4(I-1,J+1) - 1;
g3 = BW4(I-1,J) - 1;
g4 = BW4(I-1,J-1) - 1;
g5 = BW4(I,J-1) - 1;
g6 = BW4(I+1,J-1) - 1;
g7 = BW4(I+1,J) - 1;
g8 = BW4(I+1,J+1) - 1;
G1=g1*g2*g3;
G3=g3*g4*g5;
G5=g5*g6*g7;
G7=g7*g8*g1;
Nc = (g1 -G1) + (g3 -G3) + (g5 -G5) + (g7 -G7 );
if Nc < -2
b=b+1;
BEcell{i , 1}(j,1) = I;
BEcell{i , 1}(j,2) = J;
end
if Nc == -1
ee=ee+1;
BEcell{i , 2}(j,1) = I;
BEcell{i , 2}(j,2) = J;
end
end
Nb(i,1)=b;
Ne(i,1)=ee;
end
I don't know what it's wrong with it ? Have you ever seen this formula before?
also, I'm not sure about g9 there is no g9 in the matrix which is numbered by paper
EDIT
I used bwmorph :
Bifurcation = bwmorph(BW4 , 'branchpoints' ,Inf );
B=0;
for i = 1:1:Size(1)
for j = 1:1:Size(2)
if( Bifurcation(i,j)) %
B = B+1;
end
end
end
%----------------------------- Find End Points ----------------------------
Endpoint = bwmorph(can , 'endpoints' ,Inf );
E=0;
for i = 1:1:Size(1)
for j = 1:1:Size(2)
if( Endpoint(i,j))
E = E+1;
end
end
end
Related
This is my Approximate entropy Calculator in MATLAB. https://en.wikipedia.org/wiki/Approximate_entropy
I'm not sure why it isn't working. It's returning a negative value.Can anyone help me with this? R1 being the data.
FindSize = size(R1);
N = FindSize(1);
% N = input ('insert number of data values');
%if you want to put your own N in, take away the % from the line above
and
%insert the % before the N = FindSize(1)
%m = input ('insert m: integer representing length of data, embedding
dimension ');
m = 2;
%r = input ('insert r: positive real number for filtering, threshold
');
r = 0.2*std(R1);
for x1= R1(1:N-m+1,1)
D1 = pdist2(x1,x1);
C11 = (D1 <= r)/(N-m+1);
c1 = C11(1);
end
for i1 = 1:N-m+1
s1 = sum(log(c1));
end
phi1 = (s1/(N-m+1));
for x2= R1(1:N-m+2,1)
D2 = pdist2(x2,x2);
C21 = (D2 <= r)/(N-m+2);
c2 = C21(1);
end
for i2 = 1:N-m+2
s2 = sum(log(c2));
end
phi2 = (s2/(N-m+2));
Ap = phi1 - phi2;
Apen = Ap(1)
Following the documentation provided by the Wikipedia article, I developed this small function that calculates the approximate entropy:
function res = approximate_entropy(U,m,r)
N = numel(U);
res = zeros(1,2);
for i = [1 2]
off = m + i - 1;
off_N = N - off;
off_N1 = off_N + 1;
x = zeros(off_N1,off);
for j = 1:off
x(:,j) = U(j:off_N+j);
end
C = zeros(off_N1,1);
for j = 1:off_N1
dist = abs(x - repmat(x(j,:),off_N1,1));
C(j) = sum(~any((dist > r),2)) / off_N1;
end
res(i) = sum(log(C)) / off_N1;
end
res = res(1) - res(2);
end
I first tried to replicate the computation shown the article, and the result I obtain matches the result shown in the example:
U = repmat([85 80 89],1,17);
approximate_entropy(U,2,3)
ans =
-1.09965411068114e-05
Then I created another example that shows a case in which approximate entropy produces a meaningful result (the entropy of the first sample is always less than the entropy of the second one):
% starting variables...
s1 = repmat([10 20],1,10);
s1_m = mean(s1);
s1_s = std(s1);
s2_m = 0;
s2_s = 0;
% datasample will not always return a perfect M and S match
% so let's repeat this until equality is achieved...
while ((s1_m ~= s2_m) && (s1_s ~= s2_s))
s2 = datasample([10 20],20,'Replace',true,'Weights',[0.5 0.5]);
s2_m = mean(s2);
s2_s = std(s2);
end
m = 2;
r = 3;
ae1 = approximate_entropy(s1,m,r)
ae2 = approximate_entropy(s2,m,r)
ae1 =
0.00138568170752751
ae2 =
0.680090884817465
Finally, I tried with your sample data:
fid = fopen('O1.txt','r');
U = cell2mat(textscan(fid,'%f'));
fclose(fid);
m = 2;
r = 0.2 * std(U);
approximate_entropy(U,m,r)
ans =
1.08567461184858
am working on word image i want to find the center of mass in each segments.
clear all;
close all;
a = imread('i2e.jpg');
figure;imshow(a);
% Normalizatoin
b = imresize(a,[50,50]);
figure;imshow(b);
% Binarization
th = graythresh(b);
a = im2bw(b,th);
figure,imshow(a);
w = 5;
h = 5;
c = 50/h;
r = 50/w;
cellA = cell(w,h);
% apply grid basid on rows and column
a(r:c:end,:,:) = 0;
a(:,r:c:end,:) = 0;
figure,imshow(a);
count = 0;
for p1 =1:50
for q1 =1:50
r1 = p1 + r;
c1 = q1 + c;
if(p1 < 50 || q1 < 50)
count = 0;
for i = 1 : r
for j = 1 : c
r1 = i + r;
c1 = j + c;
if(a(i,j) == 0)
count = count + 1;
end
end
end
end
end
end
count;
am not getting proper results please help me to find the center of mass in each segments
reference image
I have written a small Matlab funcion which takes an image in RGB and converts it to HSV according to the conversion formulas found here.
The problem is that when I apply this to a color spectrum there is a cut in the spectrum and some values are wrong, see images (to make the comparison easier I have used the internal hsv2rgb() function to convert back to RGB. This does not happen with Matlabs own function rgb2hsv() but I can not find what I have done wrong.
This is my function
function [ I_HSV ] = RGB2HSV( I_RGB )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
[MAX, ind] = max(I_RGB,[],3);
if max(max(MAX)) > 1
I_r = I_RGB(:,:,1)/255;
I_g = I_RGB(:,:,2)/255;
I_b = I_RGB(:,:,3)/255;
MAX = max(cat(3,I_r, I_g, I_b),[],3);
else
I_r = I_RGB(:,:,1);
I_g = I_RGB(:,:,2);
I_b = I_RGB(:,:,3);
end
MIN = min(cat(3,I_r, I_g, I_b),[],3);
d = MAX - MIN;
I_V = MAX;
I_S = (MAX - MIN) ./ MAX;
I_H = zeros(size(I_V));
a = 1/6*mod(((I_g - I_b) ./ d),1);
b = 1/6*(I_b - I_r) ./ d + 1/3;
c = 1/6*(I_r - I_g) ./ d + 2/3;
H = cat(3, a, b, c);
for m=1:size(H,1);
for n=1:size(H,2);
if d(m,n) == 0
I_H(m,n) = 0;
else
I_H(m,n) = H(m,n,ind(m,n));
end
end
end
I_HSV = cat(3,I_H,I_S,I_V);
end
Original spectrum
Converted spectrum
The error was in my simplification of the calculations of a, b, and c. Changing it to the following solved the problem.
function [ I_HSV ] = RGB2HSV( I_RGB )
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
[MAX, ind] = max(I_RGB,[],3);
if max(max(MAX)) > 1
I_r = I_RGB(:,:,1)/255;
I_g = I_RGB(:,:,2)/255;
I_b = I_RGB(:,:,3)/255;
MAX = max(cat(3,I_r, I_g, I_b),[],3);
else
I_r = I_RGB(:,:,1);
I_g = I_RGB(:,:,2);
I_b = I_RGB(:,:,3);
end
MIN = min(cat(3,I_r, I_g, I_b),[],3);
D = MAX - MIN;
I_V = MAX;
I_S = D ./ MAX;
I_H = zeros(size(I_V));
a = 1/6*mod(((I_g - I_b) ./ D),6);
b = 1/6*((I_b - I_r) ./ D + 2);
c = 1/6*((I_r - I_g) ./ D + 4);
H = cat(3, a, b, c);
for m=1:size(H,1);
for n=1:size(H,2);
if D(m,n) == 0
I_H(m,n) = 0;
else
I_H(m,n) = H(m,n,ind(m,n));
end
if MAX(m,n) == 0
S(m,n) = 0;
end
end
end
I_HSV = cat(3,I_H,I_S,I_V);
end
I'm trying to perform content-based image retrieval (CBIR) using k-means clustering. I use the PCA function princomp() with a feature vector length of 190.
I have 500 test images in color taken from here. There's 5 categories in total. When I run my code I only get 3 clusters and the images look very different. What am I doing wrong?
Here is my code:
% construction of feature vector
set = [hsvHist autoCorrelogram Moments_Couleur meanAmplitude msEnergy OndelettesMoments];
% add name of image
dataset(k, :) = [set str2double(name)];
handlse.CaracVt = dataset.';
dlmwrite('f:/hellonewday', handlse.CaracVt);
% ACP function
[COEFF, SCORE, latent] = princomp(handlse.CaracVt());
laten = cumsum(latent)./sum(latent)
list = []; o = 1; c = 0;
for kn = 1:length(laten)
if (isempty(list))
list(o, :) = laten(kn);
o = o + 1;
else
for i = 1:length(list)
kki = abs(laten(kn) - list(i));
if (kki > 0.006)
c = c + 1;
end;
end;
if (c == length(list))
list(o, :) = laten(kn);
o = o + 1;
end;
end;
c = 0;
end;
handlse.NmbreCluster = length(list);
disp('The amount of clusters is: ');
disp(handlse.NmbreCluster);
handlse.CaracVt = handlse.CaracVt.';
mat = handlse.CaracVt;
mat(:, end) = [];
[kMeansClusters, c] = kmeans(mat, handlse.NmbreCluster);
dlmwrite('f:/clusters', kMeansClusters);
dlmwrite('f:/centres', c);
disp('kMeansClusters for each image: ');
disp(kMeansClusters);
c = c.';
ko = 1;
for i = 1:handlse.NmbreCluster
array = zeros(1, 191);
c(i, 191) = 0;
array(:, 1) = c(i);
for kp = 1:length(kMeansClusters)
if (i == kMeansClusters(kp))
ko = ko + 1;
array(ko, :) = handlse.CaracVt(kp, :);
end
end;
myArray{i} = array;
ko = 1;
end;
disp(myArray);
uisave('myArray', 'dataset');
I have a vector. I want to remove outliers. I got bin and no of values in that bin. I want to remove all points based on the number of elements in each bin.
Data:
d1 =[
360.471912914169
505.084636471948
514.39429429184
505.285068055647
536.321181755858
503.025854206322
534.304229816684
393.387035881967
396.497969729985
520.592172434431
421.284713703215
420.401106087984
537.05330275495
396.715779872694
514.39429429184
404.442344469518
476.846474245118
599.020867750031
429.163139144079
514.941744277933
445.426761656729
531.013596812737
374.977332648255
364.660115724218
538.306752697753
519.042387479096
1412.54699036882
405.571202133485
516.606049132218
2289.49623498271
378.228766753667
504.730621222846
358.715764917016
462.339366699398
512.429858614816
394.778786157514
366
498.760463549388
366.552861126468
355.37022947906
358.308526273099
376.745272034036
366.934599077274
536.0901883079
483.01740134285
508.975480745389
365.629593988233
536.368800360349
557.024236456548
366.776498701866
501.007025898839
330.686029339009
508.395475983019
429.563732174866
2224.68806802212
534.655786464525
518.711297351426
534.304229816684
514.941744277933
420.32368479542
367.129404978681
525.626188464768
388.329756778952
1251.30895065927
525.626188464768
412.313764019587
513.697381733643
506.675438520558
1517.71183364959
550.276294237722
543.359917550053
500.639590923451
395.129864728041];
Histogram computation:
[nelements,centers] = hist(d1);
nelements=55 13 0 0 1 1 1 0 0 2
I want to remove all points apearing less than 5 (in nelements). It means only first 2 elements in nelements( 55, 13 ) remains.
Is there any function in matlab.
You can do it along these lines:
threshold = 5;
bin_halfwidth = (centers(2)-centers(1))/2;
keep = ~any(abs(bsxfun(#minus, d1, centers(nelements<threshold))) < bin_halfwidth , 2);
d1_keep = d1(keep);
Does this do what you want?
binwidth = centers(2)-centers(1);
centersOfRemainingBins = centers(nelements>5);
remainingvals = false(length(d1),1);
for ii = 1:length(centersOfRemainingBins )
remainingvals = remainingvals | (d1>centersOfRemainingBins (ii)-binwidth/2 & d1<centersOfRemainingBins (ii)+binwidth/2);
end
d_out = d1(remainingvals);
I don't know Matlab function for this problem, but I think, that function with follow code is what are you looking for:
sizeData = size(data);
function filter_hist = filter_hist(data, binCountRemove)
if or(max(sizeData) == 0, binCountRemove < 1)
disp('Error input!');
filter_hist = [];
return;
end
[n, c] = hist(data);
sizeN = size(n);
intervalSize = c(2) - c(1);
if sizeData(1) > sizeData(2)
temp = transpose(data);
else
temp = data;
end
for i = 1:1:max(sizeN)
if n(i) < binCountRemove
a = c(i) - intervalSize / 2;
b = c(i) + intervalSize / 2;
sizeTemp = size(temp);
removeInds = [];
k = 0;
for j = 1:1:max(sizeTemp)
if and(temp(j) > a, less_equal(temp(j), b) == 1)
k = k + 1;
removeInds(k) = j;
end
end
temp(removeInds) = [];
end
end
filter_hist = transpose(temp);
%Determines when 'a' less or equal to 'b' by accuracy
function less_equal = less_equal(a, b)
delta = 10^-6; %Accuracy
if a < b
less_equal = 1;
return;
end
if abs(b - a) < delta
less_equal = 1;
return;
end
less_equal = 0;
You can do something like this
nelements=nelements((nelements >5))