📄 tex.m
字号:
% tex.m
%
% This file implements a texture classification example using
% NeuroSolutions for MATLAB.
%
% Problem Definition:
% The problem is to distinguish between the leopard and the background in
% which the leopard is sitting in the leopard.jpg file available in the
% same folder as this script. This problem falls under the category of
% problem called texture classification which falls under the broader
% category of pattern classification.
%
% Solution:
% The image file (leopard.jpg) is read into a matrix. Small sub images of
% the image is sampled out. The sampled images are used as training data
% for a neural network created using NeuroSolutions for MATLAB functions.
% The neural network is trained using the training data. The trained neural
% network is then tested on all parts of the image by sampling the entire
% image as small sub-images.
%
%
A = imread('leopard.jpg'); % Read Image into matrix
a = A(:,:,1); % We have used only the Red Channel (Red, Green, Blue - RGB) as Data for Training and Testing. You can use either of the three or all of the three based on the image and the precision in results you want to achieve.
[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');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sub-Sample 5x5 images from the leopard image for Training
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Sample from the area of the image which consists of the leopard
trainImages1 = [];
count = 1;
for i=125:5:175
for j = 200:5:300
trainImages1 = [trainImages1; a(i:i+4,j:j+4)];
h3 = rectangle ('Position', [j,i,5,5]);
set(h3, 'FaceColor', [1 0 0])
count = count + 1;
end
end
% Flatten images
% nsFlatten is a function that comes with the toolbox. It flattens images
% into single rows of data for use as training or testing data for a neural
% network
trainImages1 = double(nsFlatten (trainImages1, count-1));
% The reason for using "double" function is to convert the uint8 data type
% into double data. The nsTrain function can operate only on double data
% type. Hence we need this casting operation.
trainDesired1 = ones(count-1, 1); % Assign a arbitary number to represent the class "leopard". We are using the number 1 to represent the class leopard
% Sample images from the area of the figure that consists of the background
count = 1;
trainImages2 = [];
for i=200:5:250
for j = 1:5:100
trainImages2 = [trainImages2; a(i:i+4,j:j+4)];
h3 = rectangle ('Position', [j,i,5,5]);
set(h3, 'FaceColor', [1 1 0]);
count = count + 1;
end
end
% Flatten sub-sampled images uning nsFlatten
trainImages2 = double(nsFlatten (trainImages2, count-1));
trainDesired2 = zeros(count-1,1); % Assingn a arbitary number to represent the class "background". We are using the number 0 to represent the class background
% Combine the training data
trainImages = [trainImages1; trainImages2];
trainDesired = [trainDesired1;trainDesired2];
% This step is not neccessary. It is done to achieve better results.
% Shuffle the rows for better distribution of data. Otherwise the neural
% network would train with leopard data all at once and then background
% data all at once. Shuffling the rows will avoid that situation.
trainData = [trainImages trainDesired];
trainData = sortrows(trainData,1); % Since there is no function to randomize rows we are using sortrows as a work around.
% This step is just for clarity. x is the input data and y is the desired
% data
x = trainData(:,1:end-1);
y = trainData(:,end);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Neural network training
%%%%%%%%%%%%%%%%%%%%%%%%%%%
mynet = nsnn; % create neural network
mynet.epochs = 3000; % set the number of epochs
mynet.modelSettings.gradientDescentMethod = 'DeltaBarDelta'; % set gradient descent method
mynet = nsTrain (mynet, x, y); % train
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Break up the entire image into small 5x5 images for use as testing data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
count = 1;
allImages = [];
for i=1:5:r
for j = 1:5:c
allImages = [allImages; a(i:i+4,j:j+4)];
count = count + 1;
end
end
% Flatten the images
z = double(nsFlatten (allImages, count-1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Neural network testing
%%%%%%%%%%%%%%%%%%%%%%%%%%%%
tst = nsTest (mynet, z);
tst = round(tst); % Round off the numbers to achieve crisp 0 or 1 outputs
%%%%%%%%%%%%%%%%%%%%%%%%%%
% Display Testing results
%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -