Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

clustering of coloured histograms

I am trying to make to clusters of hsv histogram of all frames of a video. For this I have calculated hsv histogram of each frame and pushed histogram in <vector> and then in the last i have used kmeans command to make clusters . my code crashed at kmeans . please help me in finding solution . I am trying to solve this problem from many days but failed.

// newproject.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "highgui.h"

#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#include <conio.h>
#include <opencv2/imgproc/imgproc.hpp>  // Gaussian Blur
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
#include <conio.h>

using namespace cv;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
    cv::Mat centers;
    Mat src_base, hsv_base;
    cv::Mat labels;
    vector<MatND> histograms;
    string filename = "ayesh.avi";
    VideoCapture capture(filename);

    if( !capture.isOpened() )
        exit(0);

    for( ; ; )
    {
        capture >> src_base;
        if(src_base.empty())
            break;

        /// Convert to HSV
        cvtColor( src_base, hsv_base, CV_BGR2HSV );

        /// Using 16 bins for hue and 32 for saturation
        int h_bins = 16; int s_bins = 8;
        int histSize[] = { h_bins, s_bins };

        // hue varies from 0 to 256, saturation from 0 to 180
        float h_ranges[] = { 0, 256 };
        float s_ranges[] = { 0, 180 };

        const float* ranges[] = { h_ranges, s_ranges };

        // Use the o-th and 1-st channels
        int channels[] = { 0, 1 };

        /// Histograms
        MatND hist_base;

        /// Calculate the histograms for the HSV images
        calcHist( &hsv_base, 1, channels, Mat(), hist_base, 2, histSize, ranges, true, false );
        histograms.push_back(hist_base);

    }
    cv::kmeans(histograms, 8, labels,cv::TermCriteria(CV_TERMCRIT_ITER, 10, 1.0),3, cv::KMEANS_PP_CENTERS, centers);
    cout<<"code executed"<<endl;
         return 0;
}