1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
// ROM.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include "opencv2\core\core.hpp"
#include <math.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap(-1); //capture the video from webcam
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the web cam" << endl;
return -1;
}
namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
int iLowH = 15;
int iHighH = 45;
int iLowS = 100;
int iHighS = 255;
int iLowV = 100;
int iHighV = 255;
//Create trackbars in "Control" window
createTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
createTrackbar("HighH", "Control", &iHighH, 179);
createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
createTrackbar("HighS", "Control", &iHighS, 255);
createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
createTrackbar("HighV", "Control", &iHighV, 255);
int iLastX = -1;
int iLastY = -1;
//Capture a temporary image from the camera
Mat imgTmp;
cap.read(imgTmp);
//Create a black image with the size as the camera output
//Mat imgLines = Mat::zeros(imgTmp.size(), CV_8UC3);;
while (true)
{
Mat imgOriginal;
bool bSuccess = cap.read(imgOriginal); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
Mat imgHSV;
cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV
Mat imgThresholded;
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image
//morphological opening (removes small objects from the foreground)
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));
dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));
//morphological closing (removes small holes from the foreground)
dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));
erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(8, 8)));
//Calculate the moments of the thresholded image
Moments oMoments = moments(imgThresholded);
double dM01 = oMoments.m01;
double dM10 = oMoments.m10;
double dArea = oMoments.m00;
//if (dArea > 10000)
//{
//calculate the position of hand
double posX = dM10 / dArea;
double posY = dM01 / dArea;
//vector<Point2f> mc(dArea);
//for (int i = 0; i < dArea; i++)
//{
//mc[i] = Point2f(dM10 / dArea, dM01 / dArea);
//circle(imgThresholded, mc[i], 8, Scalar(255), -1, 8, 0);
//int x1 = 400;
//int y1 = 200;
double x2 = posX;
double y2 = posY;
double X = (x2 - 200);
double Y = (y2 - 200);
Mat image = Mat::zeros(400, 400, CV_8UC3);
line(image, CvPoint(200, 200), CvPoint(400, 200), Scalar(110, 220, 0), 2, 8);
line(image, CvPoint(200, 200), CvPoint(X, Y), Scalar(110, 220, 0), 2, 8);
double angle = atan2(Y, X);
double b = (angle * 180 / 3.14159265);
double a = abs((angle * 180 / 3.14159265));
cout << a;
//if (b < 0) {
//b = abs(b);
//}
//imshow("Image", image);
imshow("Thresholded Image", imgThresholded); //show the thresholded image
imgOriginal = imgOriginal + image;
imshow("Original", imgOriginal);
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
return 0;
}
}
|