2014年9月8日 星期一

[Digital image processing] MATLAB中如何擷取特定顏色?

最近在研究一個色彩學上的問題

就是如何將特定顏色 (Ex. 藍色) 擷取出來

經過一段時間的查找

在StackOverFlow上找到了一篇相似的文章

How can I convert an RGB image to grayscale but keep one color?

該文中的問題是如何將非紅色的部分變成灰階影像(Grayscale)

利用文中的方法,將藍色的圖案給試出來

先附上第一版的程式 & 圖檔作為記憶

執行結果


MATLAB 程式碼
clear all;
clc;

% Import image
prt_img = imread('C:\Users\alex Hsu\Desktop\Printer_video\20140908_test_img.jpg'); % imread can show color image
%class(prt_img);

% Color detection
for y = 1:size(prt_img,1)
    for x = 1:size(prt_img,2)
        red   = prt_img( y, x, 1 );
        green = prt_img( y, x, 2 );
        blue  = prt_img(y, x, 3);
          if (blue < (red * 1.4) || blue < (green * 1.4) )
                avg = (red + green + blue) / 3;
                for z = 1:3
                    prt_img(y, x, z) = avg;
                end
        end
    end
end

附註 :

Wiki : HSL and HSV

HSV 相對於RGB的優勢在於顏色的組成只由單一顏色,而非像 RGB 是由Red Green Blue 三種顏色混合而成,比較容易將特定顏色擷取出
/* 載入prettify的autoloader */ /* 載入JQuery */