ubaid
Posts: 4
Joined: Sat Aug 12, 2017 10:27 am

Compiling opencv c++ source code

Sun Mar 18, 2018 4:35 pm

Code: Select all

///////////////////////////////////////////////////////////////////////////////////
/// GestureDetection.cpp
///
/// Detects hand gestures and controls respective GPIO
///
/// Author: Sriram Emarose
/// Contact: sriram.emarose@gmail.com
///
///////////////////////////////////////////////////////////////////////////////////
 
#include <iostream>
#include <wiringPi.h>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
void DetectGesture(cv::Mat frame);
 
int controlPin1 = 28; // Pin 38 in Rpi3
int controlPin2 =29; // Pin 40 in Rpi3
 
int main()
{
 
if(wiringPiSetup() == -1)
{
std::cout<<"Unable to setup GPIOs access \n";
return 1;
}
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
 
cv::Mat img;
 
cv::VideoCapture capture(0);
{
while(capture.isOpened())
{
capture >> img;
 
if(!img.empty())
{
DetectGesture(img);
cv::waitKey(1);
}
 
if(i == 27)
break;
 
}
}
return 0;
}
void DetectGesture(cv::Mat frame)
{
cv::Mat img, binary;
cvtColor(frame, img, CV_BGR2GRAY);
 
//thresholding
cv::threshold(img, binary, 35, 255, cv::THRESH_BINARY_INV);
 
//Contour storage
vector<vector<Point>> Contours;
vector<Vec4i> hierarchy;
 
//convex Hull storage
vector<vector<Point>> cnvxHull(Contours.size());
vector<vector<int>> cnvxHullsI(Contours.size());
vector<vector<Vec4i>> defects(Contours.size());
 
//morphological transformations
erode(binary, binary, getStructuringElement(MORPH_RECT, Size(3, 3)));
erode(binary, binary, getStructuringElement(MORPH_RECT, Size(3, 3)));
 
dilate(binary, binary, getStructuringElement(MORPH_RECT, Size(8, 8)));
dilate(binary, binary, getStructuringElement(MORPH_RECT, Size(8, 8)));
 
//finding the contours required
findContours(binary, Contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0, 0));
 
//finding the contour of largest area and storing its index
int biggestContour = 0;
int maxarea = 0;
 
for (int i = 0; i < Contours.size(); i++)
{
double currentArea = contourArea(Contours[i]);
if ( currentArea > maxarea)
{
maxarea = currentArea;
biggestContour = i;
}
}
for (int i = 0; i < Contours.size(); i++)
{
convexhull(Contours[i], cnvxHull[i], false);
convexhull(Contours[i], cnvxHullsI[i], false);
if(cnvxHullsI[i].size() > 3 )
{
convexityDefects(Contours[i], cnvxHullsI[i], defects[i]);
}
}
 
std::vector<cv::Point> peakCount;
 
if (maxarea > 100){
 
/// Draw convexity Defects
for(int j=0; j<defects[biggestContour].size(); ++j)
{
const Vec4i& v = defects[biggestContour][j];
float depth = v[3] / 256;
if (depth > 10) //
{
int startidx = v[0];
Point ptStart(Contours[biggestContour][startidx]);
peakCount.push_back(ptStart);
 
int endidx = v[1];
Point ptEnd(Contours[biggestContour][endidx]);
 
int faridx = v[2];
Point ptFar(Contours[biggestContour][faridx]);
 
line(frame, ptStart, ptEnd, Scalar(0, 255, 0), 5);
line(frame, ptStart, ptFar, Scalar(0, 255, 0), 5);
line(frame, ptEnd, ptFar, Scalar(0, 255, 0), 5);
circle(frame, ptFar, 4, Scalar(255, 0, 0), 5);
circle(frame, ptStart, 6, Scalar(0, 0, 255), 5);
 
}
}
}
 
if(peakCount.size() > 4)
{
digitalWrite(controlPin1, 1);
 
}
else if(peakCount.size() == 4)
{
digitalWrite(controlPin2, 1);
digitalWrite(controlPin1, 0);
}
else
{
digitalWrite(controlPin1, 0);
digitalWrite(controlPin2, 0);
}
 
