2017-02-26 116 views

回答

2

這取決於圖像上的位,以及確定像素是綠色的標準。像素必須只有綠色,但沒有藍色或紅色?如果是這樣,這是一種方法。

開始通過加載圖像和分離顏色:

image = imread('your_image.jpg'); 
red = image(:,:,1); 
green = image(:,:,2); 
blue = image(:,:,3); 

然後,找到具有像素綠色的,但不是紅色或藍色:

only_green = green & ~(red | blue) 

如果你有一個綠色的定義不同像素,那麼你可以相應地改變第二步。

要將生成的矩陣顯示爲圖像,請使用imshow

1

爲了使事情更有趣,我建議以下解決方案:

  1. 從RGB轉換輸入圖像HSV。
  2. 標記HSV色彩空間中的綠色像素(在HSV色彩空間中,您可以選擇其他顏色,如黃色(不僅是原色:紅色,綠色,藍色))。

爲了強調環保,我設置多種顏色的灰度:

這裏是我的代碼:

RGB = imread('peppers.png'); 
HSV = rgb2hsv(RGB); %Convert RGB to HSV. 

figure;imshow(RGB);title('Original'); 

%Convert from range [0, 1] to [0, 255] (kind of more intuitive...) 
H = HSV(:, :, 1)*255; 
S = HSV(:, :, 2)*255; 
V = HSV(:, :, 3)*255; 

%Initialize to zeros.  
Green = zeros(size(H)); 

%Needed trial and error to find the correct range of green (after Google searching). 
Green(H >= 38 & H <=160 & S >= 50 & V >= 30) = 1; %Set green pixels to 1 

figure;imshow(Green);title('Mark Green as 1'); 

%Play with it a little... 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
Gray = rgb2gray(RGB); %Convert to gray-scale 

R = RGB(:, :, 1); 
G = RGB(:, :, 2); 
B = RGB(:, :, 3); 

Green = logical(Green); 

R(~Green) = Gray(~Green); 
G(~Green) = Gray(~Green); 
B(~Green) = Gray(~Green); 

RGB = cat(3, R, G, B); 

figure;imshow(RGB);title('Green and Gray'); 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

結果:

原圖:
enter image description here

Mark Green as 1:
enter image description here

綠色和灰色:
enter image description here

相關問題