Click here to Skip to main content
13,094,983 members (81,817 online)
Click here to Skip to main content

Stats

125.3K views
6.1K downloads
109 bookmarked
Posted 20 Nov 2007

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

, 20 Nov 2007
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 "signal.h"





//////////////////////////////////constructors/destructors////////////////////////////////////////////////////
CSignal::CSignal(wchar_t *fname): format(0), fp(0), fpmap(0), lpMap(0), N(0), M(0)
{
        wcscpy(name, fname);

        if (!read13(fname)) {
                read11(fname);
                //  read12(fname);
        }
}
CSignal::CSignal(wchar_t *fname, int n, int m): format(13), fp(0), fpmap(0), lpMap(0), N(n), M(m)
{
        wcscpy(name, fname);

        fp = CreateFile(fname, GENERIC_WRITE | GENERIC_READ, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
        if (fp == INVALID_HANDLE_VALUE) {
                N = 0;
                M = 0;
                format = 0;
                return;
        }
        fpmap = CreateFileMapping(fp, 0, PAGE_READWRITE, 0, N * M * sizeof(float), 0);
        if (!fpmap) {
                N = 0;
                M = 0;
                format = 0;
                CloseHandle(fp);
                return;
        }
        lpMap = MapViewOfFile(fpmap, FILE_MAP_WRITE, 0, 0, N * M * sizeof(float));
        if (!lpMap) {
                N = 0;
                M = 0;
                format = 0;
                CloseHandle(fpmap);
                CloseHandle(fp);
                return;
        }

        float *pdata = (float *)lpMap;
        data.push_back(pdata);

        memset(lpMap, 0, N*M*sizeof(float));

        wchar_t hdrfile[_MAX_PATH];
        wcscpy(hdrfile, fname);
        changeext(hdrfile, L".hea");
        FILE *fh = _wfopen(hdrfile, L"wt");
        if (fh) {
                fwprintf(fh, L"%d %d", N, M);
                fclose(fh);
        }
}
CSignal::~CSignal()
{
        if (format == 11)
                delete[] data[0];

        //close file mapping
        if (lpMap) {
                UnmapViewOfFile(lpMap);
                if (fpmap) {
                        CloseHandle(fpmap);
                        if (fp && fp != INVALID_HANDLE_VALUE)
                                CloseHandle(fp);
                }
        }
}
//////////////////////////////////constructors/destructors////////////////////////////////////////////////////








////////////////////////////////read data in format 1.3///////////////////////////////////////////////////////
bool CSignal::read13(wchar_t *fname)
{
        wchar_t hdrfile[_MAX_PATH];
        wcscpy(hdrfile, fname);

        changeext(hdrfile, L".hea");
        FILE *fh = _wfopen(hdrfile, L"rt");
        if (fh) {
                if (fwscanf(fh, L"%d %d", &N, &M) == 2) {
                        fclose(fh);

                        fp = CreateFile(fname, GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
                        if (fp == INVALID_HANDLE_VALUE)
                                return false;

                        LARGE_INTEGER size;
                        if (!GetFileSizeEx(fp, &size))
                                return false;

                        if (__int64(sizeof(float)*N*M) != size.QuadPart)
                                return false;

                        fpmap = CreateFileMapping(fp, 0, PAGE_READWRITE, 0, 0, 0);
                        if (!fpmap) {
                                CloseHandle(fp);
                                return false;
                        }
                        lpMap = MapViewOfFile(fpmap, FILE_MAP_WRITE, 0, 0, 0);
                        if (!lpMap) {
                                CloseHandle(fpmap);
                                CloseHandle(fp);
                                return false;
                        }

                        float *pdata = (float *)lpMap;

                        data.resize(N);
                        for (int n = 0; n < N; n++) {
                                data[n] = pdata;
                                pdata += M;
                        }

                        format = 13;
                        return true;
                } else {
                        N = 0;
                        M = 0;
                        fclose(fh);
                        return false;
                }
        } else
                return false;
}

void CSignal::changeext(wchar_t *path, wchar_t *ext)
{
        for (int i = (int)wcslen(path) - 1; i > 0; i--) {
                if (path[i] == '.') {
                        path[i] = 0;
                        wcscat(path, ext);
                        return;
                }
        }
        wcscat(path, ext);
}
////////////////////////////////read data in format 1.3///////////////////////////////////////////////////////



////////////////////////////////read data in format 1.1///////////////////////////////////////////////////////
bool CSignal::read11(wchar_t *fname)
{
        vector<float> vec;
        float tmp;
        int res;

        FILE *fh;
        if ((fh = _wfopen(fname, L"rt")) == 0)
                return false;

        for (;;) {
                res = fscanf(fh, "%f", &tmp);
                if (res == EOF || res == 0)
                        break;
                else
                        vec.push_back(tmp);
        }

        fclose(fh);

        if (vec.size() < 2)
                return false;

        N = 1;
        M = (int)vec.size();
        float *pdata = new float[M];
        for (int i = 0; i < M; i++)
                pdata[i] = vec[i];

        data.push_back(pdata);

        return true;
}
////////////////////////////////read data in format 1.1///////////////////////////////////////////////////////




////////////////////////////////dump data/////////////////////////////////////////////////////////////////////
void CSignal::dump(wchar_t *fname)
{
        FILE *fh = _wfopen(fname, L"wt");
        if (fh) {
                for (int n = 0; n < N; n++) {
                        float *pdata = data[n];
                        for (int m = 0; m < M; m++)
                                fwprintf(fh, L"%f ", pdata[m]);
                        fwprintf(fh, L"\n");
                }
                fclose(fh);
        }
}
////////////////////////////////dump data/////////////////////////////////////////////////////////////////////







///////////////////////////////normalization//////////////////////////////////////////////////////////////////
void CSignal::minmax(float *buff, int size, float &min, float &max)
{
        max = buff[0];
        min = buff[0];
        for (int i = 1; i < size; i++) {
                if (buff[i] > max)max = buff[i];
                if (buff[i] < min)min = buff[i];
        }
}
void CSignal::nminmax(float *buff, int len, float a, float b)
{
        float min, max;
        minmax(buff, len, min, max);

        for (int i = 0; i < len; i++) {
                if (max - min)
                        buff[i] = (buff[i] - min) * ((b - a) / (max - min)) + a;
                else
                        buff[i] = a;
        }
}
void CSignal::nenergy(float *buff, int len, int L)
{
        float enrg = 0.0f;
        for (int i = 0; i < len; i++)
                enrg += pow(fabs(buff[i]), (float)L);

        enrg = pow(enrg, 1.0f / (float)L);
        if (enrg == 0.0f) enrg = 1.0f;

        for (int i = 0; i < len; i++)
                buff[i] /= enrg;
}
///////////////////////////////normalization//////////////////////////////////////////////////////////////////

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
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.

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170813.1 | Last Updated 20 Nov 2007
Article Copyright 2007 by Chesnokov Yuriy
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid