As some kind of \"holiday project\" I\'m playing around with OpenCV and want to detect and measure stuff.
Current workflow (early stage - detection):
This might be kind of a hack, but since it's a "holiday project", I'll post it anyway :)
Have you tried isolating the background and then inverting the mask (this would assume anything not background is an object, but it might work for what you want).
Below is the result I got using the OpenCV inRange function:
You might want to smooth the image (pre-process) with GuassianBlur to get rid of some of the jagginess. I used a bigger dilation kernel than erosion kernel (5x5 vs. 3x3) to get rid of some noisy pixels. The smoothing might help this also tweaking the thresholds could make the erosion unnecessary. But, that should get you started.
Finally, here is my quick little code snippet I used to find this range:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <vector>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat src = imread("test.jpg");
int rh = 255, rl = 100, gh = 255, gl = 0, bh = 70, bl = 0;
string windowName = "background";
namedWindow(windowName);
createTrackbar("rh", windowName, &rh, 255);
createTrackbar("rl", windowName, &rl, 255);
createTrackbar("gh", windowName, &gh, 255);
createTrackbar("gl", windowName, &gl, 255);
createTrackbar("bh", windowName, &bh, 255);
createTrackbar("bl", windowName, &bl, 255);
// for dilation
Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));
Mat bgIsolation;
int key = 0;
do
{
inRange(src, Scalar(bl, gl, rl), Scalar(bh, gh, rh), bgIsolation);
bitwise_not(bgIsolation, bgIsolation);
erode(bgIsolation, bgIsolation, Mat());
dilate(bgIsolation, bgIsolation, element);
imshow(windowName, bgIsolation);
key = waitKey(33);
} while((char)key != 27);
waitKey();
return 0;
}
Enjoy the holiday project! Looks fun :)