使用opencv进行轮廓分割

时间:2017-06-29 10:59:26

标签: c++ image opencv

我目前正致力于使用c ++和opencv 3.0分离重叠对象,到目前为止,我已经能够找到轮廓和角点。现在我需要根据角点分离这些对象并将它们放入单独的向量中。 所以任何帮助将不胜感激!!

#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<algorithm>

using namespace cv;
using namespace std;

/// Global variables
Mat src, src_gray;
int thresh = 100;
int max_thresh = 255;
int maxCorners =14;
int maxTrackbar =14;

RNG rng(12345);
char* source_window = "Image";

/// Function header
void goodFeaturesToTrack_Demo(int, void*);
/// Function header
void thresh_callback(int, void*);
void printvec(vector<int>& contours){
    for (int i = 0; i < contours.size(); i++){
        cout << contours[i] << " ";
    }
    cout << endl;
}

/** @function main */
int main(int argc, char** argv)
{
    /// Load source image and convert it to gray
    src = imread("demo.png", 1);
    cvtColor(src, src_gray, COLOR_BGR2GRAY);
    /// Convert image to gray and blur it
    cvtColor(src, src_gray, CV_BGR2GRAY);
    blur(src_gray, src_gray, Size(3,3));


    /// Create Window
    namedWindow(source_window, WINDOW_AUTOSIZE);

    /// Create Trackbar to set the number of corners
    createTrackbar("Max  corners:", source_window, &maxCorners, maxTrackbar, goodFeaturesToTrack_Demo);


    imshow(source_window, src);

    goodFeaturesToTrack_Demo(0, 0);

    waitKey(0);
    return(0);
}


void goodFeaturesToTrack_Demo(int, void*)
{
    if (maxCorners < 1) { maxCorners = 1; }


    vector<Point2f> corners;
    double qualityLevel = 0.01;
    double minDistance = 10;
    int blockSize = 3;
    bool useHarrisDetector = false;
    double k = 0.04;

    /// Copy the source image
    Mat copy;
    copy = src.clone();

    /// Apply corner detection
    goodFeaturesToTrack(src_gray,
        corners,
        maxCorners,
        qualityLevel,
        minDistance,
        Mat(),
        blockSize,
        useHarrisDetector,
        k);


    /// Draw corners detected
    cout << "** Number of corners detected: " << corners.size() << endl;
    int r = 4;
    for (int i = 0; i < corners.size(); i++)
    {
        circle(copy, corners[i], r, Scalar(rng.uniform(0, 255), rng.uniform(0, 255),
            rng.uniform(0, 255)), -1, 8, 0);
    }

    /// Show what you got
    namedWindow(source_window, WINDOW_AUTOSIZE);
    imshow(source_window, copy);

    /// Set the neeed parameters to find the refined corners
    Size winSize = Size(5, 5);
    Size zeroZone = Size(-1, -1);
    TermCriteria criteria = TermCriteria(TermCriteria::EPS + TermCriteria::MAX_ITER, 40, 0.001);

    /// Calculate the refined corner locations
    cornerSubPix(src_gray, corners, winSize, zeroZone, criteria);
    Mat canny_output;
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    /// Detect edges using canny
    Canny(src_gray, canny_output, thresh, thresh * 2, 3);
    /// Find contours
    findContours(canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    /// Write them down

    for (int i = 0; i < corners.size(); i++)
    {
        cout << " -- Refined Corner [" << i << "]  (" << corners[i].x << "," << corners[i].y << ")" << endl;

        float valx = corners[i].x;
        float valy = corners[i].y;
        int xp = round(valx);
        int yp = round(valy);
        cout << " -- Refined Corner [" << i << "]  (" << xp << "," << yp << ")" << endl;
    }
        for (int i = 0; i < contours.size(); i++)
        {
            for (int j = 0; j < contours[i].size(); j++)
            {

                cout << "Point(x,y)=" << contours[i][j].x << "," << contours[i][j].y << endl;

                }
            }

        }

这是代码的结果:enter image description here

这就是我想要的 enter image description here

0 个答案:

没有答案