cv::imshow("Live", frame);
 
}
Hello dear all,

I am trying to get this gesture detection source code (opencv, C++) to work on my RPi, i don't understand how to get it to run..?
Kindly help me out.

Thanks,

User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Compiling opencv c++ source code

Sun Mar 18, 2018 9:50 pm

What problems are you having? What have you tried and what errors did you get?

The main problem you'll have is that the code as you showed won't compile, it looks like you copy-pasted the code from a web page as all leading whitespace is missing (not that that prevents compiling but it does make it hard to read) and every occurence of

Code: Select all

<
has been replaced with

Code: Select all

&amp;lt;
likewise with > but with gt rather than lt. There may be other characters that have been replaced too.
She who travels light — forgot something.

ubaid
Posts: 4
Joined: Sat Aug 12, 2017 10:27 am

Re: Compiling opencv c++ source code

Tue Mar 20, 2018 4:56 am

Hello,

Thanks, That was it mostly.
I was able to resolve most of the compilation errors and here's the code:

Code: Select all

#include <iostream>
#include <wiringPi.h>
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
void DetectGesture(cv::Mat frame);
 
int controlPin1 = 28; // Pin 38 in Rpi3
int controlPin2 =29; // Pin 40 in Rpi3
 
int main()
{
 
if(wiringPiSetup() == -1)
{
std::cout<<"Unable to setup GPIOs access \n";
return 1;
}
pinMode(controlPin1, OUTPUT);
pinMode(controlPin2, OUTPUT);
 
cv::Mat img;
 
cv::VideoCapture capture(0);
{
while(capture.isOpened())
{
capture >> img;
 
if(!img.empty())
{
DetectGesture(img);
cv::waitKey(1);
}
 
if(i == 27)
break;
 
}
}
return 0;
}
void DetectGesture(cv::Mat frame)
{
cv::Mat img, binary;
cvtColor(frame, img, CV_BGR2GRAY);
 
//thresholding
cv::threshold(img, binary, 35, 255, cv::THRESH_BINARY_INV);
 
//Contour storage
vector<vector<Point>> Contours;
vector<Vec4i> hierarchy;
 
//convex Hull storage
vector<vector<Point>> cnvxHull(Contours.size());
vector<vector<int>> cnvxHullsI(Contours.size());
vector<vector<Vec4i>> defects(Contours.size());
 
//morphological transformations
erode(binary, binary, getStructuringElement(MORPH_RECT, Size(3, 3)));
erode(binary, binary, getStructuringElement(MORPH_RECT, Size(3, 3)));
 
dilate(binary, binary, getStructuringElement(MORPH_RECT, Size(8, 8)));
dilate(binary, binary, getStructuringElement(MORPH_RECT, Size(8, 8)));
 
//finding the contours required
findContours(binary, Contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE, Point(0, 0));
 
//finding the contour of largest area and storing its index
int biggestContour = 0;
int maxarea = 0;
 
for (int i = 0; i < Contours.size(); i++)
{
double currentArea = contourArea(Contours[i]);
if ( currentArea > maxarea)
{
maxarea = currentArea;
biggestContour = i;
}
}
for (int i = 0; i < Contours.size(); i++)
{
convexhull(Contours[i], cnvxHull[i], false);
convexhull(Contours[i], cnvxHullsI[i], false);
if(cnvxHullsI[i].size() > 3 )
{
convexityDefects(Contours[i], cnvxHullsI[i], defects[i]);
}
}
 
std::vector<cv::Point> peakCount;
 
if (maxarea > 100){
 
/// Draw convexity Defects
for(int j=0; j<defects[biggestContour].size(); ++j)
{
const Vec4i<> v = defects[biggestContour][j];
float depth = v[3] / 256;
if (depth > 10) //
{
int startidx = v[0];
Point ptStart(Contours[biggestContour][startidx]);
peakCount.push_back(ptStart);
 
int endidx = v[1];
Point ptEnd(Contours[biggestContour][endidx]);
 
int faridx = v[2];
Point ptFar(Contours[biggestContour][faridx]);
 
line(frame, ptStart, ptEnd, Scalar(0, 255, 0), 5);
line(frame, ptStart, ptFar, Scalar(0, 255, 0), 5);
line(frame, ptEnd, ptFar, Scalar(0, 255, 0), 5);
circle(frame, ptFar, 4, Scalar(255, 0, 0), 5);
circle(frame, ptStart, 6, Scalar(0, 0, 255), 5);
 
}
}
}
 
if(peakCount.size() > 4)
{
digitalWrite(controlPin1, 1);
 
}
else if(peakCount.size() == 4)
{
digitalWrite(controlPin2, 1);
digitalWrite(controlPin1, 0);
}
else
{
digitalWrite(controlPin1, 0);
digitalWrite(controlPin2, 0);
}
 
cv::imshow("Live", frame);
 
}

