Difference between revisions of "Упражнение 11. Компютърно зрение"

From Ilianko
Line 117: Line 117:
  
 
</pre></code>
 
</pre></code>
 +
 +
 +
#include <stdio.h>
 +
#include <math.h>
 +
#include <opencv/highgui.h> //OpenCV GUI functions ̄include <stdio.h>
 +
#include <opencv/cv.h> //main OpenCV functions
 +
 +
 +
int main(int argc, char* argv[] )
 +
{
 +
char use[]="<image> <red threshold> <blue and green threshold> <blue green max diff>\n\n";
 +
char *image_file;
 +
int threshold;
 +
int BG_threshold;
 +
int BG_diff;
 +
IplImage* img;
 +
IplImage* img_result_threshold, *img_morph, *img_temp;
 +
int x,y,red,green,blue;
 +
 +
CvMemStorage* storage = cvCreateMemStorage(0);
 +
        CvSeq* contour = 0;
 +
        CvSeq* contours = 0;
 +
 +
//Get inputs parameters
 +
if( argc !=5 )
 +
        {
 +
                fprintf(stderr, "\nUse: %s %s", argv[0], use);
 +
                exit(-1);
 +
        }
 +
//Get parameters
 +
image_file=argv[1];
 +
sscanf(argv[2],"%d",&threshold);
 +
sscanf(argv[3],"%d",&BG_threshold);
 +
sscanf(argv[4],"%d",&BG_diff);
 +
//Load image
 +
CvCapture* c1 = cvCaptureFromCAM(0);
 +
    cvGrabFrame( c1 );
 +
    img=cvRetrieveFrame(c1,0);
 +
    cvWaitKey(200);
 +
    cvGrabFrame( c1 );
 +
    img=cvRetrieveFrame(c1,0);
 +
IplImage* frame = 0;
 +
IplImage* imgThreshed =cvCreateImage(cvGetSize(img), 8, 1);
 +
IplImage* imgScribble = cvCreateImage(cvGetSize(img), 8, 3);
 +
 +
 
 +
 +
//img= cvLoadImage(image_file,1);
 +
while(cvWaitKey(50) < 0)
 +
{
 +
    cvGrabFrame( c1 );
 +
    img=cvRetrieveFrame(c1,0);
 +
   
 +
//Create image result for threshold
 +
img_result_threshold= cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
 +
 +
img_morph= cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
 +
 +
img_temp=  cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);
 +
 +
 +
for(x=0; x < img->width; x++)
 +
{
 +
for(y=0;y < img->height; y++)
 +
{
 +
red=  ((uchar*) (img->imageData + img->widthStep*y))[x*3+2];
 +
green= ((uchar*) (img->imageData + img->widthStep*y))[x*3+1];
 +
blue=  ((uchar*) (img->imageData + img->widthStep*y))[x*3];
 +
 +
uchar* temp_ptr = &((uchar*)(img_result_threshold->imageData +
 +
                  img_result_threshold->widthStep*y))[x];
 +
 +
temp_ptr[0] = ( (red>threshold)&&
 +
                (green < BG_threshold) &&
 +
    (blue < BG_threshold) &&
 +
    (abs(green-blue)< BG_diff)) ? 255:0; //White to greater of threshold
 +
}
 +
}
 +
 +
 +
cvMorphologyEx(img_result_threshold, img_morph, img_temp, NULL, CV_MOP_CLOSE, 6);
 +
 +
//cvNamedWindow( "Threshold", 1 );
 +
    //    cvShowImage( "Threshold", img_morph );
 +
 +
cvFindContours( img_morph, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
 +
 +
 +
 +
int area = 0, temp = 0, k=0, s = 0;
 +
contours = contour;
 +
for( ; contour != 0; contour = contour->h_next )
 +
        {
 +
           
 +
           
 +
           
 +
            area = cvContourArea( contour , CV_WHOLE_SEQ ,0);
 +
            if (area > temp)
 +
            {
 +
s = k;
 +
temp = area;
 +
// printf("%i \n", s);
 +
}
 +
k++;
 +
        }
 +
    //  printf("%i - %i \n", temp, s);
 +
k = 0;
 +
//cvFindContours( img_morph, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
 +
   
 +
      imgThreshed =cvCreateImage(cvGetSize(img), 8, 1);
 +
     
 +
  for( ; contours != 0; contours = contours->h_next )
 +
      {
 +
            if( k == s)
 +
            {
 +
            CvScalar color = CV_RGB(55, 255, 255 );
 +
            cvDrawContours( imgThreshed, contours, color, color, -1, -1, 8, cvPoint(0,0) );
 +
  }
 +
    k++; 
 +
    } 
 +
    cvNamedWindow("thresh"); 
 +
    cvShowImage("thresh", imgThreshed);
 +
 
 +
     
 +
 
 +
        CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
 +
        cvMoments(imgThreshed, moments, 1);
 +
 +
        // The actual moment values
 +
        double moment10 = cvGetSpatialMoment(moments, 1, 0);
 +
        double moment01 = cvGetSpatialMoment(moments, 0, 1);
 +
        double area1 = cvGetCentralMoment(moments, 0, 0);
 +
       
 +
       
 +
        // Holding the last and current ball positions
 +
        static int posX = 0;
 +
        static int posY = 0;
 +
 +
        int lastX = posX;
 +
        int lastY = posY;
 +
 +
        posX = moment10/area1;
 +
        posY = moment01/area1;
 +
 +
/*The current position of the ball is stored in posX and posY, and the previous location is stored in lastX and lastY.
 +
 +
We’ll just print out the current position for debugging purposes:*/
 +
 +
        // Print it out for debugging purposes
 +
        printf("position (%d,%d)\n", posX, posY);
 +
       
 +
        if(lastX>0 && lastY>0 && posX>0 && posY>0)
 +
        {
 +
            // Draw a yellow line from the previous point to the current point
 +
            cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,0,255), 5);
 +
        }
 +
       
 +
        cvAdd(img, imgScribble, img);
 +
        cvNamedWindow("s"); 
 +
        cvShowImage("s", img);
 +
       
 +
        cvReleaseImage(&imgThreshed);
 +
        delete moments;
 +
 
 +
 
 +
    }
 +
    return 0;
 +
}
 +
  
 
