C++ Detection of White Particles

asked 2015-03-09 03:14:32 -0600

updated 2015-03-09 04:25:52 -0600

jamesnzt gravatar image

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/cvaux.h>
#include<opencv/highgui.h>
#include<opencv/cxcore.h>
//#include "opencv/cv.h"
//#include "opencv/highgui.h"
//#include "opencv/cxcore.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include<stdio.h>
#include<stdlib.h>

#define THRESHOLDING
    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
                break;
            }


            //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);
            #endif 

            // 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;
                break;
            }
        }

        return 0;

    }
edit retag flag offensive close merge delete

Comments

Hi, I don't understand what you mean "plusstring algorithm to find centroids of clusters" but may be you can use this tutorial http://docs.opencv.org/doc/tutorials/...

LBerger gravatar imageLBerger ( 2015-03-09 05:15:00 -0600 )edit