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:
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.