Ask Your Question
0

KMeans in real time

asked 2017-12-31 17:51:12 -0500

cjacquel gravatar image

Hello,

Is it possible to make Kmeans on a webcam flow for each frame in Real Time ? With which material ?

Thank you, cjacquel

edit retag flag offensive close merge delete

Comments

maybe this SO post will be helpful

sturkmen gravatar imagesturkmen ( 2017-12-31 18:07:08 -0500 )edit

@cjacquel, @sturkmen. No webcam for KMeans. Use cv2.imread

supra56 gravatar imagesupra56 ( 2017-12-31 20:56:26 -0500 )edit

@supra56 may i ask you "how old are you?"

sturkmen gravatar imagesturkmen ( 2017-12-31 21:04:20 -0500 )edit
1

@sturkmen. Those two numbers beside my name is the year of born.

supra56 gravatar imagesupra56 ( 2017-12-31 22:44:49 -0500 )edit

If you want only to change colors ( oil painting effect) you can use this post

LBerger gravatar imageLBerger ( 2018-01-01 04:05:05 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-01-01 05:20:22 -0500

LBerger gravatar image

I don't know what you want exactly but results seems is OK(may be there is a bug...)

#include <opencv2/opencv.hpp> 
#include <iostream>
#include <fstream>

using namespace std;
using namespace cv;

int main (int argc,char **argv)
{
    VideoCapture vid(0);


    if (!vid.isOpened())
    {
        cout << "cannot open video stream!";
        return 0;
    }
    int code = 0;
    Mat img;
    Mat labels;
    Mat centers; 
    int mode = 0;
    while (code != 27)
    {
        vid >> img;
        if (img.empty())
        {
            cout << "cannot read video stream!";
            return 0;
        }
        Mat src, srcF;

        img.convertTo(srcF, CV_32FC3);
        vector<Vec3f> plan;
        plan.assign((Vec3f*)srcF.datastart, (Vec3f*)srcF.dataend);
        int clusterCount = 18;
        if (centers.empty())
            kmeans(plan, clusterCount, labels, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0), 10, KMEANS_PP_CENTERS, centers);
        else
            kmeans(plan, clusterCount, labels, TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 10, 1.0), 1, KMEANS_USE_INITIAL_LABELS, centers);
        Mat mask;
        imshow("Original", img);
        int maxCluster = 0, ind = -1;
        Mat result = Mat::zeros(img.rows, img.cols, CV_8UC3);
        labels = labels.reshape(0, img.rows);
        for (int i = 0; i < clusterCount; i++)
        {
            cv::Mat cloud = (labels == i);
            result.setTo(Scalar(centers.at<float>(i, 0), centers.at<float>(i, 1), centers.at<float>(i, 2)), cloud);
        }
        labels = labels.reshape(0, img.rows*img.cols);
        imshow("New", result);
        code = waitKey(10);
        if (code == 'r')
            centers = Mat();
        if (code == 'n')
            mode = 1;
        if (code == 'u')
            mode = 0;
        if (mode == 1)
            centers = Mat();
    }
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-12-31 17:51:12 -0500

Seen: 92 times

Last updated: Jan 01 '18