== връзки ==
 
== връзки ==

Revision as of 04:46, 17 April 2012

Инсталация openCV

1. Инсталирайте cmake

sudo apt-get install cmake

2. Инсталирайте библиотеката libgtk2.0-dev

sudo apt-get install libgtk2.0-dev

3. да се свали openCV 2.2 и разархивирайте в директория Downloads/OpenCV-2.2.0

4. Влезте в директорията

cd Downloads/OpenCV-2.2.0
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ./
make
make install
export LD_LIBRARY_PATH=/home/lab/Downloads/OpenCV-2.2.0/lib:$LD_LIBRARY_PATH
sudo ldconfig


-lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy



#include <opencv/highgui.h> //OpenCV GUI functions ̄include <stdio.h>
#include <opencv/cv.h>     //main OpenCV functions


int main(int argc, char** argv)
{
  CvCapture* c1 = cvCaptureFromCAM(0);
  IplImage* img = 0;
  
  
	   
  while(cvWaitKey(2) < 0)
  {
    cvGrabFrame( c1 );	
    img=cvRetrieveFrame(c1,0);
    cvShowImage( "c1", img);
  }
 
  //destroy the window
  cvReleaseCapture(&c1);
  return 0;
}


sinaptic manager apache2 http://localhost/

sudo chmod 777 /var/www/index.html sudo chmod 777 /var/www/

<img src="test.jpg" /> cvSaveImage("/var/www/test.jpg" ,img, 0);


int main(int argc, char** argv)
{
  CvCapture* c1 = cvCaptureFromCAM(0);
  IplImage* img;
  IplImage* img1;
  IplImage* img2;
  IplImage* img3;
  
  cvGrabFrame( c1 );	
  img=cvRetrieveFrame(c1,0);
  img1=cvRetrieveFrame(c1,0);
  img2=cvRetrieveFrame(c1,0);
  img3=cvRetrieveFrame(c1,0);
 
/* get image properties */
int width  = img->width;
int height = img->height;



  cvAdd(img, img ,img , NULL); 
 
/* create new image for the grayscale version */
IplImage* bw = cvCreateImage( cvSize( width, height ), IPL_DEPTH_8U, 1 );

	   

  while(cvWaitKey(5) < 0)
  {
    
    
    img2 = img1;
    img1 = img;
     
    cvGrabFrame( c1 );	
    img=cvRetrieveFrame(c1,0);
    
   
   cvAddWeighted( img1, 0.33, img2, 0.33, 0, img3);
   cvAddWeighted( img, 1, img, 0.33, 0, img3); 

   cvCvtColor( img3, bw, CV_RGB2GRAY );
    cvCanny( bw, bw, 50, 100, 3 );


    cvShowImage( "c1", bw);
  }
 
  //destroy the window
  cvReleaseCapture(&c1);
  return 0;
}


  1. include <stdio.h>
  2. include <math.h>
  3. include <opencv/highgui.h> //OpenCV GUI functions ̄include <stdio.h>
  4. include <opencv/cv.h> //main OpenCV functions


