https://www.sigarmas.com/deteksi-wajah-secara-realtime-menggunakan-matlab/ https://www.sigarmas.com/deteksi-wajah-secara-realtime-menggunakan-matlab/

Deteksi Warna RGB Menggunakan Matlab Secara Realtime

Deteksi Warna RGB Menggunakan Matlab – Assalamualaikum, selamat datang di blog yang sederhana ini. Kali ini saya akan membagikan program untuk mendeteksi warna RGB dengan menggunakan Matlab secara real time dengan menggunakan kamera.

Program ini akan mendeteksi warna RGB secara otomatis dengan objek yang terlihat. Jadi, cara kerjanya adlah kita akan memperlihatkan objek yang berwarna RGB berapa saja, maka program akan mendeteksi warna dan jumlah warna yang ada.

PENTING, sebelum anda menjalankan program ini ada beberapa file tambahan yang haeus anda instal di Matlab yaitu menginstal plugin add-on di matlab melalui menu addon.

Langkah-langkah Deteksi Warna RGB Menggunakan Matlab

Berikut adalah langkah-langkah umum yang dapat digunakan untuk melakukan deteksi warna RGB menggunakan Matlab:

  1. Membaca citra atau video yang akan dideteksi warnanya menggunakan fungsi imread() atau VideoReader().
  2. Mengubah citra ke dalam format RGB menggunakan fungsi rgb2gray() atau im2double().
  3. Menentukan batasan nilai intensitas warna R, G, dan B yang akan digunakan sebagai kriteria deteksi warna.
  4. Memfilter citra dengan menggunakan batasan nilai intensitas warna yang telah ditentukan.
  5. Menghitung jumlah piksel yang terdeteksi sebagai warna yang diinginkan.
  6. Menampilkan hasil deteksi warna dalam bentuk gambar atau grafik.

Contoh Kode Deteksi Warna RGB Menggunakan Matlab

1. Program Matlab Detejsi Warna RGB Realtime

Nah, Berikut ini listing programnya, silahkan jalankan dan simpan dengan nama file sesuka anda.

%% Initialization
redThresh = 0.24; % Threshold for red detection
greenThresh = 0.05; % Threshold for green detection
blueThresh = 0.15; % Threshold for blue detection

vidDevice = imaq.VideoDevice('winvideo', 1, 'YUY2_1280x720', ... % Acquire input video stream
    'ROI', [1 1 640 480], ...
    'ReturnedColorSpace', 'rgb');

vidInfo = imaqhwinfo(vidDevice); % Acquire input video property

hblob = vision.BlobAnalysis('AreaOutputPort', false, ... % Set blob analysis handling
    'CentroidOutputPort', true, ...
    'BoundingBoxOutputPort', true, ...
    'MinimumBlobArea', 600, ...
    'MaximumBlobArea', 3000, ...
    'MaximumCount', 10);

hshapeinsBox = vision.ShapeInserter('BorderColorSource', 'Input port', ... % Set box handling
    'Fill', true, ...
    'FillColorSource', 'Input port', ...
    'Opacity', 0.4);

htextinsRed = vision.TextInserter('Text', 'Red : %2d', ... % Set text for number of blobs
    'Location', [5 2], ...
    'Color', [1 0 0], ... % red color
    'Font', 'Courier New', ...
    'FontSize', 14);

htextinsGreen = vision.TextInserter('Text', 'Green : %2d', ... % Set text for number of blobs
    'Location', [5 18], ...
    'Color', [0 1 0], ... % green color
    'Font', 'Courier New', ...
    'FontSize', 14);

htextinsBlue = vision.TextInserter('Text', 'Blue : %2d', ... % Set text for number of blobs
    'Location', [5 34], ...
    'Color', [0 0 1], ... % blue color
    'Font', 'Courier New', ...
    'FontSize', 14);

htextinsCent = vision.TextInserter('Text', '+ X:%4d, Y:%4d', ... % set text for centroid
    'LocationSource', 'Input port', ...
    'Color', [1 1 0], ... % yellow color
    'Font', 'Courier New', ...
    'FontSize', 14);

hVideoIn = vision.VideoPlayer('Name', 'Final Video', ... % Output video player
    'Position', [100 100 vidInfo.MaxWidth+20 vidInfo.MaxHeight+30]);

nFrame = 0; % Frame number initialization

