Thursday, December 13, 2012

Small Open CV project


Netbeans...........

Create project C++ application

******************************************************1**** In main add ...**********************************************************************

/*
 * File:   main.cpp
 * Author: lochana
 *
 * Created on 13 December, 2012, 11:16 AM
 */

#include <stdio.h>
#include <string.h>
#include <string.h>
#include<iostream>
#include <pthread.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include "ImageProcess.hpp"

using namespace std;

ImageProcess* p_ImageProcess;


void * StartGuiRef(void * p)
{
/////////////////SERIAL PORT INIT///////////////////////////////////////////////

///////////////////SERIAL PORT INIT/////////////////////////////////////////////  
p_ImageProcess=new ImageProcess;  
cout<<"...........Object Created.........\n";  
IplImage *frame;
IplImage *CombinedImage;
int brightness=196;
//CvCapture* capture = cvCreateCameraCapture(0) ;
CvCapture* capture = cvCaptureFromCAM(0) ;
cvNamedWindow("Ilakke");
cvCreateTrackbar("Brightness", "Ilakke", &brightness, 255, NULL);
cout<<"...........Cammara Initialized.........\n";
p_ImageProcess->SerialPortInit();

while(1){
   

p_ImageProcess->InitializeGraph();
while(1){
p_ImageProcess->SerialPortStatus();
frame = cvQueryFrame( capture );  
p_ImageProcess->SetMainCaptureFrame(frame);
p_ImageProcess->Do_AllProcess();
p_ImageProcess->SetBrigtness(brightness);
    //p_ImageProcess->DrawTheGraph();
    cvWaitKey(2);
    cvShowImage("Ilakke",p_ImageProcess->GetShowImage());
    if(p_ImageProcess->GetGraphPosition()==1200){ break;}
   
       }
//p_ImageProcess->RefreshGraph();

cvWaitKey(5);
cout<<"............thread gui.......\n";

}

   
}

   
int main(int argc, char**argv) {


pthread_t pt_GuiTread;
int i_ptGui = pthread_create(&pt_GuiTread,NULL,StartGuiRef,NULL);

while(1){//cout<<"............thread main.......\n";
   
}

return 0;

}

**************************************************************2*****ImageProcess.cpp*****************************************************

/*
 * File:   ImageProcess.cpp
 * Author: lochana
 *
 * Created on 13 December, 2012, 11:19 AM
 */

#include "ImageProcess.hpp"

ImageProcess::ImageProcess() {
    NumOfShot=0;
    NumOfRealShot=0;
}

ImageProcess::ImageProcess(const ImageProcess& orig) {
}

ImageProcess::~ImageProcess() {
}
void ImageProcess::SetMainCaptureFrame(IplImage* frame){
    CaptureFrame=frame;
}
void ImageProcess::Do_GrayScale(){
GrayScaleFrame=cvCreateImage(cvGetSize(CaptureFrame),IPL_DEPTH_8U,1);
cvCvtColor(CaptureFrame,GrayScaleFrame,CV_RGB2GRAY);
}
IplImage* ImageProcess::GetShowImage(){
    return ImageToShow;
}
void ImageProcess::Do_AllProcess(){
    Do_GrayScale();
    Do_FindFirePoint();
    Do_DrawAllShot();
    ImageCenter.x=(int)(0.5*CaptureFrame->width);
    ImageCenter.y=(int)(0.5*CaptureFrame->height);
    Do_DrawCircles();
    cvShowManyImages(CaptureFrame,CaptureFrame,ImageGraph);
}
void ImageProcess::Do_FindFirePoint(){

double minVal, maxVal;
CvPoint minLoc, maxLoc;
cvMinMaxLoc(GrayScaleFrame, &minVal, &maxVal, &minLoc, &maxLoc, 0);
if(maxVal>i_Brigtness){
FirePoint=maxLoc;

if(bRealFire){
RealFirePoint[NumOfRealShot]=FirePoint;
NumOfRealShot++;
            }else{
AllFirePoint[NumOfShot]=FirePoint;
NumOfShot++;
                }
if(NumOfShot==998){std::cout<<".............988...shot....\n";}
             }
}
void ImageProcess::Do_DrawAllShot(){

for( int k= 0; k <NumOfShot; k++ ){
 
 cvCircle(CaptureFrame,AllFirePoint[k],5,CV_RGB(255,255,0),-1);
}
for( int k= 0; k <NumOfRealShot; k++ ){
 
 cvCircle(CaptureFrame,RealFirePoint[k],5,CV_RGB(255,0,0),-1);
}

}
void ImageProcess::Do_DrawCircles(){
   
 cvCircle(CaptureFrame,ImageCenter,10,CV_RGB(0,255,0),-1);
 cvCircle(CaptureFrame,ImageCenter,30,CV_RGB(0,255,0),0);
 cvCircle(CaptureFrame,ImageCenter,70,CV_RGB(0,255,0),0);
 cvCircle(CaptureFrame,ImageCenter,100,CV_RGB(0,255,0),0);
 cvCircle(CaptureFrame,ImageCenter,130,CV_RGB(0,255,0),0);
 cvCircle(CaptureFrame,ImageCenter,160,CV_RGB(0,255,0),0);
 cvCircle(CaptureFrame,ImageCenter,190,CV_RGB(0,255,0),0);
 cvCircle(CaptureFrame,ImageCenter,220,CV_RGB(0,255,0),0);
 cvCircle(CaptureFrame,ImageCenter,250,CV_RGB(0,255,0),0);

}
void ImageProcess::SetBrigtness(int btr){
    i_Brigtness=btr;
}
void ImageProcess::InitializeGraph(){
x=10;i_gp=0;j_gp=10;
yx=yy=yz=valx=valy=valz=nvalx=nvaly=nvalz=i_GraphPosition=0;
ImageGraph=cvCreateImage(cvSize(1200,500), IPL_DEPTH_8U, 3);//a very long window
cvLine(ImageGraph,cvPoint(10, 250),cvPoint(2000, 250),cvScalar(0, 255, 0, 0), 1, 8, 0);//y-ax
cvLine(ImageGraph,cvPoint(10, 0),cvPoint(10, 500),cvScalar(0, 255, 0, 0), 1, 8, 0);//x-ax
valx=valy=valz=250;

}
void ImageProcess::RefreshGraph(){

cvReleaseImage(&ImageGraph);
}
void ImageProcess::DrawTheGraph(){

         nvalx=100*(sin(0.05*i_gp));//-CentX);//your input
         nvaly=100*(sin(0.05*i_gp+10));//-CentY);//10*data.d_GyroDatax;;
         nvalz=100*sin(0.05*i_gp+20);
       
       
         nvalx=(FirePoint.x-ImageCenter.x);//your input
         nvaly=(FirePoint.y-ImageCenter.y);//10*data.d_GyroDatax;;
       
         i_gp=i_gp++;

         yx=250-nvalx;
         yy=250-nvaly;
         yz=250-nvalz;
         cvLine(ImageGraph,cvPoint(j_gp,valx),cvPoint(x,yx),cvScalar(0,0, 255, 0), 1, 8, 0);
         cvLine(ImageGraph,cvPoint(j_gp,valy),cvPoint(x,yy),cvScalar(0,255,255, 0), 1, 8, 0);
         //cvLine(img,cvPoint(j_gp,valz),cvPoint(x,yz),cvScalar(0,255, 0, 0), 1, 8, 0);

         j_gp=j_gp++;
         x=j_gp;

         valx=yx;
         valy=yy;
         valz=yz;
        //cvShowImage("GRAPH", img);
         //cvWaitKey(2);
         i_GraphPosition=j_gp;
         
}
int ImageProcess::GetGraphPosition(){
    return i_GraphPosition;
}

