生物学者の友人から、トカゲの鱗片を数えるプログラムを作るのを手伝ってもらえないかと尋ねられました(これは正しい訳ですか?)。
彼は私にいくつかの画像を送ってくれて、私はMatlabでいくつかのことを試しました. 一部の画像では、他の画像よりもはるかに困難です。たとえば、暗い (黒い) 領域がある場合などです。少なくとも私の方法では。ここで役に立つ助けが得られると確信しています。これをどのように改善すればよいですか?私は正しいアプローチを取りましたか?
これらは画像の一部です。
MATLAB を使用して Image Processing and Countingに従うことで、最良の結果が得られました。基本的に、画像を白黒に変換してから、しきい値を設定します。しかし、私は少し侵食を加えました。
コードは次のとおりです。
img0=imread('C:...\pic.png');
img1=rgb2gray(img0);
%The output image BW replaces all pixels in the input image with luminance greater than level with the value 1 (white) and replaces all other pixels with the value 0 (black). Specify level in the range [0,1].
img2=im2bw(img1,0.65);%(img1,graythresh(img1));
imshow(img2)
figure;
%erode
se = strel('line',6,0);
img2 = imerode(img2,se);
se = strel('line',6,90);
img2 = imerode(img2,se);
imshow(img2)
figure;
imshow(img1, 'InitialMag', 'fit')
% Make a truecolor all-green image. I use this later to overlay it on top of the original image to show which elements were counted (with green)
green = cat(3, zeros(size(img1)),ones(size(img1)), zeros(size(img1)));
hold on
h = imshow(green);
hold off
%counts the elements now defined by black spots on the image
[B,L,N,A] = bwboundaries(img2);
%imshow(img2); hold on;
set(h, 'AlphaData', img2)
text(10,10,strcat('\color{green}Objects Found:',num2str(length(B))))
figure;
%this produces a new image showing each counted element and its count id on top of it.
imshow(img2); hold on;
colors=['b' 'g' 'r' 'c' 'm' 'y'];
for k=1:length(B),
boundary = B{k};
cidx = mod(k,length(colors))+1;
plot(boundary(:,2), boundary(:,1), colors(cidx),'LineWidth',2);
%randomize text position for better visibility
rndRow = ceil(length(boundary)/(mod(rand*k,7)+1));
col = boundary(rndRow,2); row = boundary(rndRow,1);
h = text(col+1, row-1, num2str(L(row,col)));
set(h,'Color',colors(cidx),'FontSize',14,'FontWeight','bold');
end
figure;
spy(A);
そして、これらは結果の一部です。左上隅に、カウントされた数が表示されます。
また、カウントされた要素を緑色でマークすると、少なくともユーザーが手動でカウントする必要があるものを知ることができると便利だと思います。