Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Detecting touching objects

Hello,

I am trying to detect touching objects. I am following the tutorial at opencv.org but I couldn't manage to separate the objects.

Right now I am working with playing cards, and I first dilate the image a few times then erode it to get rid of inner parts. Then I follow the tutorial.

I am using OpenCV 2.4.11.

Here is my whole code:

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

using namespace std;
using namespace cv;

int morpho_elem = 1; // 0: MORPH_RECT, 1: MORPH_CROSS, 2: MORPH_ELLIPSE
int morpho_size = 3;
int morpho_qty = 9;

int const smooth_kernel = 11;

int main(int, char** argv)
{
    // Load the image
    Mat src = imread("2cardsT2.png");

    Mat src_smooth;
    for (int i = 1; i < smooth_kernel; i = i + 2)
    {
        medianBlur(src, src_smooth, i);
    }

    Mat src_gray;
    cvtColor(src_smooth, src_gray, CV_BGR2GRAY);

    Mat src_thresh;
    threshold(src_gray, src_thresh, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

    int dilation_type;
    if (morpho_elem == 0){ dilation_type = MORPH_RECT; }
    else if (morpho_elem == 1){ dilation_type = MORPH_CROSS; }
    else if (morpho_elem == 2) { dilation_type = MORPH_ELLIPSE; }

    Mat dilation_dst, erosion_dst;

    Mat element = getStructuringElement(dilation_type,
        Size(2 * morpho_size + 1, 2 * morpho_size + 1),
        Point(morpho_size, morpho_size));

    // Apply the dilation operation
    dilate(src_thresh, dilation_dst, element);
    for (int i = 1; i < morpho_qty; i++)
    {
        dilate(dilation_dst, dilation_dst, element);
    }

    // Apply the erosion operation
    erode(dilation_dst, erosion_dst, element);
    for (int i = 1; i < morpho_qty; i++)
    {
        erode(erosion_dst, erosion_dst, element);
    }

    // Perform the distance transform algorithm
    Mat dist;
    distanceTransform(erosion_dst, dist, CV_DIST_L2, 3);

    // Normalize the distance image for range = {0.0, 1.0} so we can visualize and threshold it
    normalize(dist, dist, 0, 1., NORM_MINMAX);

    // Threshold to obtain the peaks
    // This will be the markers for the foreground objects
    threshold(dist, dist, .4, 1., CV_THRESH_BINARY);

    // Dilate a bit the dist image
    Mat kernel1 = Mat::ones(3, 3, CV_8UC1);
    dilate(dist, dist, kernel1);

    // Create the CV_8U version of the distance image
    // It is needed for findContours()
    Mat dist_8u;
    dist.convertTo(dist_8u, CV_8U);

    // Find total markers
    vector<vector<Point> > contours;
    findContours(dist_8u, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    // Create the marker image for the watershed algorithm
    Mat markers = Mat::zeros(dist.size(), CV_32SC1);

    // Draw the foreground markers
    for (size_t i = 0; i < contours.size(); i++)
        drawContours(markers, contours, static_cast<int>(i), Scalar::all(static_cast<int>(i)+1), -1);

    // Draw the background marker
    circle(markers, Point(5, 5), 3, CV_RGB(255, 255, 255), -1);

    // Perform the watershed algorithm
    watershed(src, markers);
    Mat mark = Mat::zeros(markers.size(), CV_8UC1);
    markers.convertTo(mark, CV_8UC1);
    bitwise_not(mark, mark);

    // image looks like at that point
    // Generate random colors
    vector<Vec3b> colors;
    for (size_t i = 0; i < contours.size(); i++)
    {
        int b = theRNG().uniform(0, 255);
        int g = theRNG().uniform(0, 255);
        int r = theRNG().uniform(0, 255);
        colors.push_back(Vec3b((uchar)b, (uchar)g, (uchar)r));
    }

    // Create the result image
    Mat dst = Mat::zeros(markers.size(), CV_8UC3);

    // Fill labeled objects with random colors
    for (int i = 0; i < markers.rows; i++)
    {
        for (int j = 0; j < markers.cols; j++)
        {
            int index = markers.at<int>(i, j);
            if (index > 0 && index <= static_cast<int>(contours.size()))
                dst.at<Vec3b>(i, j) = colors[index - 1];
            else
                dst.at<Vec3b>(i, j) = Vec3b(0, 0, 0);
        }
    }

    // Visualize the final image
    imshow("Final Result", dst);
    waitKey(0);
    return 0;
}

Image 1: image description

Image 2: image description

Result 1: image description

Result 2: image description