pattern recognition using neural network in matlab - matlab

I am doing a project in character recognition of a local language. I created the dataset. But I am not sure how to feed it using neural network?
In this stage, I can only select one image as input rather than whole set of same character. How to proceed?
Pls help

As far as I know these neurons don't understand 2d input, so you need to make a 1d array from image:
image1flat = image1(:);
image2flat = image2(:);
Then put them into a 2d trainning set array (note that "Samples are:" option on your picture)
%samples are columns now
trainningSet = [image1flat image2flat];
Automated code:
%change this to folder where the files are
cd('/path/to/files');
%change this to your file format
files = dir('*.png');
result = [];
for i = 1:length(files)
A{i} = imread(files(i).name);
Aflat{i} = A{i}(:);
result = [result Aflat{i}];
end
%put the result into nprtool

Related

Matlab: How to use a .dat file in Neural Network?

I'm trying to use a .dat file's data to create an RBF neural network and train it. But I don't know how to use it's columns as input and target data in the network.
This is an image of the file in matlab:
train.dat
I tried this:
fid = fopen('train.dat','r');
A = fscanf(fid, '%f');
C1 = textscan(fid,'%s%f%s%f'); %read the first line
nb_col = C1{4}; %get the number of columns (could be set by user too)
%read the remaining of the file
C2 = textscan(fid, repmat('%f',1,nb_col), 'CollectOutput',1);
fclose(fid); %close the connection
My question is what code should I write at the beginning to open the train.dat file and put it's first column into a vector (patterns) and it's third column in another vector (target)?
I'm not sure if this will work but you can try:
load train.dat
patterns = train(:,1); % If just first column
patterns = train(:,[1:2]); % If column 1 & 2 are the vector patterns
target = train(:,3);

Sorting dicom images in Matlab

