2

私は画像を撮り、その上に輪郭を描いています。ピクセル数とそれらの位置も 3 つのカテゴリでカウントする必要があります (MATLAB)

  1. 曲線の外側にあるピクセル
  2. 曲線の内側にあるピクセル
  3. 曲線の境界にあるピクセル。

MATLAB で inpolygon を使用してみました。内側と外側のピクセルをカウントできますが、境界ではカウントできません。境界では、小さなグリッドの中心を直接通過するもののみがカウントされます。輪郭が小さなグリッドの 4 つのエッジのいずれかを通過するピクセルもカウントする必要があります。

助けてください。以下のコードを提供しました。

%Polygon Plotting 
clc;clear all;close all;
I = imread('cameraman.tif');
I = imresize(I,[100 100]);
I = double(I(:,:,1));
imagesc(I,[0 255]);colormap(gray);
axis([1 size(I,1) 1 size(I,2)]);
[BW xi yi] = roipoly();  %select your own coordinates. 
X=xi;
Y=yi;
hold on;
contour(BW,'r');
hold off;
xa = 1 : size(I,2);
ya = 1 : size(I,1);
[x,y] = meshgrid(xa,ya);
[in on] = inpolygon(x,y,X,Y);
count1 = sum(sum(in));
count2 = size(I,1)*size(I,2) - count1; 
count3 = sum(sum(on));
%count1 = inside the polygon and on boundary
%count2 = outside the polygon
%count3 = on the boundary only
inside = zeros(count1,2);
outside = zeros(count2,2);
onthecurve = zeros(count3,2);
l=1;m=1;n=1;

    for i = 1:size(I,1)
        for j = 1:size(I,2)
            if in(i,j)==1
                inside(l,1)=i;
                inside(l,2)=j;
                l=l+1;
            end
            if in(i,j)==0
                outside(m,1)= i;
                outside(m,2)= j;
                m = m+1;
            end
            if on(i,j)==1
                onthecurve(n,1)= i;
                onthecurve(n,2)= j;
                n = n+1;
            end
        end
    end
figure,    
plot(inside(:,1),inside(:,2),'+g');
axis([1 size(I,1) 1 size(I,2)]);
hold on 
plot(outside(:,1),outside(:,2),'+r');
hold on
plot(onthecurve(:,1),onthecurve(:,2),'+b');
hold off

画像が正しく表示されない場合は、リンクを参照してください: 1.元の画像と輪郭 2.緑 - 内側、赤 - 外側

ご覧のとおり、輪郭上の点は青色でマークされていません。実際、count3 はほとんど常に出力 0 を返します。そのため、inpolygon は境界上のポイントをカウントするのにあまり効率的ではありません。

それらのピクセルもカウントするようにコードを変更するにはどうすればよいですか? みんなありがとう。

4

1 に答える 1

1

を使用して、白黒( ) 画像 (この場合は入力ポリゴン)edgeの境界を検出できます。BW

%% Polygon Plotting
I = imread('cameraman.tif');
imshow(I);
inBW = roipoly(); % Select your own coordinates.

%% Make BW matrices
outBW = ~inBW; % Find 'out'
edBW = edge(inBW); % Find the 'edge'
inBW(edBW) = 0; % Remove edge from 'in'
outBW(edBW) = 0; % Remove edge from 'out'

%% Show result
imshow(double(cat(3, inBW, edBW, outBW)))

また、すべてのピクセルが 3 つのセットに含まれていることを示します。

prod(size(I)) - sum(sum(inBW + outBW + edBW))

ゼロになるはずです。

それが役に立てば幸い。

于 2013-07-04T19:48:34.980 に答える