How to train neural network to compare two images?
7 views (last 30 days)
Show older comments
I have two sets of binary images...first set contains N number of images(A1, A2,...AN) and second set contains 20 images(B1,B2,...B20)...all the images have same resolution say 20x30...I need to train NN to match each image in the first set one by one with the 20 images and display the name of the image with maximum percentage of matching...for example if A1 match best with B9 then it display A1B9 on text file...how can I do this...I'm new to NN...any sort of help is highly appreciated
1 Comment
Stephen Tang
on 16 Jul 2015
Sounds like you want to train a pattern recognition neural network. To train a neural network you need to provide a set of training vectors and a set of target vectors.
Explaining what these are requires knowing the format of the NN input/output. The default settings for a pattern recognition NN takes in an input vector (ex: vector describes an image) and outputs an n-dimensional target vector with each element falling in the range (0,1). Each index of this output vector is associated with a class, and the index with the highest value is the network's decision for which class the input falls into. For example, if A2 matches B13, then ideally your network produces a vector t of length 20 where t(13) near 1 and all other t(i) near 0 for i ~= 13.
To train the NN you need to provide a matrix of example input vectors and a matrix of their corresponding ideal target vectors by concatenating several cases. Ideal target vectors are binary with the index of the correct class being 1 and all others being 0. You'll also need to decide the number of nodes in your hidden layer. A higher number of hidden layer nodes increases the network's ability to recognize classes at the cost of longer training/computation time and risk of being unable to generalize to inputs outside of the training set. I don't know of a definite way to find the ideal number, so I tend to go with trial and error on this.
During training Matlab will randomly separate your training vectors into training, testing, and validation sets. The training set is used to train the NN. The validation set is used to check if the NN is too well fitted to the training set, which is done by stopping training after performance on the validation set gets worse 6 consecutive times in a row. Performance on the test set is the best indication of your network's performance.
After a network is trained, you can give use it by giving it other inputs as if it were a function, like:
for k = 1:N
t = net(pictures(k));
[~,I] = max(t);
fprintf(file,'A%iB%I', k, I);
end
Accepted Answer
Image Analyst
on 16 Jul 2015
Why? Why do you need a Neural Network? Why not just AND the pair of images and sum the result to find out how many pixels match?
andImage = binaryImage1 & binaryImage2;
numMatchingPixels = sum(andImage(:));
This would be so much simpler.
3 Comments
Image Analyst
on 16 Jul 2015
There are several ways to classify a binary image. You could use NN I suppose. Or you could use traditional and easier image processing methods like using ANDing like I showed, or using normalized cross correlation (demo attached), or you could use Hu's image moments if the test image might be rotated or scaled compared to the library. Maybe you could just compare the area fraction of the bounding box to the known area fractions of the library images and look for the closest match. Maybe you want to use several methods and take votes - that's probably the most robust way.
More Answers (2)
Cuong Tran
on 7 Apr 2018
Hi everybody I want to ask a problem. I want to use a neural network to compare two texts. If 2 identical text is corrected in font size, add comma, space. or down the line, etc ... How do I use the neural network to detect the difference between the two texts when there is a small change? Please answer me this problem. I love you guys. Thanks very much. Hope to have a reply
0 Comments
Ashraf Musa
on 4 Feb 2017
Edited: Image Analyst
on 4 Feb 2017
I am working on a project in which I intend to match between two images , I did these steps :
- resizing
- RGB to gray
- Edge detection
- Matching pix by pix
It doesn't work 100%, but gave a reasonable result.
1 Comment
Image Analyst
on 4 Feb 2017
Looks like step 4 needs a lot more elaboration. And we're not sure exactly what "to match" means. Are you forcing one image to look like another (like Prisma)? Are you just detecting how different two images are from each other?
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!