%Code for leopard image courtesy of Neurodimension %% Set up the data: %****************************************** % % These initial lines set up the image, the % training set and the testing set. % % (No need to change these) % %******************************************* A = imread('leopard.jpg'); % Read Image into matrix a = A(:,:,1); %We're only using the red channel [r,c] = size(a); %%%%%%%%%%%%%%%%%%%%%%%%%%% % Display the Leopard image %%%%%%%%%%%%%%%%%%%%%%%%%%% % Code to position the figure on the screen screenunits = get(0, 'Units'); set(0, 'Units', 'pixels'); sc = get(0, 'ScreenSize'); % screen co-ordinates % Show figure h1 = figure; h2 = axes('Parent', h1); image(A); set(h1, 'position', [20, sc(4)-(r+50), c, r], 'menubar', 'none', 'Name', 'Sub-sampled images as Training Data'); set(h2, 'Visible', 'Off'); pause(0.1) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Sub-Sample 5x5 images from the leopard image for Training %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Sample from the area of the image which consists of the leopard count = 1; for i=125:5:175 for j = 200:5:400 Temp=a(i:i+4,j:j+4); TrainLeopard(1:25,count)=Temp(:); %These set the pixels on the screen to red: h3 = rectangle ('Position', [j,i,5,5]); set(h3, 'FaceColor', [1 0 0]) count = count + 1; end end classes1 = ones(1,count-1); % Leopard "class" is set to 1 % Sample images from the area of the figure that consists of the background count = 1; for i=180:5:250 for j = 1:5:100 Temp=a(i:i+4,j:j+4); TrainBackground(1:25,count)=Temp(:); %Sets the pixel colors to yellow h3 = rectangle ('Position', [j,i,5,5]); set(h3, 'FaceColor', [1 1 0]); count = count + 1; end end pause(0.1); %Used so the image gets rendered clear Temp classes2 = -1*ones(1,count-1); % Make the background "class" -1 % Combine the training data X = double([TrainLeopard, TrainBackground]); Targets =[classes1, classes2]; % Before training, get the whole image ready for testing: count = 1; for i=1:5:r for j = 1:5:c Temp=a(i:i+4,j:j+4); FullImage(1:25,count) = double(Temp(:)); count = count + 1; end end %% %**************************************************************** % % Neural network training: Where you come in. % %**************************************************************** net=newff(X,Targets,10); % Here are some optional settings that come in useful: %net.divideParam.trainRatio=0.8 %net.divideParam.valRatio=0.1; %net.divideParam.testRatio=0.1; %net.trainFcn='traingd'; %net.trainParam.epochs=1500; net=train(net,X,Targets); tst=sim(net,FullImage); %*********************************************************************** % % Display Testing results: No need to change anything from here down % %*********************************************************************** %Just in case these were used in your code, we re-set them: A = imread('leopard.jpg'); % Read Image into matrix a = A(:,:,1); % We'll only use the red channel for this project. [r,c] = size(a); % set up figure h1 = figure; h2 = axes('Parent', h1); image(A); set(h1, 'position', [40+c, sc(4)-(r+50), c, r], 'menubar', 'none', 'Name', 'Trained Neural Network Response'); set(h2, 'Visible', 'Off'); % Show results count = 1; for i=1:5:r for j = 1:5:c h3 = rectangle ('Position', [j,i,5,5]); if tst(count)>0 set(h3, 'FaceColor', [1,0,0]) end count = count + 1; end end