画像の特定の領域にのみアルゴリズムを適用しようとしています。私は試しimfreehand
ましたが、少なくとも私にとっては、この関数を使用してそれを行うことはできませんでした.
では、コードを実行して操作を画像の特定の領域にのみ適用する方法はありMATLAB
ますか?
ありがとう。
「imroi」関数 (imfreehand と imellipse を含む) のいずれかで定義されたマスクを使用すると、roifilt2 を使用して、特定のフィルターまたは関数を使用して roi だけをフィルター処理できます。
まず、領域を定義します。
imshow(I); %display your image
h = imfreehand; % now pick the region
BW = createmask(h); %makes BW mask
次に、次のいずれかの方法で roifilt2 を使用します。
フィルターを定義して適用します。
H = fspecial('unsharp');
I2 = roifilt2(H,I,BW);`
与えられた関数をroiに適用します:
I2 = roifilt2(I, BW, 'histeq');
パラメータを指定して、特定の関数をROIに適用します。
fh = @(I)(histeq(I,5)); %define function
I2 = roifilt2(I, BW, fh);
最後は I2 = hist(I,5); の呼び出しと同じです。しかし、定義されたROIでのみ機能します。
到着予定時刻:
Roi で複数の関数を呼び出したい場合は、独自の関数を定義するのが最も簡単な場合があります。この関数は、画像入力 (およびオプションで他のパラメーター) を受け取り、適切なフィルター/関数を画像に適用し、最終的な画像を出力します。次に、上記の「histeq」と同じ方法で「myfunc」を呼び出します。
ロイポリを試すことができます。
SO hereの例があります。
次に例を示します。
img = imread('peppers.jpg'); % loads the image we want to use
[BW,xi,yi] = roipoly(img); % create a polynomial surrounding region
BW = repmat(uint8(BW),[1,1,3]); % create mask
selIMG = img.*BW; % apply mask to retain roi and set all else to 0
imview(selIMG)
se=strel('disk',3);
erosion=imerode(selIMG,se);
result_image=imsubtract(selIMG,erosion);
imview(result_image)
編集
侵食時: matlab docが説明してimerode
いるように、周囲のピクセルから最小値を選択します (imdilate
反対のことを行います)。これは、私の答えの元の処理がimerode
.
img = imread('peppers.jpg'); % loads the image we want to use
[BW,xi,yi] = roipoly(img); % logical mask which contains pixels of interest
nBW = uint8(~BW); % inverse of logical mask to pick surrounding pixels
surroundingMaxedOut = repmat(255*nBW,[1,1,3]); % set surrounding pixels to max value
nBW = repmat(nBW,[1,1,3]); % make mask with 3 channels to pick surrounding pixels
BW = repmat(uint8(BW),[1,1,3]); % make mask with 3 channels to handle RGB
selIMG = img.*BW; % pick the image region of interest
selIMG = selIMG + surroundingMaxedOut; % final selection with surrounding pixels maxed out
imview(selIMG) % inspect the selection
se=strel('disk',3);
erosion=imerode(selIMG,se); % apply erosion
finalIMG = img.*nBW + BW.*erosion; % insert eroded selection into the original image
imview(finalIMG)
他の回答が示すように、matlab にはこれらの操作を暗黙的に処理するルーチンがあり、特にメモリ管理の点でより効率的ですが、この例ではより多くの制御が提供されるため、何が起こっているかを確認できます。