Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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

, 20 Nov 2007 GPL3
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
som_demo.zip
bin
rgb.som
rgb_1.som
som.exe
som_src.zip
src
Lib
LibSOM

#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)

Share

About the Author

Chesnokov Yuriy
Engineer
Russian Federation Russian Federation
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 20 Nov 2007
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid