Ask Your Question

DylanVanAssche's profile - activity

2018-12-07 07:57:51 -0600 received badge  Nice Question (source)
2018-12-05 12:44:41 -0600 received badge  Student (source)
2018-12-05 12:42:31 -0600 edited question [NOT A QUESTION] answers.opencv.org is missing HTTPS support

[NOT A QUESTION] answers.opencv.org is missing HTTPS support Hi, My browser warned me about this subdomain because of t

2018-12-05 12:42:17 -0600 edited question [NOT A QUESTION] answers.opencv.org is missing HTTPS support

[NOT A QUESTION] answers.opencv.org is missing HTTPS support Hi, My browser warned me about this subdomain because of t

2018-12-05 12:41:52 -0600 commented question [NOT A QUESTION] answers.opencv.org is missing HTTPS support

Oh okay, I didn't know that moderators can't modify the website itself. Hopefully they can fix this since Google Chrome

2018-12-05 12:34:42 -0600 commented question [NOT A QUESTION] answers.opencv.org is missing HTTPS support

https://github.com/opencv-infrastructure/answers.opencv.org/issues/68

2018-12-05 12:30:10 -0600 commented question [NOT A QUESTION] answers.opencv.org is missing HTTPS support

Thanks for the quick response, I will open an issue there. I didn't know there was a special Github repository for this

2018-12-05 12:24:38 -0600 asked a question [NOT A QUESTION] answers.opencv.org is missing HTTPS support

[NOT A QUESTION] answers.opencv.org is missing HTTPS support Hi, My browser warned me about this subdomain because of t

2018-12-05 07:37:39 -0600 received badge  Self-Learner (source)
2018-12-03 12:59:19 -0600 marked best answer Machine Learning: NormalBayesClassifier no results

I created a small OpenCV program with 3 Machine Learning classifiers for a lab course to find red strawberries in an image. The green strawberries and everything else are removed from the image.

I use the following classifiers:

  • kNN
  • Normal Bayes
  • SVM

Environment:

  • CMake 3.12.4
  • GCC 8.2.1
  • CLion 2018.3
  • Arch Linux
  • OpenCV 3.4.4

They are all a subclass of ml::StatModel so I can use classifier->predict(pixel, label) to classify if the pixel is a strawberry or not. This works for kNN and SVM fine, but not for Normal Bayes for some reason. The mask is completely black. I added some screenshots of KNN and SVm results below.

Input: Input

KNN: KNN

SVM: SVM

I will re-enable (in the header) an opening and closing operation later to remove the noise and connect the blobs.

Here's my code:

strawberry.h

#ifndef SESSIE_5_3_STRAWBERRY_H
#define SESSIE_5_3_STRAWBERRY_H

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

using namespace std;
using namespace cv;

#define KERNEL_SIZE 5
#define CIRCLE_THICKNESS -1
#define CIRCLE_RADIUS 5
#define KNN_GROUPS 3
#define ML_OPENING_ITER 0
#define ML_CLOSING_ITER 0
#define SVM_ITER 100
#define SVM_EPSILON 1e-6

void runner(int trackbarPos, void *data);
void mouse(int event, int x, int y, int flags, void* userdata);
void descriptor(Mat img, vector<Point2d> foregroundPoints, vector<Point2d> backgroundPoints, Mat&    trainingData, Mat& labels);
void KNN(Mat trainingsData, Mat labels);
void NaiveBayes(Mat trainingsData, Mat labels);
void SVM(Mat trainingsData, Mat labels);
void showResult(Ptr<ml::StatModel> classifier);

#endif //SESSIE_5_3_STRAWBERRY_H

main.cpp

#include "strawberry.h"

vector<Point2d> savedPositivePoints;
vector<Point2d> savedNegativePoints;
Mat strawberryImg;
int mode = 0;

void mouse(int event, int x, int y, int flags, void* userdata) {
if  ( event == EVENT_LBUTTONDOWN )
{
    cout << "Left button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    Point2d p = Point2d(x, y);
    if(mode) {
        savedPositivePoints.push_back(p);
    }
    else {
        savedNegativePoints.push_back(p);
    }
}
else if  ( event == EVENT_RBUTTONDOWN )
{
    cout << "Right button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    if(mode) {
        savedPositivePoints.pop_back();
    }
    else {
        savedNegativePoints.pop_back();
    }
}
else if  ( event == EVENT_MBUTTONDOWN )
{
    cout << "Middle button of the mouse is clicked - position (" << x << ", " << y << ")" << endl;
    if(mode) {
        cout << "POSITIVE" << endl;
        for (int i = 0; i < savedPositivePoints.size(); i++) {
            cout << savedPositivePoints.at(i) << endl;
        }
    }
    else {
        cout << "NEGATIVE" << endl;
        for (int i = 0; i < savedNegativePoints.size(); i++) {
            cout << savedNegativePoints.at(i) << endl;
        }
    }
}

