﻿ c# – 图像分析 – 光纤识别 - 代码日志

#### c# – 图像分析 – 光纤识别

PS：我不知道在哪里提出答案,所以我把它放在StackOverflow上.

@Nakilon解决方案

``````%# read image

II = I;
for k=1:size(II,3)
mn = min(min( II(:,:,k) )); mx = max(max( II(:,:,k) ));
II(:,:,k) = ( II(:,:,k) - mn ) ./ (mx-mn);
end

%# Sharpen[]
II = imfilter(II, fspecial('unsharp'));

%# MinDetect[], MaxDetect[]
II = rgb2gray(II);
mn = imextendedmin(II,0.3,8);
mx = imextendedmax(II,0.7,8);

%# pad image because Mathematica handles border cases differently than MATLAB

q = medfilt2(q, [5 5]*2+1, 'symmetric');                 %# MedianFilter[]
q = ordfilt2(q, 1, ones(2*5+1), 'symmetric');            %# MinFilter[]
q = ordfilt2(q, (25*2+1)^2, ones(25*2+1), 'symmetric');  %# MaxFilter[]
q = ordfilt2(q, 1, ones(20*2+1), 'symmetric');           %# MinFilter[]

a = imsubtract(mn,q)==1;    %# a = mn; a(q) = false;
b = immultiply(mx,q);       %# b = mx & q;
c = imadd(a,b);             %# c = a | b;

%# show images
figure(1)
subplot(121), imshow(mn)
subplot(122), imshow(mx)
figure(2), imshow(q)
figure(3)
subplot(121), imshow(a)
subplot(122), imshow(b)
figure(4), imshow(c)
``````

At the edges of an image, MedianFilter/MinFilter/MaxFilter uses smaller neighborhoods.

@belisarius解决方案

``````%# read image

%# LaplacianGaussianFilter[]
II = imfilter( I , fspecial('log', [2 2]*2+1, (2*2+1)/2) );

for k=1:size(II,3)
mn = min(min( II(:,:,k) )); mx = max(max( II(:,:,k) ));
II(:,:,k) = ( II(:,:,k) - mn ) ./ (mx-mn);
end

%# Binarize[]
BW = im2bw(II, 0.6);

%# DeleteSmallComponents[]
BW = bwareaopen(BW, 2, 8);

%# show images
figure
subplot(121), imshow(BW)
subplot(122), imshow( imoverlay(I,BW,[0 1 0]) )
``````