2016-10-31 4 views
2

suppose Lets Ich habe dieses Bild:Matlab: Finden Sie minimale Pixel in 2D mit spezifischer rgb

enter image description here

Ich brauche das minimale Pixel mit rgb erhalten = (0, 80, 41). das Pixel finden ich tun würde:

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); 

Ja, ich habe es gegründet, aber wie kann ich erhalten x, y-Koordinaten des Pixels (2D)?

+1

Ich denke, müssen Sie angeben, was zu tun du meinst mit "minimum p ixel ". Was sind minimale 2D Koordinaten für dich? – marcoresk

+0

hte fminimum (x, y) koordiniert. Es wäre das erste grüne Pixel oben links. – albert

+0

Wie @marcoresk sagte, können Sie _x_ und _y_ nicht gleichzeitig minimieren. Möchten Sie die Summe von 'x' und' y' minimieren? Der größte von 'x' und' y'? ...? –

Antwort

1

Ihr Fehler verwendet find Operation für jeden Farbkanal separat.

Die Lösung ist einfach, den Zustand der Anwendung zuerst:

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

obiges Beispiel minimieren Reihe ersten Koordinate.

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

Erklären Sie mit gutem Beispiel:

Im Fall, dass Sie die Spalte Faust zu minimieren, können Sie vor find Anwendung transponieren

%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); 

Bilder:
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); 

Bild 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

Sie können eine Diagonale des Bildes zeichnen (mit der Funktion poly2mask) und das erste Pixel entlang der Diagonale mit dem angegebenen Wert finden.

[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) 

Je effizienter ist es, die Gleichung der diagonalen Linie mit zwei Eckpunkten zu finden und nur mit Pixeln entlang der Linie zu arbeiten.