runner(0, NULL);
}

void descriptor(Mat img, vector<Point2d> foregroundPoints, vector<Point2d> backgroundPoints, Mat& trainingData, Mat& labels) {
Mat hsv;
Mat trainingDataForeground(foregroundPoints.size(), 3, CV_32FC1);
Mat trainingDataBackground(backgroundPoints.size(), 3, CV_32FC1);
Mat labels_fg = Mat::ones(foregroundPoints.size(), 1, CV_32SC1);
Mat labels_bg = Mat::zeros(backgroundPoints.size(), 1, CV_32SC1);
cvtColor(img, hsv, CV_BGR2HSV);

// foreground
for(int i=0; i < foregroundPoints.size(); i++) {
    Vec3b pixel = hsv.at<Vec3b>(foregroundPoints.at(i).y, foregroundPoints.at(i).x);
    trainingDataForeground.at<float>(i, 0) = pixel[0];
    trainingDataForeground.at<float>(i, 1) = pixel[1];
    trainingDataForeground.at<float>(i, 2) = pixel[2];
}

// background
for(int i=0; i < backgroundPoints.size(); i++) {
    Vec3b pixel = hsv.at<Vec3b>(backgroundPoints ...
(more)
2018-12-03 12:24:41 -0600 commented question Machine Learning: NormalBayesClassifier no results

@berak Can you accept my answer, I need at least 20 points to accept my own answer.

2018-12-03 12:24:10 -0600 edited answer Machine Learning: NormalBayesClassifier no results

Ask @berak pointed out and thanks to @StevenPuttemans to put me on the right track: mask_normalBayes.at<uchar>(i,

2018-12-03 12:23:43 -0600 answered a question Machine Learning: NormalBayesClassifier no results

Ask @berak pointed out: mask_normalBayes.at<uchar>(i,j) = labels_normalBayes.at<int>(0,0); mask_SVM.at<u

2018-12-03 04:42:39 -0600 commented question Python with OpenCV error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

Are you sure that the input frame for cvtColor isn't empty? It looks that way since OpenCV can't open your FLV file.

2018-12-02 10:55:52 -0600 commented question Machine Learning: NormalBayesClassifier no results

I will, as soon as the SPAM protection is over.

2018-12-02 09:38:37 -0600 commented question Machine Learning: NormalBayesClassifier no results

That's probably the issue. I replace these lines with an IF test which covers both label types.

2018-12-02 02:38:31 -0600 commented question Machine Learning: NormalBayesClassifier no results

@StevenPuttemans I just wanted to apply good lab practices first: use the Internet :) You set me on the right track! I

2018-12-02 02:31:07 -0600 commented question Machine Learning: NormalBayesClassifier no results

@StevenPuttemans I just wanted to apply good lab practices first: use the Internet :) You set me on the right track! I

2018-12-01 09:55:41 -0600 commented question Machine Learning: NormalBayesClassifier no results

I know, he's a famous guy here :) It's indeed the 5th session of 2018_labo_beeldinterpretatie.

2018-12-01 09:55:08 -0600 commented question Machine Learning: NormalBayesClassifier no results

I know, he's a famous guy here :) It's indeed the 5th session of 2018_labo_beeldinterpretatie

2018-12-01 09:54:19 -0600 edited question Machine Learning: NormalBayesClassifier no results

Machine Learning: NormalBayesClassifier no results I created a small OpenCV program with 3 Machine Learning classifiers

2018-12-01 09:51:36 -0600 commented question Machine Learning: NormalBayesClassifier no results

That's true!

2018-12-01 09:50:19 -0600 received badge  Editor (source)
2018-12-01 09:50:19 -0600 edited question Machine Learning: NormalBayesClassifier no results

Machine Learning: NormalBayesClassifier no results I created a small OpenCV program with 3 Machine Learning classifiers

2018-12-01 09:46:32 -0600 commented question Machine Learning: NormalBayesClassifier no results

I will do that, I did it on pastebin to avoid a very long question full of code.

2018-12-01 09:39:36 -0600 asked a question Machine Learning: NormalBayesClassifier no results

Machine Learning: NormalBayesClassifier no results I created a small OpenCV program with 3 Machine Learning classifiers