Ask Your Question
1

Detecting touching objects

asked 2015-10-22 16:13:48 -0600

vitruvius gravatar image

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 ...
(more)
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-10-23 08:49:08 -0600

theodore gravatar image

you need the edges of the cards to be preserved, therefore the smoothing that you are applying is not useful. Instead a sharpening algorithm should be applied. The following code works for the second image but not for the first. You need to find a sharpening kernel/solution that will help you preserve the edges.

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

using namespace cv;
using namespace std;

int main()
{
    cv::Mat src = cv::imread("cards.jpg");
    if (!src.data)
        return -1;

    imshow("src", src);

    // Create binary image from source image
    cv::Mat bw;
    cv::cvtColor(src, bw, CV_BGR2GRAY);

    cv::threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);
    imshow("bin", bw);

    // Fill holes
    cv::floodFill(bw,cv::Point2i(0,0),cv::Scalar(1));
    for(int i=0;i<bw.rows*bw.cols;i++)
    {
        if(bw.data[i]==0)
            bw.data[i]=255;
    }

    cv::threshold(bw, bw, 40, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

    imshow("filled", bw);

    cv::Mat dist;
    cv::distanceTransform(bw, dist, CV_DIST_L2, 3);
    cv::normalize(dist, dist, 0, 1., cv::NORM_MINMAX);
    imshow("Distance Transform Image", dist);

    cv::threshold(dist, dist, .5, 1., CV_THRESH_BINARY);

//    // Dilate a bit the dist image
//    Mat kernel1 = Mat::ones(7, 7, CV_8UC1);
//    dilate(dist, dist, kernel1);
//    erode(dist, dist, kernel1);
//    imshow("Peaks", dist);

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

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

    // Total objects
    int ncomp = contours.size();

    cv::Mat markers = cv::Mat::zeros(dist.size(), CV_32SC1);
    for (int i = 0; i < ncomp; i++)
        cv::drawContours(markers, contours, i, cv::Scalar::all(i+1), -1);

    cv::circle(markers, cv::Point(5,5), 3, CV_RGB(255,255,255), -1);
    imshow("Markers", markers*10000);

    cv::watershed(src, markers);

    // Generate random colors
    std::vector<cv::Vec3b> colors;
    for (int i = 0; i < ncomp; i++)
    {
        int b = cv::theRNG().uniform(0, 255);
        int g = cv::theRNG().uniform(0, 255);
        int r = cv::theRNG().uniform(0, 255);

        colors.push_back(cv::Vec3b((uchar)b, (uchar)g, (uchar)r));
    }

    // Create the result image
    cv::Mat dst = cv::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 <= ncomp)
                dst.at<cv::Vec3b>(i,j) = colors[index-1];
            else
                dst.at<cv::Vec3b>(i,j) = cv::Vec3b(0,0,0);
        }
    }

    cv::imshow("dst", dst);

    cv::waitKey(0);
    return 0;
}

image description

image description

image description

image description

image description

image description

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2015-10-22 16:13:48 -0600

Seen: 3,226 times

Last updated: Oct 23 '15