calling a function from the same folder within script
조회 수: 19 (최근 30일)
이전 댓글 표시
this is my fingervein region extraction code
function [region, edges] = region(img, mask_h, mask_w)
[img_h, img_w] = size(img);
% Determine lower half starting point
if mod(img_h,2) == 0
half_img_h = img_h/2 + 1;
else
half_img_h = ceil(img_h/2);
end
% Construct mask for filtering
mask = zeros(mask_h,mask_w);
mask(1:mask_h/2,:) = -1;
mask(mask_h/2 + 1:end,:) = 1;
% Filter image using mask
img_filt = imfilter(img, mask,'replicate');
%figure; imshow(img_filt)
% Upper part of filtred image
img_filt_up = img_filt(1:floor(img_h/2),:);
[~, y_up] = max(img_filt_up);
% Lower part of filtred image
img_filt_lo = img_filt(half_img_h:end,:);
[~,y_lo] = min(img_filt_lo);
% Fill region between upper and lower edges
region = zeros(size(img));
for i=1:img_w
region(y_up(i):y_lo(i)+size(img_filt_lo,1), i) = 1;
end
% Save y-position of finger edges
edges = zeros(2,img_w);
edges(1,:) = y_up;
edges(2,:) = round(y_lo + size(img_filt_lo,1));
%stord in the fvr.m within the folder(maximum curve)
%%%following code is to find the maximum curvature
function veins = max_curvature(img, fvr, sigma)
winsize = ceil(4*sigma);
[X,Y] = meshgrid(-winsize:winsize, -winsize:winsize);
h = (1/(2*pi*sigma^2)).*exp(-(X.^2 + Y.^2)/(2*sigma^2));
hx = (-X/(sigma^2)).*h;
hxx = ((X.^2 - sigma^2)/(sigma^4)).*h;
hy = hx';
hyy = hxx';
hxy = ((X.*Y)/(sigma^4)).*h;
% Do the actual filtering
fx = imfilter(img, hx, 'replicate', 'conv');
fxx = imfilter(img, hxx, 'replicate', 'conv');
fy = imfilter(img, hy, 'replicate', 'conv');
fyy = imfilter(img, hyy, 'replicate', 'conv');
fxy = imfilter(img, hxy, 'replicate', 'conv');
f1 = 0.5*sqrt(2)*(fx + fy); % \
f2 = 0.5*sqrt(2)*(fx - fy); % /
f11 = 0.5*fxx + fxy + 0.5*fyy; % \\
f22 = 0.5*fxx - fxy + 0.5*fyy; % //
[img_h, img_w] = size(img); % Image height and width
%% Calculate curvatures
k = zeros(img_h, img_w, 4);
k(:,:,1) = (fxx./((1 + fx.^2).^(3/2))).*fvr; % hor
k(:,:,2) = (fyy./((1 + fy.^2).^(3/2))).*fvr; % ver
k(:,:,3) = (f11./((1 + f1.^2).^(3/2))).*fvr; % \
k(:,:,4) = (f22./((1 + f2.^2).^(3/2))).*fvr; % /
%% Scores
%V = zeros(img_h, img_w, 4);
Vt = zeros(img_h, img_w, 4);
Wr = 0;
% Horizontal direction
bla = k(:,:,1) > 0;
for y=1:img_h
for x=1:img_w
if(bla(y,x))
Wr = Wr + 1;
end
if ( Wr > 0 && (x == img_w || ~bla(y,x)) )
if (x == img_w)
% Reached edge of image
pos_end = x;
else
pos_end = x - 1;
end
pos_start = pos_end - Wr + 1; % Start pos of concave
[~, I] = max(k(y, pos_start:pos_end,1));
pos_max = pos_start + I - 1;
Scr = k(y,pos_max,1)*Wr;
%V(y,pos_max,1) = V(y,pos_max,1) + Scr;
Vt(y,pos_max) = Vt(y,pos_max) + Scr;
Wr = 0;
end
end
end
% Vertical direction
bla = k(:,:,2) > 0;
for x=1:img_w
for y=1:img_h
if(bla(y,x))
Wr = Wr + 1;
end
if ( Wr > 0 && (y == img_h || ~bla(y,x)) )
if (x == img_h)
% Reached edge of image
pos_end = y;
else
pos_end = y - 1;
end
pos_start = pos_end - Wr + 1; % Start pos of concave
[~, I] = max(k(pos_start:pos_end,x,2));
pos_max = pos_start + I - 1;
Scr = k(pos_max,x,2)*Wr;
%V(pos_max,x,2) = V(pos_max,x,2) + Scr;
Vt(pos_max,x) = Vt(pos_max,x) + Scr;
Wr = 0;
end
end
end
% Direction: \
bla = k(:,:,3) > 0;
for start=1:(img_w+img_h-1)
% Initial values
if (start <= img_w)
x = start;
y = 1;
else
x = 1;
y = start - img_w + 1;
end
done = false;
while ~done
if(bla(y,x))
Wr = Wr + 1;
end
if ( Wr > 0 && (y == img_h || x == img_w || ~bla(y,x)) )
if (y == img_h || x == img_w)
% Reached edge of image
pos_x_end = x;
pos_y_end = y;
else
pos_x_end = x - 1;
pos_y_end = y - 1;
end
pos_x_start = pos_x_end - Wr + 1;
pos_y_start = pos_y_end - Wr + 1;
%d = diag(k(pos_y_start:pos_y_end, pos_x_start:pos_x_end, 3));
% More efficient implementation than diag(..)
d = k(((pos_x_start-1)*img_h + pos_y_start + 2*img_w*img_h):(img_h + 1):((pos_x_end-1)*img_h + pos_y_end + 2*img_w*img_h));
[~, I] = max(d);
pos_x_max = pos_x_start + I - 1;
pos_y_max = pos_y_start + I - 1;
Scr = k(pos_y_max,pos_x_max,3)*Wr;
%V(pos_y_max,pos_x_max,3) = V(pos_y_max,pos_x_max,3) + Scr;
Vt(pos_y_max,pos_x_max) = Vt(pos_y_max,pos_x_max) + Scr;
Wr = 0;
end
if((x == img_w) || (y == img_h))
done = true;
else
x = x + 1;
y = y + 1;
end
end
end
% Direction: /
bla = k(:,:,4) > 0;
for start=1:(img_w+img_h-1)
% Initial values
if (start <= img_w)
x = start;
y = img_h;
else
x = 1;
y = img_w+img_h-start;
end
done = false;
while ~done
if(bla(y,x))
Wr = Wr + 1;
end
if ( Wr > 0 && (y == 1 || x == img_w || ~bla(y,x)) )
if (y == 1 || x == img_w)
% Reached edge of image
pos_x_end = x;
pos_y_end = y;
else
pos_x_end = x - 1;
pos_y_end = y + 1;
end
pos_x_start = pos_x_end - Wr + 1;
pos_y_start = pos_y_end + Wr - 1;
%d = diag(flipud(k(pos_y_end:pos_y_start, pos_x_start:pos_x_end, 4)));
% More efficient implementation than diag(flipud(..))
d = k(((pos_x_start-1)*img_h + pos_y_start + 3*img_w*img_h):(img_h - 1):((pos_x_end-1)*img_h + pos_y_end + 3*img_w*img_h));
[~, I] = max(d);
pos_x_max = pos_x_start + I - 1;
pos_y_max = pos_y_start - I + 1;
Scr = k(pos_y_max,pos_x_max,4)*Wr;
%V(pos_y_max,pos_x_max,4) = V(pos_y_max,pos_x_max,4) + Scr;
Vt(pos_y_max,pos_x_max) = Vt(pos_y_max,pos_x_max) + Scr;
Wr = 0;
end
if((x == img_w) || (y == 1))
done = true;
else
x = x + 1;
y = y - 1;
end
end
end
%Vt = V(:,:,1) + V(:,:,2) + V(:,:,3) + V(:,:,4);
%% Connection of vein centres
Cd = zeros(img_h, img_w, 4);
for x=3:img_w-3
for y=3:img_h-3
Cd(y,x,1) = min(max(Vt(y,x+1), Vt(y,x+2)) ,...
max(Vt(y,x-1), Vt(y,x-2))); % Hor
Cd(y,x,2) = min(max(Vt(y+1,x), Vt(y+2,x)) ,...
max(Vt(y-1,x), Vt(y-2,x))); % Vert
Cd(y,x,3) = min(max(Vt(y-1,x-1),Vt(y-2,x-2)),...
max(Vt(y+1,x+1),Vt(y+2,x+2))); % \
Cd(y,x,4) = min(max(Vt(y+1,x-1),Vt(y+2,x-2)),...
max(Vt(y-1,x+1),Vt(y-2,x+2))); % /
end
end
veins = max(Cd,[],3);
% %% Plot results
% figure('Name', 'Second order derivatives');
% subplot(2,2,1);
% imshow(fxx, []);
% title('Horizontal');
% subplot(2,2,2);
% imshow(fyy, []);
% title('Vertical');
% subplot(2,2,3);
% imshow(f11, []);
% title('\');
% subplot(2,2,4);
% imshow(f22, []);
% title('/');
%
% figure('Name', 'Curvatures');
% subplot(2,2,1);
% %imshow(log(k(:,:,1) + 1), []);
% imshow(k(:,:,1) > 0, []);
% title('Horizontal');
% subplot(2,2,2);
% %imshow(log(k(:,:,2) + 1), []);
% imshow(k(:,:,2) > 0, []);
% title('Vertical');
% subplot(2,2,3);
% %imshow(log(k(:,:,3) + 1), []);
% imshow(k(:,:,3) > 0, []);
% title('\');
% subplot(2,2,4);
% %imshow(log(k(:,:,4) + 1), []);
% imshow(k(:,:,4) > 0, []);
% title('/');
%
% figure('Name', 'Scores');
% subplot(2,2,1);
% imshow(V(:,:,1));
% title('Horizontal');
% subplot(2,2,2);
% imshow(V(:,:,2));
% title('Vertical');
% subplot(2,2,3);
% imshow(V(:,:,3));
% title('\');
% subplot(2,2,4);
% imshow(V(:,:,3));
% title('/');
the above file is sotred in final_max_curvv.m within the folder maximum_curv
main script is:
clc;
clear all;
imtool close all;
clear;
img=im2double(imread('https://www.researchgate.net/profile/Yu_Lu17/publication/271552773/figure/fig1/AS:392237897797643@1470528210555/Finger-vein-image-samples-from-different-Universities-All-these-images-are-randomly.png')); % Read image
img = imresize(img, 0.5); % Downscale image
mask_h=4; % Height of the mask
mask_w=20; % Width of the mask
[fvr, edges] = region(img, mask_h, mask_w);
% Create a nice image for showing the edges
edge_img = zeros(size(img));
edge_img(edges(1,:) + size(img,1)*[0:size(img,2)-1]) = 1;
edge_img(edges(2,:) + size(img,1)*[0:size(img,2)-1]) = 1;
rgb = zeros([size(img) 3]);
rgb(:,:,1) = (img - img.*edge_img) + edge_img;
rgb(:,:,2) = (img - img.*edge_img);
rgb(:,:,3) = (img - img.*edge_img);
%fvr = region(img,4,40);
sigma = 3; % Parameter
v_max_curvature = maximum_curvature(img,fvr,sigma);
md = median(v_max_curvature(v_max_curvature>0));
v_max_curvature_bin = v_max_curvature > md;
% Overlay the extracted veins on the original image
overlay_max_curvature = zeros([size(img) 3]);
overlay_max_curvature(:,:,1) = img;
overlay_max_curvature(:,:,2) = img + 0.4*v_max_curvature_bin;
overlay_max_curvature(:,:,3) = img;
figure;
subplot(3,2,1)
imshow(img,[])
title('Original captured image')
subplot(3,2,2)
imshow(fvr)
title('Detected finger region')
subplot(3,2,3)
imshow(rgb)
title('Detected finger edge')
subplot(3,2,3)
imshow(v_max_curvature_bin)
title('Binarised veins extracted by maximum curvature method')
subplot(3,2,4)
imshow(overlay_max_curvature)
title('Maximum curvature method')
subplot(3,2,5)
imshow()
title('')
subplot(3,2,6)
imshow(overlay_repeated_line)
title('Repeated line tracking method')
the above code is stored in the maximum_curve folder with the name of final_max_curve
ERROR DISPLAYING:
Error in final_max_curvv (line 10)
[fvr, edges] = region(img, mask_h, mask_w);
댓글 수: 0
채택된 답변
Walter Roberson
2020년 7월 16일
[img_h, img_w] = size(img);
That line is wrong. You are reading in in RGB image, so it is 3 dimensional. When you use fewer outputs for a size() call than the array has dimensions, then the final output (in this case img_w) will be assigned the product of all remaining dimensions. The array is 192 x 256 x 3, so img_h will be assigned 192, and img_w will be assigned (256 * 3) = 768.
Your code then proceeds to process the input image as if it were an array with size(img,3) = 3 times as many columns as it really has, so you get an array that has 768 columns on output. You then try to do computation between that array of width 768 and the original image of width 256.
I am sure that when you displayed the image that it looked like a grayscale image, but it is an RGB image that happens to only use shades of gray.
댓글 수: 2
Walter Roberson
2020년 7월 16일
After
img=im2double(imread('https://www.researchgate.net/profile/Yu_Lu17/publication/271552773/figure/fig1/AS:392237897797643@1470528210555/Finger-vein-image-samples-from-different-Universities-All-these-images-are-randomly.png')); % Read image
add
img = rgb2gray(img);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Read, Write, and Modify Image에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!