void ImageProcess::cvShowManyImages(IplImage *MainImage,IplImage *ImSub1,IplImage *ImSub2) {
 
    IplImage *DispImage;
    // Create a new 3 channel image
    DispImage = cvCreateImage( cvSize(1000,600), 8, 3 );

        // Set the image ROI to display the current image
        cvSetImageROI(DispImage, cvRect(200, 20,MainImage->width,MainImage->height));

        // Resize the input image and copy the it to the Single Big Image
        cvResize(MainImage, DispImage);

        cvResetImageROI(DispImage);
       
        // Set the image ROI to display the current image
        cvSetImageROI(DispImage, cvRect(20,20,30,MainImage->height));

        // Resize the input image and copy the it to the Single Big Image
        cvResize(ImSub1, DispImage);      
       
        // Reset the ROI in order to display the next image
        cvResetImageROI(DispImage);
       
          // Set the image ROI to display the current image
        cvSetImageROI(DispImage, cvRect(200,20+MainImage->height,0.1*(ImSub2->width),0.1*(ImSub2->height)));
       

        // Resize the input image and copy the it to the Single Big Image
        cvResize(ImSub2, DispImage);      
       
        // Reset the ROI in order to display the next image
        cvResetImageROI(DispImage);
 
       
    ImageToShow= DispImage;
    ImageToShow=CaptureFrame;
   
   
}
void ImageProcess::SerialPortInit(){
    fd=0;
    bRealFire=false;
fd = open("/dev/ttyUSB3", O_RDWR | O_NOCTTY);

struct termios options;

    /*
     * Get the current options for the port...
     */

    tcgetattr(fd, &options);

 

    cfsetispeed(&options, B9600);
    cfsetospeed(&options, B9600);

    /*
     * Enable the receiver and set local mode...
     */

    options.c_cflag |= (CLOCAL | CREAD);

    /*
     * Set the new options for the port...
     */

    tcsetattr(fd, TCSANOW, &options);



}
void ImageProcess::SerialPortStatus(){
   
    cout<<".........."<<fd<<"....\n";
   //if(fd!=8){
    int rd=
    read(fd,psResponse,1);
    //if(rd<0){return;}
    printf("Bytes sent are %d \n",rd);
    cout<<"....."<<psResponse<<"\n";
     if(*psResponse=='A'){cout<<"..................$$$\n";
     bRealFire=false;
     }
     else{
     cout<<"..........................................@@@\n";
     bRealFire=true;
     }
    // }
     //* */
    //return b_fired;
}

**************************************************************************8***3*****ImageProcess.hpp******************************************************
/*
 * File:   ImageProcess.h
 * Author: lochana
 *
 * Created on 13 December, 2012, 11:19 AM
 */

#ifndef IMAGEPROCESS_HPP
#define IMAGEPROCESS_HPP
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <math.h>
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <iostream>
using namespace std;

class ImageProcess {
public:
    ImageProcess();
    ImageProcess(const ImageProcess& orig);
    virtual ~ImageProcess();
    void SetMainCaptureFrame(IplImage* frame);
    IplImage* CaptureFrame;
    IplImage* GrayScaleFrame;
    IplImage* GetShowImage();
    void Do_AllProcess();
    void SetBrigtness(int btr);
    void InitializeGraph();
    void RefreshGraph();
    IplImage* ImageGraph;
    void DrawTheGraph();
    int GetGraphPosition();
    void cvShowManyImages(IplImage *MainImage,IplImage *ImSub1,IplImage *ImSub2);
    IplImage* ImageToShow;
    void SerialPortInit();
    void SerialPortStatus();
private:
    void Do_GrayScale();
    void Do_FindFirePoint();
    void Do_DrawAllShot();  
    CvPoint FirePoint;
    CvPoint AllFirePoint[1000];
    CvPoint RealFirePoint[1000];
    int NumOfShot;
    int NumOfRealShot;
    CvPoint ImageCenter;
    void Do_DrawCircles();
    int i_Brigtness;
    int yx,yy,yz,valx,valy,valz,x,nvalx,nvaly,nvalz,i_gp,j_gp,i_GraphPosition;
    int fd,ir,ig,ib;
    char psResponse[1];
    bool bRealFire;
};

#endif /* IMAGEPROCESS_HPP */


********************************************************************

project properties

Linker-->Additional library directories
/user/local/include/opencv
/user/local/include/opencv2

Linker-->Library-->Add library
/user/local/lib *select all*
Linker-->Library--> standerd library--> *Posix threads*

No comments:

Post a Comment