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*

Install Opencv 2.4.3 in Ubantu 10.10 or higher [10 steps]


1. Down Netbeans 6.9 using Ubuntu software center.
2. install netbeans-7.2.1-ml-cpp-linux.sh


3.//We are going to setup OpenCV to use the new Qt highgui interface, which is much better than the simple highgui interface. Also, we will install OpenCV with support for OpenGL, as well as reading and writing videos, access to a webcam, Python, C and C++ interfaces, and Intel Threading Building Blocks (TBB).

OK, so the first step is to make sure that everything in the system is updated and upgraded:
3.1 sudo apt-get update
sudo apt-get upgrade

//Now, you need to install many dependencies, such as support for reading and writing image files, drawing on the screen, some needed tools, etc… This step is very easy, you only need to write the following command in the Terminal:

3.2 sudo apt-get install build-essential libgtk2.0-dev libjpeg62-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev

sudo apt-get install sphinx-common


//Time to get the OpenCV 2.4.3 source code:
3.3 cd ~
wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.3/OpenCV-2.4.3.tar.bz2
tar -xvf OpenCV-2.4.3.tar.bz2
cd OpenCV-2.4.3



//Now we have to generate the Makefile by using cmake. In here we can define which parts of OpenCV we want to compile. Since we want to use Python, TBB, OpenGL, Qt, work with videos, etc, here is where we need to set that. Just execute the following line at the terminal to create the appropriate Makefile. Note that there are two dots at the end of the line, it is an argument for the cmake program and it means the parent directory (because we are inside the build directory, and we want to refer to the OpenCV directory, which is its parent).

3.4 mkdir build
cd build
cmake -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..


////Check that the above command produces no error and that in particular it reports FFMPEG as YES. If this is not the case you will not be able to read or write videos. Also, check that Python, TBB, OpenGL, V4L, OpenGL and Qt are detected.

//Now, you are ready to compile and install OpenCV 2.x.x:
3.4 make
sudo make install

//Now you have to configure OpenCV. First, open the opencv.conf file with the following code:
3.5 sudo gedit /etc/ld.so.conf.d/opencv.conf

//Add the following line at the end of the file(it may be an empty file, that is ok) and then save it:
3.6 /usr/local/lib

//Run the following code to configure the library:
3.7 sudo ldconfig

//Now you have to open another file:
3.8 sudo gedit /etc/bash.bashrc

//Add these two lines at the end of the file and save it:
3.9 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
    export PKG_CONFIG_PATH


//Finally, close the console and open a new one, restart the computer or logout and then login again. OpenCV will not work correctly until you do this.
Now you have OpenCV 2.4.3 installed in your computer with Python, TBB, OpenGL, video, and Qt support.

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

Test....................................................

Now let’s build some samples included in OpenCV:
1 cd ~/OpenCV-2.4.3/samples/c
2 chmod +x build_all.sh
3 ./build_all.sh

Now we are ready to run the examples:
1 ./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg

detect face
2     ./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --nested-cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_eye.xml" --scale=1.5 lena.jpg



set background black
3. ~/OpenCV-2.4.3/build/bin/grabcut ~/OpenCV-2.4.3/samples/cpp/lena.jpg


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





Wednesday, December 12, 2012

[Solved] Rpm ---> Deb in linux Ubantu

Run this command to install alien and other necessary packages:
sudo apt-get install alien dpkg-dev debhelper build-essential
To convert a package from rpm to debian format, use this command syntax. The sudo may not be necessary, but we’ll include it just in case.
sudo alien packagename.rpm
To install the package, you’ll use the dpkg utility, which is the internal package management tool behind debian and Ubuntu.
sudo dpkg -i packagename.deb
The package should now be installed, providing it’s compatible with your system.