2016-10-31 9 views
2

私はこのイメージがあるとします特定のRGBと2Dに最小ピクセルを見つける:Matlabの:

enter image description here

Iは、RGB =(0、80、41)と最小ピクセルを取得する必要があります。

bart = imread('bart.jpg'); 

% Find pixels that have red=0, g = 80 or b = 41 
r_find = find(bart(:, :, 1) == 0); 
g_find = find(bart(:, :, 2) == 80); 
b_find = find(bart(:, :, 3) == 41); 

% intersect pixels to find pixels that his color are (0,80,41) 
find_res = intersect(intersect(r_find, g_find), b_find); 

%find the minimum pixel (1D) 
pixel = min(find_res); 

はい、私はそれを創設しましたが、ピクセル(2D)のx、y座標はどのように取得できますか?

+1

私はあなたが何をすべきかを指定することがあると思いますあなたはwhithを意味する "最小p ixel "。あなたの最小2D座標は何ですか? – marcoresk

+0

hte fminimum(x、y)coordenates。左上の最初の緑色ピクセル – albert

+0

@marcoreskは言ったように、_x_と_y_を同時に最小化することはできません。 'x'と' y'の和を最小にしたいですか? 'x'と' y'のうち最大のものは? ...? –

答えて

1

あなたの間違いは、それぞれのカラーチャンネルに対してfindの操作を別々に使用しています。

[row, col] = find(((bart(:, :, 1) == 0) & (bart(:, :, 2) == 80) & (bart(:, :, 3) == 41)), 1) 

上記の例では、行は、第1の座標最小:

溶液が最初の条件を適用する簡単です。

あなたが列拳を最小限に抑える必要がある場合は、あなたがfindを適用する前に移調することができます

[col, row] = find([((bart(:, :, 1) == 0) & (bart(:, :, 2) == 80) & (bart(:, :, 3) == 41))]', 1) 

例により説明:

%Read input image 
RGB = imread('https://i.stack.imgur.com/2sRiY.jpg'); 

%Unpack RGB planes (just for example purpose - you don't need to do it). 
R = RGB(:, :, 1); 
G = RGB(:, :, 2); 
B = RGB(:, :, 3); 

%(R == 0) is a logical matrix with 1 where condition is true, and 0 where false. 
%Same think for (G == 80) and for (B == 41) 
figure;imshow(R == 0); %Same as imshow(RGB(:,:,1) == 0) 
figure;imshow(G == 80); 
figure;imshow(B == 41); 

画像:
R == 0を

enter image description here

G == 80
enter image description here

Bの== 41
enter image description here

%Now use AND operation between the three logical matrices: 
A = ((RGB(:, :, 1) == 0) & (RGB(:, :, 2) == 80) & (RGB(:, :, 3) == 41)); 

%A is a logical matrix with 1 where condition is true, and 0 where false. 
figure;imshow(A); 

画像A:
enter image description here

%The following operation minimize column first: 
%Find first index where A equals true (where value equals 1). 
[col, row] = find(A, 1); 

%In case you need to minimize the row first, you can transpose A: 
[row, col] = find(A', 1); 

%All operations in single statement: 
[row, col] = find(((RGB(:, :, 1) == 0) & (RGB(:, :, 2) == 80) & (RGB(:, :, 3) == 41)), 1); 
0

画像の対角線(poly2mask関数を使用)を描き、指定された値を持つ対角に沿った最初のピクセルを見つけることができます。

[rs ,cs,~] = size(bart); 
%create a mask image that has diagonal pixels value of 1 
mask = poly2mask([0 cs cs], [0 rs rs],rs, cs); 
%setting last argument to 1 to find the first pixel that meets the condition 
[r c] = find(mask & bart(:, :, 1) == 0 & bart(:, :, 2) == 80 & bart(:, :, 3) == 41,1) 

2つのコーナーポイントを使用して対角線の方程式を見つけ、ラインに沿ったピクセルのみで作業する方が効率的です。