Reputation: 41
I am new to using OpenCV and C++, I have process a picture and extract the contour in the picture. Then I draw the contour on a new black frame. Specifically, I draw the contour with the colour red. I would like to get the coordinates of each and all the red pixels in the black frame and store it in an array. I need help on this part, codes are preferred. Thanks in advance.
Here is my code:
#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;
}
Here is the output:
Upvotes: 1
Views: 5533
Reputation: 20130
replace CV_CHAIN_APPROX_SIMPLE by CV_CHAIN_APPROX_NONE (see doc) to get every contour point in the findContours result.
apörox_simple reduces the number of contour points by replacing straight parts by start- and end-points. approx_none doesnt reduce the number of contour points at all.
The points are the in your
vector<vector<Point> > contours
you can access them by:
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
}
Upvotes: 4