2016-10-31 84 views
2

讓假設我有此圖像:Matlab的:找到在2D最小像素具有特定的RGB

enter image description here

我需要得到與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

我認爲你必須指定做什麼你的意思是「最小p」 ixel」。什麼是您的最小二維座標? – 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) 

上面的例子,最小化行第一座標。

如果你需要以最小化柱拳頭,可以施加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); 

圖片:
ř== 0

enter image description here

摹== 80
enter image description here

乙== 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) 

使用兩個角點找到對角線方程的效率更高,並且只能使用線上的像素。

相關問題