feedfoward neuralnet for color recognision in matlab

Here's an example code for creating and training a simple feedforward neural network in MATLAB for color recognition from a given RGB image:

main.m
% Load training dataset
load color_dataset.mat

% Create target labels for the dataset
targets = zeros(3, length(colorTargets));
for i = 1:length(colorTargets)
    if strcmp(colorTargets{i}, 'red')
        targets(1,i) = 1;
    elseif strcmp(colorTargets{i}, 'green')
        targets(2,i) = 1;
    elseif strcmp(colorTargets{i}, 'blue')
        targets(3,i) = 1;
    end
end

% Create and initialize the neural network
net = feedforwardnet(10);
net = init(net);

% Set training parameters
net.trainFcn = 'trainlm';
net.performFcn = 'mse';
net.trainParam.showWindow = false;
net.trainParam.epochs = 100;

% Train the neural network
[net, tr] = train(net, colorInputs, targets);

% Test the trained neural network on an RGB image
img = imread('test_image.jpg');
out = net(reshape(double(img), [], 3)');

% Convert the output to a color label
[~, idx] = max(out);
if idx == 1
    colorLabel = 'red';
elseif idx == 2
    colorLabel = 'green';
elseif idx == 3
    colorLabel = 'blue';
end

disp(['The color in the image is: ' colorLabel]);
1035 chars
44 lines

Note that this is just a simple example and there are many ways to improve the accuracy and efficiency of the neural network for color recognition.

gistlibby LogSnag