Упражнение 11. Компютърно зрение

From Ilianko

Инсталация 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;
}

#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;
}

d

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <linux/input.h>
#include <linux/uinput.h>

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


#define die(str, args...) do { \
        perror(str); \
        exit(EXIT_FAILURE); \
    } while(0)

int main(int argc, char* argv[] )
{
    int                    fd,lastX = 0, lastY=0;
    struct uinput_user_dev uidev;
    struct input_event     ev;
    int                    dx, dy;
    int                    i;

    fd = open("/dev/uinput", O_WRONLY | O_NONBLOCK);
    if(fd < 0)
        die("error: open");

    if(ioctl(fd, UI_SET_EVBIT, EV_KEY) < 0)
        die("error: ioctl");
    if(ioctl(fd, UI_SET_KEYBIT, BTN_LEFT) < 0)
        die("error: ioctl");

    if(ioctl(fd, UI_SET_EVBIT, EV_REL) < 0)
        die("error: ioctl");
    if(ioctl(fd, UI_SET_RELBIT, REL_X) < 0)
        die("error: ioctl");
    if(ioctl(fd, UI_SET_RELBIT, REL_Y) < 0)
        die("error: ioctl");

    memset(&uidev, 0, sizeof(uidev));
  snprintf(uidev.name, UINPUT_MAX_NAME_SIZE, "uinput-sample");
    uidev.id.bustype = BUS_USB;
    uidev.id.vendor  = 0x1;
    uidev.id.product = 0x1;
    uidev.id.version = 1;

    if(write(fd, &uidev, sizeof(uidev)) < 0)
        die("error: write");

    if(ioctl(fd, UI_DEV_CREATE) < 0)
        die("error: ioctl");

    sleep(2);

    
  
            dx = -100;
            dy = -10;
          
       
            memset(&ev, 0, sizeof(struct input_event));
            ev.type = EV_REL;
            ev.code = REL_X;
            ev.value = dx;
            if(write(fd, &ev, sizeof(struct input_event)) < 0)
                die("error: write");

            memset(&ev, 0, sizeof(struct input_event));
            ev.type = EV_REL;
            ev.code = REL_Y;
            ev.value = dy;
            if(write(fd, &ev, sizeof(struct input_event)) < 0)
                die("error: write");

            memset(&ev, 0, sizeof(struct input_event));
            ev.type = EV_SYN;
            ev.code = 0;
            ev.value = 0;
            if(write(fd, &ev, sizeof(struct input_event)) < 0)
                die("error: write");

            usleep(15000);
      

    if(ioctl(fd, UI_DEV_DESTROY) < 0)
        die("error: ioctl");

    close(fd);

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;
 
 
        
            dx = lastX - posX;
            dy = lastY - posY;
          lastY = posY;
          lastX = posX;
          
memset(&ev, 0, sizeof(struct input_event));
            ev.type = EV_REL;
            ev.code = REL_X;
            ev.value = dx;
            if(write(fd, &ev, sizeof(struct input_event)) < 0)
                die("error: write");

            memset(&ev, 0, sizeof(struct input_event));
            ev.type = EV_REL;
            ev.code = REL_Y;
            ev.value = dy;
            if(write(fd, &ev, sizeof(struct input_event)) < 0)
                die("error: write");

            memset(&ev, 0, sizeof(struct input_event));
            ev.type = EV_SYN;
            ev.code = 0;
            ev.value = 0;
            if(write(fd, &ev, sizeof(struct input_event)) < 0)
                die("error: write"); 
 
        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/