Posted by : M yunus Rabu, 26 Agustus 2015





Peralatan yang dibutuhkan:

- Arduino Mega 2560
- Pan/Tilt Servo
- Cammera Webcam
- Laptop Ubuntu
- OpenCV yang sudah terinstal


Rangkaian:








Software:
Code untuk OpenCV:
#include <opencv/cvaux.h>
#include <opencv/highgui.h>
#include <opencv/cxcore.h>
#include <stdio.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <unistd.h>

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

// Default capture size - 640x480
CvSize size = cvSize(640,480);
//CvSize size = cvSize(320,240);
// Open capture device. 0 is /dev/video0, 1 is /dev/video1, etc.
CvCapture* capture = cvCaptureFromCAM(1);
if( !capture )
{
fprintf( stderr, "ERROR: capture is NULL \n" );
getchar();
return -1;
}

// Create a window in which the captured images will be presented
cvNamedWindow( "Camera", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "HSV", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "EdgeDetection", CV_WINDOW_AUTOSIZE );

// Detect a red ball
CvScalar hsv_min = cvScalar(150, 84, 130, 0);
CvScalar hsv_max = cvScalar(358, 256, 255, 0);

IplImage * hsv_frame = cvCreateImage(size, IPL_DEPTH_8U, 3);
IplImage* thresholded = cvCreateImage(size, IPL_DEPTH_8U, 1);

while( 1 )
{
// Get one frame
IplImage* frame = cvQueryFrame( capture );
if( !frame )
{
fprintf( stderr, "ERROR: frame is null...\n" );
getchar();
break;
}

// Covert color space to HSV as it is much easier to filter colors in the HSV color-space.
cvCvtColor(frame, hsv_frame, CV_BGR2HSV);
// Filter out colors which are out of range.
cvInRangeS(hsv_frame, hsv_min, hsv_max, thresholded);

// Memory for hough circles
CvMemStorage* storage = cvCreateMemStorage(0);
// hough detector works better with some smoothing of the image
cvSmooth( thresholded, thresholded, CV_GAUSSIAN, 9, 9 );
CvSeq* circles = cvHoughCircles(thresholded, storage, CV_HOUGH_GRADIENT, 2,
thresholded->height/4, 100, 50, 10, 400);

for (int i = 0; i < circles->total; i++)
{
float* p = (float*)cvGetSeqElem( circles, i );
//int* p=(int*)b;

        FILE *file;
        file = fopen("/dev/ttyACM0","w");

        //printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] );

        int a=1,b=2,c=3,d=4;


    if((p[0]<320.000000)&&(p[1]<240.000000)){
          fprintf(file,"%d",a);
      printf("a\n");
        }
        else if((p[0]>320.000000)&&(p[1]<240.000000)){
          fprintf(file,"%d",b);
         printf("b\n");
    }
        else if((p[0]>320.000000)&&(p[1]>240.000000)){
          fprintf(file,"%d",c);
         printf("c\n");
    }        
        else if((p[0]< 320.000000)&&(p[1]>240.000000)){
          fprintf(file,"%d",d);
     printf("d\n");
        }

    fclose(file);
    //open serial       
         // fclose(file);
//        sleep(0.5);



cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])),
3, CV_RGB(0,255,0), -1, 8, 0 );
cvCircle( frame, cvPoint(cvRound(p[0]),cvRound(p[1])),
cvRound(p[2]), CV_RGB(255,0,0), 3, 8, 0 );

sleep(0.5);

}

cvShowImage( "Camera", frame ); // Original stream with detected ball overlay
cvShowImage( "HSV", hsv_frame); // Original stream in the HSV color space
cvShowImage( "After Color Filtering", thresholded ); // The stream after color filtering

cvReleaseMemStorage(&storage);

// Do not release the frame!

//If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux version),
//remove higher bits using AND operator
if( (cvWaitKey(10) & 255) == 27 ) break;
}

// Release the capture device housekeeping
cvReleaseCapture( &capture );
cvDestroyWindow( "mywindow" );
return 0;
}



code untuk arduino:
#include <Servo.h>
Servo tilt;
Servo pan;
int i=90,j=90;
char incomingByte = 0;   // for incoming serial data

void setup(){
    tilt.attach(9);
    pan.attach(10);
    Serial.begin(9600);
     tilt.write(90);
     pan.write(90);   
}

void satu(){
          pan.write(i);
          tilt.write(j);
          i=i+1;
          j=j-1;
}
void dua(){
          pan.write(i);
          tilt.write(j);
          i=i-1;
          j=j-1;
}
void tiga(){
          pan.write(i);
          tilt.write(j);
          i=i-1;
          j=j+1;
}
void empat(){
          pan.write(i);
          tilt.write(j);
          i=i+1;
          j=j+1;
}

void loop()
{

  // send data only when you receive data:
  if (Serial.available() > 0)
  {
   
    incomingByte = Serial.read();

      switch (incomingByte)
      {
        case '1':
          satu(); 
          delay(10);
          break;
        case '2':
          dua();
          delay(10);
          break;
        case '3':
          tiga();
          delay(10);
          break;
        case '4':
          empat();
          delay(10);
          break;
             }
    }
   
  }

Hasil::


 

{ 3 komentar... read them below or Comment }

  1. Wah mantab kak, izin share kak hehe
    Belajar pengolahan citra digital darimana kak?

    BalasHapus
  2. mas mau nanyak sebelumnya,cara compile nya gmna ya mas ? uda bisa tapi masi error mas.

    BalasHapus
  3. How do I sign up for an account at a casino - DrmCAD
    For players 김포 출장샵 to 제주도 출장안마 sign up and play slots, 익산 출장안마 you can use their link in their 부산광역 출장샵 casino login page. Once the bonus is triggered, 포천 출장안마

    BalasHapus

- Copyright © Legendre-electronics - Date A Live - Powered by Blogger - Designed by Johanes Djogan -