Now i am having a syntactical error with the line:

Code: Select all

const Vec4i<> v = defects[biggestContour][j];

and also i is undefined at:

Code: Select all

if(i == 27)
Kindly assist.

Here's the compilation errors
pi@raspberrypi:~ $ g++ $(pkg-config --cflags --libs opencv) gesture.c -o programgesture.c: In function ‘int main()’:
gesture.c:48:4: error: ‘i’ was not declared in this scope
if(i == 27)
^
gesture.c: In function ‘void DetectGesture(cv::Mat)’:
gesture.c:64:20: error: ‘>>’ should be ‘> >’ within a nested template argument list
vector<vector<Point>> Contours;
^
gesture.c:68:20: error: ‘>>’ should be ‘> >’ within a nested template argument list
vector<vector<Point>> cnvxHull(Contours.size());
^
gesture.c:69:18: error: ‘>>’ should be ‘> >’ within a nested template argument list
vector<vector<int>> cnvxHullsI(Contours.size());
^
gesture.c:70:20: error: ‘>>’ should be ‘> >’ within a nested template argument list
vector<vector<Vec4i>> defects(Contours.size());
^
gesture.c:97:43: error: ‘convexhull’ was not declared in this scope
convexhull(Contours, cnvxHull, false);
^
gesture.c:112:7: error: ‘cv::Vec4i {aka cv::Vec<int, 4>}’ is not a template
const Vec4i<> v = defects[biggestContour][j];

User avatar
KLL
Posts: 1453
Joined: Wed Jan 09, 2013 3:05 pm
Location: thailand
Contact: Website

Re: Compiling opencv c++ source code

Tue Mar 20, 2018 5:22 am


User avatar
Paeryn
Posts: 2987
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Compiling opencv c++ source code

Tue Mar 20, 2018 10:59 am

ubaid wrote:
Tue Mar 20, 2018 4:56 am
Now i am having a syntactical error with the line:

Code: Select all

const Vec4i<> v = defects[biggestContour][j];
You changed that one wrong, in the copied text it was written

Code: Select all

const Vec4i &amp;amp; v =
&amp;amp; should just become a single ampersand, nothing to do with angle-brackets (less than or greater than) which you substituted.
ubaid wrote:
Tue Mar 20, 2018 4:56 am
and also i is undefined at:

Code: Select all

if(i == 27)
Kindly assist.
I can't help there, that's an error in the original code as the variable i doesn't exist in that function.

The errors about using

Code: Select all

>>
instead of

Code: Select all

> >
for nested templates is exactly that, you need to put the space between them if you are compiling using an earlier standard than C++11, prior to that two greater-than symbols together were always seen as the right shift operator.

As KLL said, contact the author and ask them to provide working code (it appears that those html codes were visible on the web page so they didn't even bother to check what they put).
She who travels light — forgot something.

SriramEmarose
Posts: 1
Joined: Thu Apr 12, 2018 5:42 pm

Re: Compiling opencv c++ source code

Thu Apr 12, 2018 5:50 pm

Hello Ubaid,

Apologies for the issues with the code. Looks like special symbols in source has changed when I switched to newer website theme and I did't notice it. Thanks all for noticing and for the help.

I have post the detailed description in github, please check the link below if you are still looking for it :)

https://github.com/SriramEmarose/Gestru ... nition.git

Thanks

Return to “Beginners”