使用OpenCV和C ++查找沿轮廓的点的坐标

时间:2017-04-11 22:49:54

标签: c++ opencv contour

我是使用OpenCV和C ++的新手,我已处理图片并提取图片中的轮廓。然后我在新的黑框上绘制轮廓。具体来说,我用红色绘制轮廓。我想获得黑色框架中每个和所有红色像素的坐标并将其存储在一个数组中。我需要这方面的帮助,代码是首选。提前谢谢。

这是我的代码:

#include <iostream>
#include <Windows.h>
#include <sstream>

#include <opencv2/opencv.hpp>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>

using namespace cv;
using namespace std;

void on_trackbar(int, void*);
void createTrackbars();
void toggle(int);

const int MAX_NUM_OBJECTS = 500;

const int FRAME_WIDTH = 900;
const int FRAME_HEIGHT = 600;

const int MIN_OBJECT_AREA = 20 * 20;
const int MAX_OBJECT_AREA = FRAME_HEIGHT*FRAME_WIDTH / 1.5;

Point middle;

int l_MIN = 30;
int l_MAX = 165;
int a_MIN = 139;
int a_MAX = 165;
int b_MIN = 136;
int b_MAX = 172;

int kerode = 2;
int kdilate = 8;

bool showchangedframe = true;

int main(int argc, char** argv)
{
    createTrackbars();
    on_trackbar(0, 0);

    int x, y;
    Mat frame, labframe, rangeframe;
    Mat newframe, newrf;
    int key;

    while ((key = waitKey(30)) != 27)
    {
        toggle(key);
        frame = imread(argv[1], 1);
        newframe = imread(argv[1], 1);
        dframe = imread(argv[1], 1);
        newframe = Scalar(0, 0, 0);
        dframe = Scalar(0, 0, 0);
        cvtColor(frame, labframe, COLOR_BGR2Lab);
        inRange(labframe, Scalar(l_MIN, a_MIN, b_MIN), Scalar(l_MAX, a_MAX, b_MAX), rangeframe);

        erode(rangeframe, rangeframe, getStructuringElement(MORPH_RECT, Size(kerode, kerode)));
        dilate(rangeframe, rangeframe, getStructuringElement(MORPH_RECT, Size(kdilate, kdilate)));

        newrf = rangeframe.clone();

        int largest_area = 0;
        int largest_contour_index = 0;
        vector<vector<Point> > contours;

        findContours(newrf, contours, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

        vector<Moments> mu(contours.size()); //get moments
        for (int i = 0; i < contours.size(); i++)
        {
            mu[i] = moments(contours[i], false);
        }

        vector<Point2f> mc(contours.size()); //get centers
        for (int i = 0; i < contours.size(); i++)
        {
            mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
        }

        for (int i = 0; i < contours.size(); i++) //iterate through each contour. 
        {
            double a = contourArea(contours[i], false); //Find the area of contour

            if (a>largest_area)
            {
                largest_area = a;
                largest_contour_index = i; //Store the index of largest contour
            }
        }

        drawContours(newframe, contours, largest_contour_index, CV_RGB(255, 0, 0), 4);
        circle(newframe, mc[largest_contour_index], 5, CV_RGB(255, 255, 0), -1, 8, 0);

        imshow("Detected", newframe);

        if (showchangedframe)
            imshow("Camera", frame);
        else
            imshow("Camera", rangeframe);
    }
}

void on_trackbar(int, void*)
{
    if (kerode == 0)
        kerode = 1;
    if (kdilate == 0)
        kdilate = 1;
}

void createTrackbars()
{
    String trackbarWindowName = "TrackBars";
    namedWindow(trackbarWindowName, WINDOW_NORMAL);
    createTrackbar("l_MIN", trackbarWindowName, &l_MIN, l_MAX, on_trackbar);
    createTrackbar("l_MAX", trackbarWindowName, &l_MAX, l_MAX, on_trackbar);
    createTrackbar("a_MIN", trackbarWindowName, &a_MIN, a_MAX, on_trackbar);
    createTrackbar("a_MAX", trackbarWindowName, &a_MAX, a_MAX, on_trackbar);
    createTrackbar("b_MIN", trackbarWindowName, &b_MIN, b_MAX, on_trackbar);
    createTrackbar("b_MAX", trackbarWindowName, &b_MAX, b_MAX, on_trackbar);
    createTrackbar("Erosion", trackbarWindowName, &kerode, 31, on_trackbar);
    createTrackbar("Dilation", trackbarWindowName, &kdilate, 31, on_trackbar);
}

void toggle(int key)
{
    if (key == 'r')
        showchangedframe = !showchangedframe;
}

这是输出:

Output

1 个答案:

答案 0 :(得分:4)

用CV_CHAIN_APPROX_NONE(参见doc)替换CV_CHAIN_APPROX_SIMPLE以获取findContours结果中的每个轮廓点。

apörox_simple通过用起点和终点替换直线部分来减少轮廓点的数量。 approx_none根本不会减少轮廓点的数量。

分数在你的

vector<vector<Point> > contours

您可以通过以下方式访问它们:

for (size_t cC = 0; cC < contours.size(); ++cC)
    for(size_t cP =0; cP < contours[cC].size(); cP++)
    {
         Point currentContourPixel = contours[cC][cP];
         // do whatever you want
    }