%% Processing Loop
while(nFrame < 300)
    rgbFrame = step(vidDevice); % Acquire single frame
    rgbFrame = flipdim(rgbFrame,2); % obtain the mirror image for displaying

    % Get red component of the image
    diffFrameRed = imsubtract(rgbFrame(:,:,1), rgb2gray(rgbFrame));
    diffFrameRed = medfilt2(diffFrameRed, [3 3]); % Filter out the noise by using median filter
    binFrameRed = im2bw(diffFrameRed, redThresh); % Convert the image into binary image with the red objects as white

    % Get green component of the image
    diffFrameGreen = imsubtract(rgbFrame(:,:,2), rgb2gray(rgbFrame));
    diffFrameGreen = medfilt2(diffFrameGreen, [3 3]); % Filter out the noise by using median filter
    binFrameGreen = im2bw(diffFrameGreen, greenThresh); % Convert the image into binary image with the green objects as white

    % Get blue component of the image
    diffFrameBlue = imsubtract(rgbFrame(:,:,3), rgb2gray(rgbFrame));
    diffFrameBlue = medfilt2(diffFrameBlue, [3 3]); % Filter out the noise by using median filter
    binFrameBlue = im2bw(diffFrameBlue, blueThresh); % Convert the image into binary image with the blue objects as white

    % Get the centroids and bounding boxes of the red blobs
    [centroidRed, bboxRed] = step(hblob, binFrameRed);
    centroidRed = uint16(centroidRed); % Convert the centroids into Integer for further steps

    % Get the centroids and bounding boxes of the green blobs
    [centroidGreen, bboxGreen] = step(hblob, binFrameGreen);
    centroidGreen = uint16(centroidGreen); % Convert the centroids into Integer for further steps

    % Get the centroids and bounding boxes of the blue blobs
    [centroidBlue, bboxBlue] = step(hblob, binFrameBlue);
    centroidBlue = uint16(centroidBlue); % Convert the centroids into Integer for further steps

    rgbFrame(1:50,1:90,:) = 0; % put a black region on the output stream
    vidIn = step(hshapeinsBox, rgbFrame, bboxRed, single([1 0 0])); % Instert the red box
    vidIn = step(hshapeinsBox, vidIn, bboxGreen, single([0 1 0])); % Instert the green box
    vidIn = step(hshapeinsBox, vidIn, bboxBlue, single([0 0 1])); % Instert the blue box

    for object = 1:1:length(bboxRed(:,1)) % Write the corresponding centroids for red
        centXRed = centroidRed(object,1); centYRed = centroidRed(object,2);
        vidIn = step(htextinsCent, vidIn, [centXRed centYRed], [centXRed-6 centYRed-9]);
    end

    for object = 1:1:length(bboxGreen(:,1)) % Write the corresponding centroids for green
        centXGreen = centroidGreen(object,1); centYGreen = centroidGreen(object,2);
        vidIn = step(htextinsCent, vidIn, [centXGreen centYGreen], [centXGreen-6 centYGreen-9]);
    end

    for object = 1:1:length(bboxBlue(:,1)) % Write the corresponding centroids for blue
        centXBlue = centroidBlue(object,1); centYBlue = centroidBlue(object,2);
        vidIn = step(htextinsCent, vidIn, [centXBlue centYBlue], [centXBlue-6 centYBlue-9]);
    end

    vidIn = step(htextinsRed, vidIn, uint8(length(bboxRed(:,1)))); % Count the number of red blobs
    vidIn = step(htextinsGreen, vidIn, uint8(length(bboxGreen(:,1)))); % Count the number of green blobs
    vidIn = step(htextinsBlue, vidIn, uint8(length(bboxBlue(:,1)))); % Count the number of blue blobs

    step(hVideoIn, vidIn); % Output video stream
    nFrame = nFrame+1;
end

%% Clearing Memory
release(hVideoIn); % Release all memory and buffer used
release(vidDevice);
clear all;
clc

Berikut ini hasil outputnya.

Deteksi Warna RGB Menggunakan Matlab Secara Realtime

2. Program Matlab Detejsi Warna RGB

% Membaca citra
    img = imread('gambar.jpg');
    
    % Mengubah citra ke dalam format RGB
    img_rgb = im2double(img);
    
    % Menentukan batasan nilai intensitas warna
    red_threshold = 0.7;
    green_threshold = 0.3;
    blue_threshold = 0.2;
    
    % Memfilter citra dengan batasan nilai intensitas warna
    filtered_img = (img_rgb(:,:,1) > red_threshold) & (img_rgb(:,:,2) > green_threshold) & (img_rgb(:,:,3) > blue_threshold);
    
    % Menghitung jumlah piksel yang terdeteksi sebagai warna yang diinginkan
    num_pixels = sum(filtered_img(:));
    
    % Menampilkan hasil deteksi warna
    figure;
    imshow(filtered_img);
    title(['Deteksi Warna RGB - Jumlah Piksel: ', num2str(num_pixels)]);

Kesimpulan

Deteksi warna RGB menggunakan Matlab adalah proses yang dapat membantu kita mengidentifikasi dan membedakan warna berdasarkan komponen warna merah, hijau, dan biru.

Baca Juga :   Membuat Emoticon Senyum Menggunakan C++ OpenGL

Dengan menggunakan Matlab, kita dapat dengan mudah melakukan deteksi warna RGB dengan langkah-langkah yang sederhana dan menggunakan contoh kode sebagai panduan. Deteksi warna RGB menggunakan Matlab dapat digunakan dalam berbagai aplikasi seperti pengolahan citra, pengenalan objek, dan lain sebagainya.

Demikianlah artikel kali ini, jangan lupa bagikan kesosial media kamu, dan jangan lupa berlangganan email kami agar selalu dapat update artikel terbaru dari kami

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *