Skip to content

Instantly share code, notes, and snippets.

@vernwalrahul
Created January 9, 2017 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vernwalrahul/76a074cd17e270442639766be063f4fa to your computer and use it in GitHub Desktop.
Save vernwalrahul/76a074cd17e270442639766be063f4fa to your computer and use it in GitHub Desktop.
// Marker Task.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include<opencv2\opencv.hpp>
#include<stdio.h>
#include<iostream>
using namespace std;
using namespace cv;
struct hsv
{
int h, s, v;
}lv = { 0,0,0 }, hv = { 15,255,255 };
Mat orig;
Mat binary(480,640,0);
void detect(Mat img)
{
inRange(img, Scalar(lv.h, lv.s, lv.v), Scalar(hv.h, hv.s, hv.v), binary);
erode(binary, binary, Mat(), Point(-1, -1), 4);
dilate(binary, binary, Mat(), Point(-1, -1), 6 );
imshow("binary: ", binary);
waitKey(50);
}
void getcontour(Mat binary)
{
Mat output(binary.rows, binary.cols, CV_8UC1,Scalar(0));
vector<vector<Point>> contours;
vector<vector<Point>> cnt;
vector<RotatedRect> boundrect;
cnt.resize(1);
boundrect.resize(1);
vector<Vec4i> hierarchy;
Moments mu;
int i;
//cvtColor(frame, gray, CV_BGR2GRAY);
//imshow("gray", gray);
//cout << "before find contour: ";
findContours(binary, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(-1, -1));
//cout << "size of contour: ="<< contours.size() << endl;
for (i = 0; i < contours.size(); i++)
{
//approxPolyDP(contours[i], cnt[0], .02*arcLength(contours[i], true), true);
//cout << arcLength(cnt[0], true) << endl;
if (arcLength(contours[i], true)>100)
{
boundrect[0] = minAreaRect(Mat(contours[i]));
Point2f rect_points[4]; boundrect[0].points(rect_points);
for (int j = 0; j < 4; j++)
line(output, rect_points[j], rect_points[(j + 1) % 4], 255, 1, 8);
}
}
imshow("output: ", output);
waitKey(50);
}
int main()
{
Mat frame;
VideoCapture vc("G:\\marker\\task_marker_22_seconds.avi");
vc >> frame;
//Mat HSV(frame.rows, frame.cols, CV_8UC3);
cout << "r=" << frame.rows << " c=" << frame.cols << endl;
Mat HSV(frame.rows, frame.cols, CV_8UC3);
while (frame.data)
{
imshow("orig: ", frame);
cvtColor(frame, HSV, CV_BGR2HSV);
//cvtColor(frame, HSV, CV_BGR2HSV);
detect(HSV);
//detect(frame);
getcontour(binary);
vc >> frame;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment