Background removal improvement

asked 2017-12-28 08:24:27 -0600

Petyu gravatar image

updated 2017-12-28 08:56:33 -0600

LBerger gravatar image

I would like to remove the background of bees to be able to do further analyses with them. Input file C:\fakepath\mite2.jpg My current solution is:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char** argv)
{
    Mat mat = imread("./mite2.jpg ");
    Mat preimgThresholded, hsv_image;
    cvtColor(mat, hsv_image, CV_BGR2HSV);
    inRange(hsv_image, Scalar(13, 30, 30), Scalar(180, 250, 250), preimgThresholded);

    erode(preimgThresholded, preimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(6, 6)) );
    dilate( preimgThresholded, preimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(6, 6)) );

    //morphological closing (fill small holes in the foreground)
    dilate( preimgThresholded, preimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(50, 50)) );
    //morphological opening (remove small objects from the foreground)
    erode(preimgThresholded, preimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(50, 50)) );

    //morphological opening (remove small objects from the foreground)
    erode(preimgThresholded, preimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(70, 70)) );
    dilate( preimgThresholded, preimgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(70, 70)) );

    Mat mat_filtered;
    mat.copyTo(mat_filtered, preimgThresholded);
    cvtColor(mat_filtered, hsv_image, CV_BGR2HSV);

    imshow("Cells", mat_filtered);
    waitKey();
}

This is the current output: image description

My current solution doesn't robust at all. (depends on color and doesn't work properly)

I would really appreciate if someone could help me how to improve my current solution. Any ideas, suggestions are welcomed.

edit retag flag offensive close merge delete

Comments

Do you perhaps have a video? Background subtraction works with either a video, color differences, or depth. If you don't have those, you probably should use a detector/classifier such as the Cascade Classifier or something else you train.

Tetragramm gravatar imageTetragramm ( 2017-12-28 11:33:14 -0600 )edit

Thanks for your comment. The background subtraction is a really good idea. I can create two images with bees and without bees about the same comb. I'm just wondering how I can align the honeycomb on the two pictures. (I have to shake off the bees and put the honeycomb back to the almost same place, but there will be some pixel differences)

Petyu gravatar imagePetyu ( 2018-01-01 10:44:59 -0600 )edit