#include "opencv2/highgui/highgui.hpp"
#include <iostream>
/******mansung******/
#include "opencv/cv.h"
#include "opencv/cxcore.h"
#include <deque>
/*******************/
using namespace std;
using namespace cv;
CvSeq* getCirclesInImage(IplImage*, CvMemStorage*, IplImage*);
void drawCircleAndLabel(IplImage*, float*);
bool circlesBeHomies(float*, float*);
const int HISTORY_SIZE = 5;
const int X_THRESH = 300;
const int Y_THRESH = 100;
const int R_THRESH = 50;
const int MATCHES_THRESH = 3;
const int WIDTH = 640;
const int HEIGHT = 480;
const int VIEW = 1;
const int TEXT = 0;
int VIEWMODE = TEXT;
int main(int argc, char** argv){
/*VideoCapture cap(-1);
if (!cap.isOpened())
{
cout << "Cannot open camera" << endl;
return -1;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
namedWindow("Output",CV_WINDOW_AUTOSIZE);
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "Cannot read a frame from camera" << endl;
break;
}
imshow("Output", frame);
if (waitKey(30) == 27)
{
cout << "Exit" << endl;
break;
}
}*/
CvCapture* capture = 0; //cam
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, WIDTH );
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
IplImage* frame = 0;
int idx = 0, maxradius = 0;
capture = cvCaptureFromCAM(0);
if(!capture) {
cout<< "cam cannot connect" << endl;
return -1;
}
// cvNamedWindow("raw_video", CV_WINDOW_AUTOSIZE);
cvNamedWindow("processed_video", CV_WINDOW_AUTOSIZE);
//namedWindow("aa", CV_WINDOW_AUTOSIZE);
CvMemStorage* storage = cvCreateMemStorage(0);
IplImage* grayscaleImg = cvCreateImage(cvSize(WIDTH, HEIGHT), 8/*depth*/, 1/*channel*/);
CvPoint track1 = {-1, -1};
CvPoint track2 = {-1, -1};
float rad1 = -1;
float rad2 = -1;
int key = 0;
deque<CvSeq*> samples;
while(1) {
if (waitKey(30) == 27) {
cout<<"ESC EXIT"<<endl;
break;
}
frame = cvQueryFrame(capture);
if(!frame) {
cout<<"Frame NULL"<<endl;
break;
}
//cvShowImage("raw_video", frame);
//cout<<"d"<<endl;
CvSeq* circles = getCirclesInImage(frame, storage, grayscaleImg);
//cout<<"a"<<endl;
cout<<circles->total<<endl;
if(circles->total > 0) {
int match = 0;
float* p = (float*)cvGetSeqElem(circles, idx);
float x = p[0];
float y = p[1];
float r = p[2];
if(x-r < 0 || y-r < 0 || x+r >= frame->width || y+r >= frame->height) {
cout<<"Pointing Over Range"<<endl;
continue;
}
for(int j=0; j<samples.size(); j++) {
CvSeq* oldSample = samples[j];
for(int k=0; k<oldSample->total; k++) {
float* p2 = (float*)cvGetSeqElem(oldSample, k);
if(circlesBeHomies(p, p2)) {
match++;
break;
}
}
}
if(match > MATCHES_THRESH) {
if(VIEWMODE) {
cvSetImageROI(frame, cvRect(x-r, y-r, 2*r, 2*r));
cvResetImageROI(frame);
drawCircleAndLabel(frame, p);
}
cout<<"Circle Detected!!"<<endl;
if(p[0]!=0) {
//int turn = ( p[0]<0?-1*p[0]:p[0]);
cout<<"go straight, turn [X,Y]=[ "<<WIDTH/2-p[0]<<","<<HEIGHT/2-p[1]<<"]"<<endl;
}
}
}
samples.push_back(circles);
if(samples.size() > HISTORY_SIZE) {
samples.pop_front();
}
if(VIEWMODE)
cvShowImage("processed_video", frame);
// cvShowImage("processed_video", grayscaleImg);
}
return 0;
}
CvSeq* getCirclesInImage(IplImage* frame, CvMemStorage* storage, IplImage* grayscaleImg) {
//img2gray
cvCvtColor(frame, grayscaleImg, CV_BGR2GRAY);
//imgSmooth, Gaussian filter, less noise
cvSmooth(grayscaleImg, grayscaleImg, CV_GAUSSIAN, 7, 9);
//detect circle in img
CvSeq* circles = cvHoughCircles(
grayscaleImg,
storage,
CV_HOUGH_GRADIENT,
2,
grayscaleImg->height/4,
200,
100
);
return circles;
}
void drawCircleAndLabel(IplImage* frame, float* p) {
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1, 1, 0.0, 1, 8);
cvCircle( frame, cvPoint(cvRound(p[0]), cvRound(p[1])), 1, CV_RGB(1,0,0), 3, 8, 0 ); //center point
cvCircle( frame, cvPoint(cvRound(p[0]), cvRound(p[1])), cvRound(p[2]), CV_RGB(0,255,0), 3, 8, 0 ); //circle
}
bool circlesBeHomies(float* c1, float* c2) {
bool ret = (abs(c1[0]-c2[0]) < X_THRESH)
&& (abs(c1[1]-c2[1]) < Y_THRESH)
&& (abs(c1[2]-c2[2]) < R_THRESH);
// cout<<"ret="<<ret<<endl;
// cout<<"x:"<<(abs(c1[0]-c2[0]))<<",y:"<<(abs(c1[1]-c2[1]))<<",r:"<<(abs(c1[2]-c2[2])) <<endl;
return ret;
}
'소프트웨어 > Linux/Device Driver' 카테고리의 다른 글
ARM] ARM 최적화로 인한 코드 오동작 방어 (4) | 2018.07.23 |
---|---|
rasp] test module (0) | 2014.09.03 |
rasp] opencv설치 (0) | 2014.09.02 |
Linux] sh파일에서 export 안될때 (0) | 2014.08.21 |
Linux] Ubuntu에서 putty처럼 minicom 사용 (0) | 2014.08.05 |
Linux] Ubuntu Chrome Remote Control Error_chrome_remote_desktop_20140805_033253_s_M4NS (1) | 2014.08.05 |
linux] apt-get 404 오류 해결법 (0) | 2014.07.22 |