Attention! This forum will be made read-only by Dec-20. Please migrate to Most of existing active users should've received invitation by e-mail.
Ask Your Question

holakai's profile - activity

2015-03-09 03:17:06 -0500 asked a question C++ Detection of White Particles

Hi there! I am trying to do detection of particles using OpenCV: image description

So far, I have been able to threshold the image (video) based on intensity. However, currently I am trying to use kmean to detect the oval shape particles, but to no avail. I need some help with the coding for the kmeans; I have scoured through OpenCV help pages, but yet to no avail. Will greatly appreciate help! Thank you very much

// Step 1: Obtain Ultrasound image // Step 2: Convert to black and white image // Step 3: Threshold video based on B&W intensity // Step 4: Use plusstring algorithm to find centroids of clusters // e.g. K-mean plus string algorithm // Step 5: Appoint center to each cluster // Step 6: Tracking algorithm: Kalman filter // Step 7: Output: Draw circle around particle

#include "stdafx.h"
//#include "opencv/cv.h"
//#include "opencv/highgui.h"
//#include "opencv/cxcore.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

    using namespace cv;
    using namespace std;

    int main(int argc, char* argv[])
        VideoCapture cap("C:\\BubbleCut.avi"); // open the video file for reading

        if (!cap.isOpened())  // if not success, exit program
            cout << "Cannot open the video file" << endl;
            return -1;

        //Step: Declare all Mat
        Mat frame;
        Mat frame_grey;
        Mat frame_thresholded; //to give binary image 0-255
        Mat frame_houghtrans; //Canny edge gives a binary image too
        Mat labels;
        Mat data, centers;

        vector<Vec3f> circles;

        //cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms

        double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video

        cout << "Frame per seconds : " << fps << endl;

        //namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); create a window called "MyVideo" can be omitted

        while (1)
            cap >> frame; //replace lines: receive frame from cap and store into Mat object aka frame

            if (frame.empty())
                cout << "End of file" << endl << endl;
                cap.release(); //not necessary

            //Step _ : Convert ultrasound raw video into greyscale video 
            cvtColor(frame, frame_grey, COLOR_RGB2GRAY);
            //plusstring algorithm, blob detection or hough transform

            #ifdef THRESHOLDING
            adaptiveThreshold(frame_grey, frame_thresholded, 255, ADAPTIVE_THRESH_GAUSSIAN_C, THRESH_BINARY, 45, 0);

            // Step 4: Use plusstring algorithm to find centroids of clusters
            //         e.g. K-mean plus string algorithm, find parameter of circle

            **//kmeans(frame_thresholded, 8, labels, TermCriteria(CV_TERMCRIT_ITER, 10, 1.0), 3, KMEANS_PP_CENTERS, centers);**

            // Step 7: Display: Videos
            imshow("Raw video", frame); //show the frame in "MyVideo" window
            imshow("Thresholded", frame_thresholded);

            if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
                cout << "esc key is pressed by user" << endl;

        return 0;