I am working with lung data sets in matlab, but I need to sort the slices correctly and show them.
I knew that can be done using the "instance number" parameter in Dicom header, but I did not manage to run the correct code.
How can I do that?
Here is my piece of code:
Dicom_directory = uigetdir();
sdir = strcat(Dicom_directory,'\*.dcm');
files = dir(sdir);
I = strcat(Dicom_directory, '\',files(i).name);
x = repmat(double(0), [512 512 1 ]);
x(:,:,1) = double(dicomread(I));
axes(handles.axes1);
imshow(x,[]);
First of all, to get the DICOM header, you need to use dicominfo which will return a struct containing each of the fields. If you want to use the InstanceNumber field to sort by, then you can do this in such a way.
%// Get all of the files
directory = uigetdir();
files = dir(fullfile(directory, '*.dcm'));
filenames = cellfun(#(x)fullfile(directory, x), {files.name}, 'uni', 0);
%// Ensure that they are actually DICOM files and remove the ones that aren't
notdicom = ~cellfun(#isdicom, filenames);
files(notdicom) = [];
%// Now load all the DICOM headers into an array of structs
infos = cellfun(#dicominfo, filenames);
%// Now sort these by the instance number
[~, inds] = sort([infos.InstanceNumber]);
infos = infos(inds);
%// Now you can loop through and display them
dcm = dicomread(infos(1));
him = imshow(dcm, []);
for k = 1:numel(infos)
set(him, 'CData', dicomread(infos(k)));
pause(0.1)
end
That being said, you have to be careful sorting DICOMs using the InstanceNumber. This is not a robust way of doing it because the "InstanceNumber" can refer to the same image acquired over time or different slices throughout a 3D volume. If you want one or the other, I would choose something more specific.
If you want to sort physical slices, I would recommend sorting by the SliceLocation field (if available). If sorting by time, you could use TriggerTime (if available).
Also you will need to consider that there could also potentially be multiple series in your folder so maybe consider using the SeriesNumber to differentiate these.

How to visualize the visual words from a bag of features classifier in MATLAB?

I am using bag of features to classify histology images in MATLAB.
here is the code that I used. It's taken from the `imageCategoryClassificationExample
Location of the compressed data set
url=http://www.vision.caltech.edu/Image_Datasets/Caltech101/101_ObjectCategories.tar.gz';
Store the output in a temporary folder
outputFolder = fullfile(tempdir, 'caltech101');
rootFolder = fullfile(outputFolder, '101_ObjectCategories');
imgSets = [ imageSet(fullfile(rootFolder, 'airplanes')), ...
imageSet(fullfile(rootFolder, 'ferry')), ...
imageSet(fullfile(rootFolder, 'laptop')) ];
determine the smallest amount of images in a category
minSetCount = min([imgSets.Count]);
Use partition method to trim the set.
imgSets = partition(imgSets, minSetCount, 'randomize');
Separate the sets into training and validation data.
[trainingSets, validationSets] = partition(imgSets, 0.3, 'randomize');
Create the bag of features classifier
bag = bagOfFeatures(trainingSets);
Additionally, the bagOfFeatures object provides an encode method for
counting the visual word occurrences in an image. It produced a histogram
that becomes a new and reduced representation of an image.
img = read(imgSets(1), 1);
[featureVector,words] = encode(bag, img);
Plot the histogram of visual word occurrences
figure
bar(featureVector)
title('Visual word occurrences')
xlabel('Visual word index')
ylabel('Frequency of occurrence')
After the creation of the bag of features object how can I visualize the final codebook and which visual words make up each image? Can I reconstruct the image from these words? I think it has to do with the use of encode to create a visualWords object. But how do I proceed after that?
There is no built-in function in MATLAB that would do that for you. However there are papers on the subject.

calculate number of matches between two images using MATLAB vl_sift

I’m new to MATLAB. I’m using VL_Feat library. I’m trying to construct a code that can calculate number of matching points between two images. Up to now I know how to match two images. What I want to get is number of matching points.
As an example
“X key points found in image 1”
“Y key points found in image 2”
“z matches”
Can anyone help me?
im1Path = fullfile(vl_root, 'data', 'roofs1.jpg') ;
im2Path = fullfile(vl_root, 'data', 'roofs2.jpg') ;
im1 = imread(im1Path) ;
im2 = imread(im2Path) ;
[f1,d1] = vl_sift(im2single(rgb2gray(im1))) ;
[f2,d2] = vl_sift(im2single(rgb2gray(im2))) ;
[matches, scores] = vl_ubcmatch(d1,d2) ;
fprintf(' %d a counts.\n', vl_ubcmatch(d1,d2));
As I understand you want to find the no of keypoints of the two images separately.the given statements below will not produce the exact output you want but I hope this will help you to some extent. this also show some important info regarding the keypoints. If the two images are I & J,then after reading the two images you can add these lines-
I = single(rgb2gray(I));
vl_covdet(I, 'verbose');
J = single(rgb2gray(J));
vl_covdet(J, 'verbose');
and then rest of the codes.

How can I create/process variables in a loop in MATLAB?

I need to calculate the mean, standard deviation, and other values for a number of variables and I was wondering how to use a loop to my advantage. I have 5 electrodes of data. So to calculate the mean of each I do this:
mean_ch1 = mean(ch1);
mean_ch2 = mean(ch2);
mean_ch3 = mean(ch3);
mean_ch4 = mean(ch4);
mean_ch5 = mean(ch5);
What I want is to be able to condense that code into a line or so. The code I tried does not work:
for i = 1:5
mean_ch(i) = mean(ch(i));
end
I know this code is wrong but it conveys the idea of what I'm trying to accomplish. I want to end up with 5 separate variables that are named by the loop or a cell array with all 5 variables within it allowing for easy recall. I know there must be a way to write this code I'm just not sure how to accomplish it.
You have a few options for how you can do this:
You can put all your channel data into one large matrix first, then compute the mean of the rows or columns using the function MEAN. For example, if each chX variable is an N-by-1 array, you can do the following:
chArray = [ch1 ch2 ch3 ch4 ch5]; %# Make an N-by-5 matrix
meanArray = mean(chArray); %# Take the mean of each column
You can put all your channel data into a cell array first, then compute the mean of each cell using the function CELLFUN:
meanArray = cellfun(#mean,{ch1,ch2,ch3,ch4,ch5});
This would work even if each chX array is a different length from one another.
You can use EVAL to generate the separate variables for each channel mean:
for iChannel = 1:5
varName = ['ch' int2str(iChannel)]; %# Create the name string
eval(['mean_' varName ' = mean(' varName ');']);
end
If it's always exactly 5 channels, you can do
ch = {ch1, ch2, ch3, ch4, ch5}
for j = 1:5
mean_ch(j) = mean(ch{j});
end
A more complicated way would be
for j = 1:nchannels
mean_ch(j) = eval(['mean(ch' num2str(j) ')']);
end
Apart from gnovice's answer. You could use structures and dynamic field names to accomplish your task. First I assume that your channel data variables are all in the format ch* and are the only variables in your MATLAB workspace. The you could do something like the following
%# Move the channel data into a structure with fields ch1, ch2, ....
%# This could be done by saving and reloading the workspace
save('channelData.mat','ch*');
chanData = load('channelData.mat');
%# Next you can then loop through the structure calculating the mean for each channel
flds = fieldnames(chanData); %# get the fieldnames stored in the structure
for i=1:length(flds)
mean_ch(i) = mean(chanData.(flds{i});
end