Ask Your Question

sakaz's profile - activity

2019-06-13 08:07:44 -0600 received badge  Notable Question (source)
2018-02-02 16:41:15 -0600 received badge  Popular Question (source)
2016-01-12 09:07:31 -0600 received badge  Student (source)
2015-06-17 09:25:09 -0600 asked a question Contours Centers pass to K-Means ??

Hi All, I am learning about opencv and used some functions. Now i am working on a project to detect the direction of people / object. For this purpose i need find the center of the object. After Background Subtraction the object comes in different part of contours and need to join them into ONE. I have read about K-Means Clustering but i am unable to pass the 2D vector float data. Would you please give me sample example code how to achieve my goal.

/// Find contours findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

/// Get the moments
vector<Moments> mu(contours.size() );
for( int i = 0; i < contours.size(); i++ )
 { mu[i] = moments( contours[i], false ); }

///  Get the mass centers:
vector<Point2f> mc( contours.size() );
for( int i = 0; i < contours.size(); i++ )
 { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }

I got the centers of all contours into MC and now need to pass to K-Means. I have learnt with the examples which is working but not sure about my real data.

float data2[15][2]=
    {{118.90323, 1088.7419},
    {143.5, 1064.5},
    {110, 1054},
    {662, 645},
    {650, 625.5},
    {94, 363},
    {60, 360},
    {103.97369, 315.71054},
    {70.5, 313},
    {70.1, 313},
    {70.2, 313},
    {70.3, 313},
    {70.4, 313},
    {70.7, 313},
    {1466, 278.55554}};

    Mat centers1;
    Mat points1(15,2, CV_32FC1,*data2);
    //std::vector<cv::Point2f> points1(data2);  //std::cout << points1 << std::endl;
    Mat labels1;
    int k =4;
    cv::kmeans(points1,k, labels1,TermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0),3, KMEANS_PP_CENTERS, centers1);
    std::cout << "labels: " << labels1 << endl;
    std::cout << "centers " << centers1 << endl;

Looking forward your assistance in this regard. Shan

2015-06-06 16:57:57 -0600 received badge  Scholar (source)
2015-06-06 16:57:43 -0600 commented answer get Center of nearby contours ??

Thanks alot for your kind help and suggestion. I have used std::vector<std::vector<cv::point> for sorting out labels. :)

2015-06-06 11:39:04 -0600 commented answer get Center of nearby contours ??

Thanks for your reply. I do understand it but i have tried to store only the points with the same labels but should i need to erase the vector. would you please give me little code idea.

int buffer = 0;
        if (label != buffer) {
            buffer = label;

            cv::RotatedRect rotated_bounding_rect1 = minAreaRect(merged_contour_points1);
            Rect brect1 = rotated_bounding_rect1.boundingRect();
            rectangle(grouped, brect1, Scalar(255,255,0));

    merged_contour_points1.erase(merged_contour_points1.begin(), merged_contour_points1.end() max_points_length);           

            }

            for (int j = 0; j < contours[i].size(); j++) {

                merged_contour_points1.push_back(contours[i][j]);
}
2015-06-06 10:23:17 -0600 asked a question get Center of nearby contours ??

I am working on a project using OPENCV for some image processing. I been learning from examples, tutorials but I am new on opencv so I have a question. I have attached pictures (1) is original input (2) output for better understanding all of you. I have used dbscan algorithm for segmentation. My purpose is to detect multiple objects in an image and draw rectangle around them and give center point of each rectangle to track or know the position.

But after some image processing filters , effects, Background subtraction, Findcontours ... i have found center of all contours. and pass to merged_contour_points vector. But it draw rectangle around all contours. I Hope you understand.

Here are my codes;

#include "stdafx.h"
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
#include <map>
#include <sstream>


template <class T>
inline std::string to_string (const T& t)
{
    std::stringstream ss;
    ss << t;
    return ss.str();
}

class DbScan
{
public:
    std::map<int, int> labels;
    vector<Rect>& data;
    int C;
    double eps;
    int mnpts;
    double* dp;
    //memoization table in case of complex dist functions
#define DP(i,j) dp[(data.size()*i)+j]
    DbScan(vector<Rect>& _data,double _eps,int _mnpts):data(_data)
    {
        C=-1;
        for(int i=0;i<data.size();i++)
        {
            labels[i]=-99;
        }
        eps=_eps;
        mnpts=_mnpts;
    }
    void run()
    {
        dp = new double[data.size()*data.size()];
        for(int i=0;i<data.size();i++)
        {
            for(int j=0;j<data.size();j++)
            {
                if(i==j)
                    DP(i,j)=0;
                else
                    DP(i,j)=-1;
            }
        }
        for(int i=0;i<data.size();i++)
        {
            if(!isVisited(i))
            {
                vector<int> neighbours = regionQuery(i);
                if(neighbours.size()<mnpts)
                {
                    labels[i]=-1;//noise
                }else
                {
                    C++;
                    expandCluster(i,neighbours);
                }
            }
        }
        delete [] dp;
    }
    void expandCluster(int p,vector<int> neighbours)
    {
        labels[p]=C;
        for(int i=0;i<neighbours.size();i++)
        {
            if(!isVisited(neighbours[i]))
            {
                labels[neighbours[i]]=C;
                vector<int> neighbours_p = regionQuery(neighbours[i]);
                if (neighbours_p.size() >= mnpts)
                {
                    expandCluster(neighbours[i],neighbours_p);
                }
            }
        }
    }

    bool isVisited(int i)
    {
        return labels[i]!=-99;
    }

    vector<int> regionQuery(int p)
    {
        vector<int> res;
        for(int i=0;i<data.size();i++)
        {
            if(distanceFunc(p,i)<=eps)
            {
                res.push_back(i);
            }
        }
        return res;
    }

    double dist2d(Point2d a,Point2d b)
    {
        return sqrt(pow(a.x-b.x,2) + pow(a.y-b.y,2));
    }

    double distanceFunc(int ai,int bi)
    {
        if(DP(ai,bi)!=-1)
            return DP(ai,bi);
        Rect a = data[ai];
        Rect b = data[bi];
        /*
        Point2d cena= Point2d(a.x+a.width/2,
                              a.y+a.height/2);
        Point2d cenb = Point2d(b.x+b.width/2,
                              b.y+b.height/2);
        double dist = sqrt(pow(cena.x-cenb.x,2) + pow(cena.y-cenb.y,2));
        DP(ai,bi)=dist;
        DP(bi,ai)=dist;*/
        Point2d tla =Point2d(a.x,a.y);
        Point2d tra =Point2d(a.x+a.width,a.y);
        Point2d bla =Point2d(a.x,a.y+a.height);
        Point2d bra =Point2d(a.x+a.width,a.y+a.height);

        Point2d tlb =Point2d(b.x,b.y);
        Point2d trb =Point2d(b.x+b.width,b.y);
        Point2d ...
(more)