int main(int argc, char* argv[] ) { char use[]="<image> <red threshold> <blue and green threshold> <blue green max diff>\n\n"; char *image_file; int threshold; int BG_threshold; int BG_diff; IplImage* img; IplImage* img_result_threshold, *img_morph, *img_temp; int x,y,red,green,blue;

CvMemStorage* storage = cvCreateMemStorage(0);

       CvSeq* contour = 0;
       CvSeq* contours = 0;

//Get inputs parameters if( argc !=5 )

       {
               fprintf(stderr, "\nUse: %s %s", argv[0], use);
               exit(-1);
       }

//Get parameters image_file=argv[1]; sscanf(argv[2],"%d",&threshold); sscanf(argv[3],"%d",&BG_threshold); sscanf(argv[4],"%d",&BG_diff); //Load image CvCapture* c1 = cvCaptureFromCAM(0);

   cvGrabFrame( c1 );	
   img=cvRetrieveFrame(c1,0);
   cvWaitKey(200);
   cvGrabFrame( c1 );	
   img=cvRetrieveFrame(c1,0);

IplImage* frame = 0; IplImage* imgThreshed =cvCreateImage(cvGetSize(img), 8, 1); IplImage* imgScribble = cvCreateImage(cvGetSize(img), 8, 3);


//img= cvLoadImage(image_file,1); while(cvWaitKey(50) < 0) {

   cvGrabFrame( c1 );	
   img=cvRetrieveFrame(c1,0);
   

//Create image result for threshold img_result_threshold= cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);

img_morph= cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);

img_temp= cvCreateImage(cvSize(img->width, img->height), IPL_DEPTH_8U, 1);


for(x=0; x < img->width; x++) { for(y=0;y < img->height; y++) { red= ((uchar*) (img->imageData + img->widthStep*y))[x*3+2]; green= ((uchar*) (img->imageData + img->widthStep*y))[x*3+1]; blue= ((uchar*) (img->imageData + img->widthStep*y))[x*3];

uchar* temp_ptr = &((uchar*)(img_result_threshold->imageData + img_result_threshold->widthStep*y))[x];

temp_ptr[0] = ( (red>threshold)&& (green < BG_threshold) && (blue < BG_threshold) && (abs(green-blue)< BG_diff)) ? 255:0; //White to greater of threshold } }


cvMorphologyEx(img_result_threshold, img_morph, img_temp, NULL, CV_MOP_CLOSE, 6);

//cvNamedWindow( "Threshold", 1 );

   //    cvShowImage( "Threshold", img_morph );

cvFindContours( img_morph, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );


int area = 0, temp = 0, k=0, s = 0;

contours = contour;

for( ; contour != 0; contour = contour->h_next )

       {
           
           
           
           area = cvContourArea( contour , CV_WHOLE_SEQ ,0);
           if (area > temp)
           { 

s = k; temp = area; // printf("%i \n", s); } k++;

       }
    //   printf("%i - %i \n", temp, s);
k = 0;
//cvFindContours( img_morph, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );
   
      imgThreshed =cvCreateImage(cvGetSize(img), 8, 1);
      
  for( ; contours != 0; contours = contours->h_next )
      {
           if( k == s)
           {
           CvScalar color = CV_RGB(55, 255, 255 );
           cvDrawContours( imgThreshed, contours, color, color, -1, -1, 8, cvPoint(0,0) );

}

    k++;   
    }  
   cvNamedWindow("thresh");   
   cvShowImage("thresh", imgThreshed);
  
      
  
       CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
       cvMoments(imgThreshed, moments, 1);

       // The actual moment values
       double moment10 = cvGetSpatialMoment(moments, 1, 0);
       double moment01 = cvGetSpatialMoment(moments, 0, 1);
       double area1 = cvGetCentralMoment(moments, 0, 0);
       
       
        // Holding the last and current ball positions
       static int posX = 0;
       static int posY = 0;

       int lastX = posX;
       int lastY = posY;

       posX = moment10/area1;
       posY = moment01/area1;

/*The current position of the ball is stored in posX and posY, and the previous location is stored in lastX and lastY.

We’ll just print out the current position for debugging purposes:*/

       // Print it out for debugging purposes
       printf("position (%d,%d)\n", posX, posY);
       
        if(lastX>0 && lastY>0 && posX>0 && posY>0)
       {
           // Draw a yellow line from the previous point to the current point
           cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,0,255), 5);
       }
       
       cvAdd(img, imgScribble, img);
       cvNamedWindow("s");   
       cvShowImage("s", img);
       
       cvReleaseImage(&imgThreshed);
       delete moments;
 
  
   }
   return 0;

}


връзки

http://robocraft.ru/blog/computervision/435.html

http://nashruddin.com/