Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / MFC

Kohonen's Self Organizing Maps in C++ with Application in Computer Vision Area

Rate me:
Please Sign up or sign in to vote.
4.88/5 (35 votes)
20 Nov 2007GPL318 min read 173.9K   7.1K   109  
The article demonstrates the self organizing maps clustering approach for unsupervised AI classification tasks with application examples in computer vision area for faces clustering and recognition

#include "stdafx.h"
#include "node.h"



///////////////////////////////////node constructor/destructor///////////////////////////////////////////
Node::Node(const float *weights, int weights_number, 
           const float *coords, int coords_number, int class_) : m_weights_number(weights_number), m_class(class_), m_precision(0.0f)
{
        m_weights = (float *)malloc(m_weights_number * sizeof(float));
        for (int i = 0; i < weights_number; i++)
                m_weights[i] = weights[i];

        m_coords = (float *)malloc(coords_number * sizeof(float));
        for (int i = 0; i < coords_number; i++)
                m_coords[i] = coords[i];
}
Node::~Node()
{
        free(m_weights);
        free(m_coords);
}
///////////////////////////////////node constructor/destructor///////////////////////////////////////////



/////////////////////////////votes calculation/////////////////////////////////////////////
void Node::clear_votes(int classes_number)
{
        if (m_votes.size() && classes_number == (int)m_votes.size()) {
                for (int c = 0; c < classes_number; c++)
                        m_votes[c] = 0;
        } else {
                m_votes.clear();
                for (int c = 0; c < classes_number; c++)
                        m_votes.push_back(0);
        }
        m_class = 0;
        m_precision = 0.0f;
}

bool Node::evaluate_class(const int *classes, int classes_number) //classes 1,2,3  or  2,4,5  or 5,2,1 ... not in ascending order
{
        if (classes_number) {
                m_precision = 0.0f;

                //get max votes number and assign a class to that node
                int maxvotes = m_votes[0];
                m_class = classes[0];
                for (int c = 1; c < classes_number; c++) {
                        if (maxvotes < m_votes[c]) {
                                maxvotes = m_votes[c];
                                m_class = classes[c];
                        }
                }

                //calculate node presicion = maxvotes/(cls1votes+cls2votes+ ... )
                if (maxvotes) {
                        for (int c = 0; c < classes_number; c++)
                                m_precision += m_votes[c];
                        m_precision = ((float)maxvotes / m_precision);
                } else
                        m_class = 0;

                return true;
        } else
                return false;
}
//////////////////////////////////////////////////////////////////////////////////////////////

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Engineer
Russian Federation Russian Federation
Highly skilled Engineer with 14 years of experience in academia, R&D and commercial product development supporting full software life-cycle from idea to implementation and further support. During my academic career I was able to succeed in MIT Computers in Cardiology 2006 international challenge, as a R&D and SW engineer gain CodeProject MVP, find algorithmic solutions to quickly resolve tough customer problems to pass product requirements in tight deadlines. My key areas of expertise involve Object-Oriented
Analysis and Design OOAD, OOP, machine learning, natural language processing, face recognition, computer vision and image processing, wavelet analysis, digital signal processing in cardiology.

Comments and Discussions