diff --git a/Techfest_2013/Techfest_2013.sln b/Techfest_2013/Techfest_2013.sln new file mode 100644 index 0000000..40fe5fb --- /dev/null +++ b/Techfest_2013/Techfest_2013.sln @@ -0,0 +1,20 @@ + +Microsoft Visual Studio Solution File, Format Version 11.00 +# Visual C++ Express 2010 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Techfest_2013", "Techfest_2013\Techfest_2013.vcxproj", "{6880EF99-5C77-44C4-957B-D0EA8098DE47}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Win32 = Debug|Win32 + Release|Win32 = Release|Win32 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {6880EF99-5C77-44C4-957B-D0EA8098DE47}.Debug|Win32.ActiveCfg = Debug|Win32 + {6880EF99-5C77-44C4-957B-D0EA8098DE47}.Debug|Win32.Build.0 = Debug|Win32 + {6880EF99-5C77-44C4-957B-D0EA8098DE47}.Release|Win32.ActiveCfg = Release|Win32 + {6880EF99-5C77-44C4-957B-D0EA8098DE47}.Release|Win32.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/Techfest_2013/Techfest_2013/ReadMe.txt b/Techfest_2013/Techfest_2013/ReadMe.txt new file mode 100644 index 0000000..4b25ff9 --- /dev/null +++ b/Techfest_2013/Techfest_2013/ReadMe.txt @@ -0,0 +1,40 @@ +======================================================================== + CONSOLE APPLICATION : Techfest_2013 Project Overview +======================================================================== + +AppWizard has created this Techfest_2013 application for you. + +This file contains a summary of what you will find in each of the files that +make up your Techfest_2013 application. + + +Techfest_2013.vcxproj + This is the main project file for VC++ projects generated using an Application Wizard. + It contains information about the version of Visual C++ that generated the file, and + information about the platforms, configurations, and project features selected with the + Application Wizard. + +Techfest_2013.vcxproj.filters + This is the filters file for VC++ projects generated using an Application Wizard. + It contains information about the association between the files in your project + and the filters. This association is used in the IDE to show grouping of files with + similar extensions under a specific node (for e.g. ".cpp" files are associated with the + "Source Files" filter). + +Techfest_2013.cpp + This is the main application source file. + +///////////////////////////////////////////////////////////////////////////// +Other standard files: + +StdAfx.h, StdAfx.cpp + These files are used to build a precompiled header (PCH) file + named Techfest_2013.pch and a precompiled types file named StdAfx.obj. + +///////////////////////////////////////////////////////////////////////////// +Other notes: + +AppWizard uses "TODO:" comments to indicate parts of the source code you +should add to or customize. + +///////////////////////////////////////////////////////////////////////////// diff --git a/Techfest_2013/Techfest_2013/Techfest_2013.cpp b/Techfest_2013/Techfest_2013/Techfest_2013.cpp new file mode 100644 index 0000000..e9d1dcf --- /dev/null +++ b/Techfest_2013/Techfest_2013/Techfest_2013.cpp @@ -0,0 +1,422 @@ +// Techfest_2013.cpp : Defines the entry point for the console application. +// +//This time we'll be using triangle markers for bot identification. Will consume less space becoz less points to store. + +#include "stdafx.h" +#include + +#include +CvPoint circle[2]; // global variable to store centre of circular tags on bot +CvPoint nr_sq; // global variable to store centre of nearest square + +#include + +#include + +#include + +#include + +using namespace std; + +// Initialize the serial port variable and parameters +HANDLE hPort = CreateFile(TEXT("COM1"), GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0); +DCB dcb={0}; // Create a DCB struct variable +/*********************Function for Sending Data***********************/ + +bool writebyte(char* data) +{ + +DWORD byteswritten; +a1: +//Sleep (100); + dcb.DCBlength = sizeof(dcb); +if (!GetCommState(hPort,&dcb)) + +{ + +cout<<"\nSerial port cant b opened\n"; +goto a1; + +//return false; + +} + +dcb.BaudRate = CBR_9600; //9600 Baud +dcb.ByteSize = 8; //8 data bits +dcb.Parity = NOPARITY; //no parity +dcb.StopBits = ONESTOPBIT; //1 stop + +int retVal=0; + +if (SetCommState(hPort,&dcb)) //If Com port cannot be configured accordingly return false; + retVal = WriteFile(hPort,(int *)data,1,&byteswritten,NULL); //Write the data to be sent to Serial port +//printf("\n BYTES Written %d %d \n",byteswritten,*data); +if (retVal==0) + return false; +else + return true; + +// return true if the data is written +} + +void movent_decide(IplImage *img) // finds angle b/w the square center and the vector of bot orientation +{ + double side1=0.0,side2=0.0,side3=0.0,sum1=0.0; + char data='n'; + + // draw lines to join the centres of circles(vector) to see bot orientation + cvLine(img,circle[0],circle[1],CV_RGB(255,0,0),3,4,0); + // draw line b/w center of red circle to sq center + cvLine(img,circle[0],nr_sq,CV_RGB(255,0,0),3,4,0); + cvLine(img,circle[1],nr_sq,CV_RGB(255,0,0),3,4,0); + side1= sqrt(pow((double)circle[1].y-circle[0].y,2)+pow((double)circle[1].x-circle[0].x,2)); + side2= sqrt(pow((double)circle[1].y-nr_sq.y,2)+pow((double)circle[1].x-nr_sq.x,2)); + side3= sqrt(pow((double)circle[0].y-nr_sq.y,2)+pow((double)circle[1].x-nr_sq.x,2)); + printf("%f %f %f\n",side1,side2,side3); + sum1=side1+side2; + data='r'; + if(writebyte(&data)) + printf("\n%c ",data); + if((sum1-side3)>20 || side2>side3) + { + if(nr_sq.y<=circle[1].y) + { + if(circle[1].x < nr_sq.x) + { + //turn right + data='r'; + if(writebyte(&data)) + printf("\n%c ",data); + } + if(circle[1].x > nr_sq.x) + { + //turn left + data='l'; + if(writebyte(&data)) + printf("\n%c ",data); + } + } + if(nr_sq.y>circle[1].y) + { + if(circle[1].x < nr_sq.x) + { + //turn left + data='l'; + if(writebyte(&data)) + printf("\n%c ",data); + } + if(circle[1].x > nr_sq.x) + { + //turn right + data='r'; + if(writebyte(&data)) + printf("\n%c ",data); + } + } + } + else + { + if(side2>200) + { + // go forward + data='f'; + if(writebyte(&data)) + printf("\n%c ",data); + } + else + { + data='n'; + if(writebyte(&data)) + printf("\n%c ",data); + } + + } + + + + + // calculates and prints the angle between the two lines + //angle=fabs(atan((double)(nr_sq.y-circle[0].y)/(nr_sq.x-circle[0].x))-atan((double)(circle[1].y-circle[0].y)/(circle[1].x-circle[0].x))); + //printf("angle - %f\n",angle*(180/3.146)); +} + + + +IplImage* thatonly(IplImage* m1,IplImage* m2,IplImage* m3,int contrast) // returns the image containing only one particular channel(m3) +{ + cvAdd(m1, m2, m2); + cvSub(m3, m2, m3); + cvScale(m3, m3,contrast ); // increasing contrast + cvDilate(m3, m3, 0,1); + cvErode(m3,m3, 0, 1); + //cvReleaseImage(&m1); + //cvReleaseImage(&m2); + return m3; +} + +CvPoint centre(CvPoint* at ,int n) // calculates and returns centroid of the figure +{ + int x=0,y=0,i; + for(i=0;iRED\n1->GREEN\n2->BLUE"); + + while(true) + { + + img=cvQueryFrame(capture); + if(!img) + break; + //sum = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS); + //printf("\n FPS - %f ",sum); + cvShowImage("Original",img); + red=cvCreateImage(cvGetSize(img), 8, 1); + green=cvCreateImage(cvGetSize(img), 8, 1); + blue=cvCreateImage(cvGetSize(img), 8, 1); + //ronly=cvCreateImage(cvGetSize(img), 8, 1); + //gonly=cvCreateImage(cvGetSize(img), 8, 1); + //bonly=cvCreateImage(cvGetSize(img), 8, 1); + if(prevch==ch) // to know whether slider for channel select has moved or not + { + switch(ch) // if not then update the specific channel related variables + { + case 0: + rcont=contrast; + rthresh=thresh; + break; + case 1: + gcont=contrast; + gthresh=thresh; + break; + case 2: + bcont=contrast; + bthresh=thresh; + break; + } + } + else // if moved then show the new channel's variable values + { + prevch=ch; + switch(ch) + { + case 0: + contrast=rcont; + thresh=rthresh; + cvSetTrackbarPos("Thresh","Threshold",thresh); + cvSetTrackbarPos("Contrast","Threshold",contrast); + break; + case 1: + contrast=gcont; + thresh=gthresh; + cvSetTrackbarPos("Thresh","Threshold",thresh); + cvSetTrackbarPos("Contrast","Threshold",contrast); + break; + case 2: + contrast=bcont; + thresh=bthresh; + cvSetTrackbarPos("Thresh","Threshold",thresh); + cvSetTrackbarPos("Contrast","Threshold",contrast); + break; + } + + } + + + + + cvSplit(img,blue, green, red, NULL); + red=thatonly(blue,green,red,rcont); + ronly=cvCloneImage(red); // cvClone() func is used and ronly=red is not done because then we are just refering to same image and when next cvSplit() executes it creates a problem...... becoz red and ronly are pointers.. + //cvShowImage("Red",ronly); + + + cvSplit(img,blue, green, red, NULL); + blue=thatonly(green,red,blue,bcont); + bonly=cvCloneImage(blue); + //cvShowImage("Blue",bonly); + + cvSplit(img,blue, green, red, NULL); + green=thatonly(red,blue,green,gcont); + gonly=cvCloneImage(green); + //cvShowImage("Green",gonly); + + cvReleaseImage(&red); // releasing unused images + cvReleaseImage(&green); + cvReleaseImage(&blue); + + imgthresh[0] = threshImg(ronly,rthresh); // thresholding each channel depending upon the value set by the slider + imgthresh[1] = threshImg(gonly,gthresh); + imgthresh[2] = threshImg(bonly,bthresh); + + + switch(ch) // this helps to display correct images on the windows depending upon the channel select slider value + { + case 0: + cvShowImage("Threshold",imgthresh[0]); + cvShowImage("Channel",ronly); + break; + case 1: + cvShowImage("Threshold",imgthresh[1]); + cvShowImage("Channel",gonly); + break; + case 2: + cvShowImage("Threshold",imgthresh[2]); + cvShowImage("Channel",bonly); + break; + } + //imgthresh=red; + for(n=0;n<=2;n++) // loop to find contours in red green blue channels + { + + cvFindContours(imgthresh[n], storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); + + while(contours) + { + // approximating contours boundaries to get rid of torned edges + result = cvApproxPoly(contours, sizeof(CvContour), storage, CV_POLY_APPROX_DP,cvContourPerimeter(contours)*0.03, 0); + x=result->total ; // calculating the total no of edges in a approximated contour(blob) + // if x==4 then its a square and fabs()-> gives abolute value and with cvContourArea we are setting the min and max blob size to be detected in image + if(x==4 && fabs(cvContourArea(result,CV_WHOLE_SEQ))>min && fabs(cvContourArea(result,CV_WHOLE_SEQ))total); + // loop to store the coordinates of the edges of the detected contour and storing then in CvPoint structure + for( i=0 ; ix,pt->y); + at[i]=cvPoint(pt->x,pt->y); + + } + + // loop to draw boundary around the contour + for(i=0;i6 && fabs(cvContourArea(result,CV_WHOLE_SEQ))>min && fabs(cvContourArea(result,CV_WHOLE_SEQ))total); + for( i=0 ; ix,pt->y); + at[i]=cvPoint(pt->x,pt->y); + } + for(i=0;ih_next; + } + } + //movent_decide(img); + cvShowImage("Original",img); + c = cvWaitKey(20); // waiting for 20 milli seconds + if(c==27) // detecting esc key press + break; + // doing some cleaning up + cvReleaseImage(&ronly); + cvReleaseImage(&gonly); + cvReleaseImage(&bonly); + cvReleaseImage(&imgthresh[0]); + cvReleaseImage(&imgthresh[1]); + cvReleaseImage(&imgthresh[2]); + + + //cvClearSeq(result); + //cvClearSeq(contours); + //cvReleaseMemStorage(&contours->storage); + + } + + + //cvReleaseImage(&img); + cvReleaseMemStorage(&storage); + //cvClearSeq(contours); + CloseHandle(hPort); + + cvReleaseCapture(&capture); + //cvClearSeq(contours); + return 0; +} diff --git a/Techfest_2013/Techfest_2013/Techfest_2013.vcxproj b/Techfest_2013/Techfest_2013/Techfest_2013.vcxproj new file mode 100644 index 0000000..d826a57 --- /dev/null +++ b/Techfest_2013/Techfest_2013/Techfest_2013.vcxproj @@ -0,0 +1,95 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {6880EF99-5C77-44C4-957B-D0EA8098DE47} + Win32Proj + Techfest_2013 + + + + Application + true + Unicode + + + Application + false + true + Unicode + + + + + + + + + + + + + true + C:\OpenCV2.1\include\opencv;$(IncludePath) + C:\OpenCV2.1\lib;$(LibraryPath) + C:\OpenCV2.1\src\cv;C:\OpenCV2.1\src\cvaux;C:\OpenCV2.1\src\cxcore;C:\OpenCV2.1\src\highgui;$(SourcePath) + + + false + + + + Use + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + cv210d.lib;cxcore210d.lib;highgui210d.lib;%(AdditionalDependencies) + + + + + Level3 + Use + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + true + + + + + + + + + + + + Create + Create + + + + + + + \ No newline at end of file diff --git a/Techfest_2013/Techfest_2013/Techfest_2013.vcxproj.filters b/Techfest_2013/Techfest_2013/Techfest_2013.vcxproj.filters new file mode 100644 index 0000000..8efb6a2 --- /dev/null +++ b/Techfest_2013/Techfest_2013/Techfest_2013.vcxproj.filters @@ -0,0 +1,36 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + + + + Header Files + + + Header Files + + + + + Source Files + + + Source Files + + + \ No newline at end of file diff --git a/Techfest_2013/Techfest_2013/stdafx.cpp b/Techfest_2013/Techfest_2013/stdafx.cpp new file mode 100644 index 0000000..a6bffd6 --- /dev/null +++ b/Techfest_2013/Techfest_2013/stdafx.cpp @@ -0,0 +1,8 @@ +// stdafx.cpp : source file that includes just the standard includes +// Techfest_2013.pch will be the pre-compiled header +// stdafx.obj will contain the pre-compiled type information + +#include "stdafx.h" + +// TODO: reference any additional headers you need in STDAFX.H +// and not in this file diff --git a/Techfest_2013/Techfest_2013/stdafx.h b/Techfest_2013/Techfest_2013/stdafx.h new file mode 100644 index 0000000..b005a83 --- /dev/null +++ b/Techfest_2013/Techfest_2013/stdafx.h @@ -0,0 +1,15 @@ +// stdafx.h : include file for standard system include files, +// or project specific include files that are used frequently, but +// are changed infrequently +// + +#pragma once + +#include "targetver.h" + +#include +#include + + + +// TODO: reference additional headers your program requires here diff --git a/Techfest_2013/Techfest_2013/targetver.h b/Techfest_2013/Techfest_2013/targetver.h new file mode 100644 index 0000000..87c0086 --- /dev/null +++ b/Techfest_2013/Techfest_2013/targetver.h @@ -0,0 +1,8 @@ +#pragma once + +// Including SDKDDKVer.h defines the highest available Windows platform. + +// If you wish to build your application for a previous Windows platform, include WinSDKVer.h and +// set the _WIN32_WINNT macro to the platform you wish to support before including SDKDDKVer.h. + +#include