Archive for 2015
Read dan Write Image (Install dan Backup) Image Raspberry Pi Menggunakan Terminal Ubuntu
By : M yunusKali ini gua mau share bagaimana caranya install dan Backup image Raspberry Pi menggunakan Terminal Ubuntu. Gua menggunakan Ubuntu 14.04.
Alat dan Bahan:
- SD card 8 GB( maks 32 GB)
- Laptop dengan distro Ubuntu (distro lain bisa digunakan juga).
- Image Raspberry Pi, kalo belum punya bisa download disini.
Install Image Raspberry Pi:
1- Buka terminal Ubuntu (Ctrl+Alt+T)
2- Masukan SDcard ke laptop/komputer anda.
3- ketikan perintah berikut :
df -h
setelah itu akan keluar hasil sebagai berikut:
diatas terlihat jika SDcard terbaca sebagai /dev/sdc .
4- setelah itu pada terminal Ubuntu ketikan perintah berikut
dd bs=4M if=2015-09-24-raspbian-jessie.img of=/dev/sdc
sesuaikan bagian /dev/sdc dengan nama SDcard masing".
5- untuk melihat progres Write Image Raspberry, buka terminal baru dengan (Ctrl+Shift+T)
6- ketikan perintah berikut dan lihat kembali pada terminal write image sebelumnya.
sudo pkill -USR1 -n -x dd
Backup Image Raspberry pi:
1- langkahnya sama seperti diatas, hanya pada point ke 4 diganti dengan perintah.
sudo dd bs=4M if=2015-09-24-raspbian-jessie.img of/dev/sdc
Smangat Mencoba!!!!!!!!!!! :D
Raspberry Pi Guitar Pedals
By : M yunusyuk simak!!!!!!!!!!! :D
HARDWARE:
- Raspberry Pi B+
- Box kecil untuk raspberry
- USB Sound
- 2x 3mm jack to audio jack female ( kalo gak dapet custom sendiri)
- fan mini ( untuk ngipasin raspi biar gak kepanasan
- 2x 7 segment
- 1 led indikator
- spul gitar akustik
- speaker aktif
- gitar akustik
SOFTWARE:
- pastikan di raspberry anda sudah terinstall raspbian wheezy.
kalo belum ngikutin tutorial disini aja gan :D
https://www.raspberrypi.org/documentation/installation/installing-images/
- Setelah itu bua terminal jalankan perintah" dibawah..
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install qjackctl
sudo apt get install rakarrack
sudo apt-get install xdotool
sudo apt-get install jackd
- setelah terinstall, kita akan membuat aplikasi qjackctl dan rakarrack berjalan langsung saat pertama kali dihidupkan( jadi gakperlu repot" pake monitor ckck)
- buat script dengan perintah:
sudo nano pedal.sh
-setelah itu masukan script dibawah ini. setelahnya ctrl+x, shift+y, enter
#!/bin/sh
xdotool exec jackd -P80 -p16 -t2000 -v -dalsa -dhw:0 -p1024 -n2 -r44100 -S -s
- setelah itu buat script lain dengan perintah:
sudo nano kill.sh
-setelah itu masukan script dibawah, setelahnya ctrl+x, shift+y, enter.
#!/bin/sh
xdotool sleep 15 mousemove 569 749 sleep 1 click 3 mousemove 565 730 sleep 1 click 1
- buat script la dengan nama:
sudo nano pedalall.sh
- setelah itu masukan script dibawah, setelahnya ctrl+x, shift+y, enter.
#!/bin/sh
lxterminal -e "bash -c \"./pedal.sh; exec bash\""
lxterminal --command=qjackctl
lxterminal --command=rakarrack
lxterminal -e "bash -c \"./python.sh; exec bash\""
xdotool sleep 7
lxterminal -e "bash -c \"./kill.sh; exec bash\""
- buat script lg dengan nama python.sh
sudo nano python.sh
- masukan script dibawah dan save:
#!/bin/sh
xdotool exec sudo python python/7segment2.py
- buat script python untuk mengakses GPIO raspberry untuk 7 segment, led indikator dan pedal push dengan perintah :
mkdir python
cd python
sudo nano 7segment2.py
-masukan program python kedalamnya dan save:
import RPi.GPIO as GPIO
import time
import subprocess
ledPin = 21
buttonPin2= 20
led1 = 26
led2 = 16
led3 = 19
led4 = 13
led5 = 12
led6 = 6
led7 = 5
led8 = 7
led9 = 8
led10 = 11
led11 = 25
led12 = 9
led13 = 10
led14 = 24
led15 = 23
led16 = 22
leds = [led1, led2, led3, led4, led5, led6, led7, led8, led9, led10, led11, led12, led13, led14, led15, led16]
GPIO.setmode(GPIO.BCM)
GPIO.setup(ledPin, GPIO.OUT)
GPIO.setup(buttonPin2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
for led in leds:
GPIO.setup(led, GPIO.OUT)
#==========================...............===============================
#------------------------------------------------------------------------
#-------------------------FUNGSI PANGGILAN-------------------------------
#------------------------------------------------------------------------
def NUM1(index):
GPIO.output(leds[ index + 0 ] , GPIO.HIGH)
GPIO.output(leds[ index + 1 ], GPIO.HIGH)
GPIO.output(leds[ index + 2 ], GPIO.LOW)
GPIO.output(leds[ index + 3 ], GPIO.HIGH)
GPIO.output(leds[ index + 4 ], GPIO.HIGH)
GPIO.output(leds[ index + 5 ], GPIO.HIGH)
GPIO.output(leds[ index + 6 ], GPIO.HIGH)
GPIO.output(leds[ index + 7 ], GPIO.LOW)
def NUM2(index):
GPIO.output(leds[ index + 0 ] , GPIO.LOW)
GPIO.output(leds[ index + 1 ] , GPIO.LOW)
GPIO.output(leds[ index + 2 ] , GPIO.HIGH)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.LOW)
GPIO.output(leds[ index + 5 ] , GPIO.HIGH)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.LOW)
def NUM3(index):
GPIO.output(leds[ index + 0 ] , GPIO.HIGH)
GPIO.output(leds[ index + 1 ] , GPIO.LOW)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.LOW)
GPIO.output(leds[ index + 5 ] , GPIO.HIGH)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.LOW)
def NUM4(index):
GPIO.output(leds[ index + 0 ] , GPIO.HIGH)
GPIO.output(leds[ index + 1 ] , GPIO.HIGH)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.LOW)
GPIO.output(leds[ index + 5 ] , GPIO.LOW)
GPIO.output(leds[ index + 6 ] , GPIO.HIGH)
GPIO.output(leds[ index + 7 ] , GPIO.LOW)
def NUM5(index):
GPIO.output(leds[ index + 0 ] , GPIO.HIGH)
GPIO.output(leds[ index + 1 ] , GPIO.LOW)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.LOW)
GPIO.output(leds[ index + 5 ] , GPIO.LOW)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.HIGH)
def NUM6(index):
GPIO.output(leds[ index + 0 ] , GPIO.LOW)
GPIO.output(leds[ index + 1 ] , GPIO.LOW)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.LOW)
GPIO.output(leds[ index + 5 ] , GPIO.LOW)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.HIGH)
def NUM7(index):
GPIO.output(leds[ index + 0 ] , GPIO.HIGH)
GPIO.output(leds[ index + 1 ] , GPIO.HIGH)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.HIGH)
GPIO.output(leds[ index + 5 ] , GPIO.HIGH)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.LOW)
def NUM8(index):
GPIO.output(leds[ index + 0 ] , GPIO.LOW)
GPIO.output(leds[ index + 1 ] , GPIO.LOW)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.LOW)
GPIO.output(leds[ index + 5 ] , GPIO.LOW)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.LOW)
def NUM9(index):
GPIO.output(leds[ index + 0 ] , GPIO.HIGH)
GPIO.output(leds[ index + 1 ] , GPIO.LOW)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.LOW)
GPIO.output(leds[ index + 5 ] , GPIO.LOW)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.LOW)
def NUM0(index):
GPIO.output(leds[ index + 0 ] , GPIO.LOW)
GPIO.output(leds[ index + 1 ] , GPIO.LOW)
GPIO.output(leds[ index + 2 ] , GPIO.LOW)
GPIO.output(leds[ index + 3 ] , GPIO.HIGH)
GPIO.output(leds[ index + 4 ] , GPIO.HIGH)
GPIO.output(leds[ index + 5 ] , GPIO.LOW)
GPIO.output(leds[ index + 6 ] , GPIO.LOW)
GPIO.output(leds[ index + 7 ] , GPIO.LOW)
def NUMALL(val,index):
if val == 0:
NUM0(index)
if val == 1:
NUM1(index)
if val == 2:
NUM2(index)
if val == 3:
NUM3(index)
if val == 4:
NUM4(index)
if val == 5:
NUM5(index)
if val == 6:
NUM6(index)
if val == 7:
NUM7(index)
if val == 8:
NUM8(index)
if val == 9:
NUM9(index)
#==========================...............===============================
#------------------------------------------------------------------------
#----------------------------FUNGSI UTAMA--------------------------------
#------------------------------------------------------------------------
try:
while True:
subprocess.call(["xdotool","mousemove","34","70","click","1"])
num1 = 0
while num1 < 6:
NUMALL(num1,0)
num2 = 0
while num2 < 10:
if GPIO.input(buttonPin2)==True:
NUMALL(num2,8)
time.sleep(0.1)
num2 += 1
GPIO.output(ledPin, GPIO.HIGH)
subprocess.call(["xdotool","mousemove","34","70","click","1"])
time.sleep(0.1)
subprocess.call(["xdotool","mousemove","291","158","click","1"])
time.sleep(0.1)
subprocess.call(["xdotool","mousemove","34","70","click","1"])
time.sleep(0.1)
num1 += 1
num1 = 5
while num1 > 0:
NUMALL(num1,0)
num2 = 9
while num2 >= 0:
if GPIO.input(buttonPin2)==True:
NUMALL(num2,8)
time.sleep(0.1)
num2 -= 1
GPIO.output(ledPin, GPIO.HIGH)
subprocess.call(["xdotool","mousemove","34","70","click","1"])
time.sleep(0.1)
subprocess.call(["xdotool","mousemove","228","155","click","1"])
time.sleep(0.1)
subprocess.call(["xdotool","mousemove","34","70","click","1"])
time.sleep(0.1)
num1 -= 1
except KeyboardInterrupt:
GPIO.cleanup()
#--------------------------------------------------------------------------------------------------------------------------------
- setelah semua script dibuat, selanjutnya membut script tsb menjadi executable dengan perintah:
sudo chmod +x namaScript.sh
- setelah itu kita set screen raspberry dengan perintah:
sudo nano /boot/config.txt
- hilangkan tanda "#" didepan perintah:
#framebuffer_width=1023 #framebuffer_height=767
sehingga menjadi
framebuffer_width=1023
framebuffer_height=767
akhiri dengan, ctrl+x, shift+y, enter.
- setelah itu setting patchbay qjackctl menjadi seperti dalam video ini:
https://www.youtube.com/watch?v=q9tFSsi60AQ&feature=youtu.be
- setelah itu reboot raspberry pi:
sudo reboot
-setelah semua berjalan dengan baik rangkaikan semua hardware tampak seperti ini:
- jika ada pertanyaan bisa coment atw kalo mau fast respond,, Call ke 083820864843
- Videonya gan:
Face Tracking Haar Cascade Classifier menggunakan Pan/Tilt Camera OpenCV 2.4.9 pada Ubuntu 14.04
By : M yunusHaar Cascade Classifier adalah algoritma yang digunakan untuk mengenali fitur wajah manusia. Haar Cascade Classifier atau Penggolong Riam Haar mampu membedakan wajah dan bukan wajah. Haar Cascade Classifier mengklasifikasi gambar kedalam image positif dan image negatif. Image positif bernilai True, sedangkan image negatif bernilai False. Saat ada wajah yang sedang di jajaki oleh kamera dan di proses melalui OpenCV, maka OpenCV akan melakukan traini untuk mengukur image tersebit kedalam image positif dan negatif. keterangan lengkap dan bagaimana Haar Cascade Classifier bekerja dapat dilihat di docs.opencv.org
==================================================
skema rangkaian elektronik:
===================================================
CODE OPENCV:
===================================================
// Include header files
#include "cv.h"
#include "highgui.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>
// Create memory for calculations
static CvMemStorage* storage = 0;
// Create a new Haar classifier
static CvHaarClassifierCascade* cascade = 0;
// Function prototype for detecting and drawing an object from an image
void detect_and_draw( IplImage* image );
// Create a string that contains the cascade name
const char* cascade_name =
"haarcascade_frontalface_alt.xml";
/* "haarcascade_profileface.xml";*/
// Main function, defines the entry point for the program.
int main( int argc, char** argv )
{
//Serial to Arduino
FILE *file;
file = fopen("/dev/ttyACM0","w");
// Structure for getting video from camera or avi
CvCapture* capture = 0;
/// Images to capture the frame from video or camera or from file
IplImage *frame, *frame_copy = 0;
// Used for calculations
int optlen = strlen("--cascade=");
// Input file name for avi or image file.
const char* input_name;
// Check for the correct usage of the command line
if( argc > 1 && strncmp( argv[1], "--cascade=", optlen ) == 0 )
{
cascade_name = argv[1] + optlen;
input_name = argc > 2 ? argv[2] : 0;
}
else
{
fprintf( stderr,
"Usage: facedetect --cascade=\"<cascade_path>\" [filename|camera_index]\n" );
return -1;
/*input_name = argc > 1 ? argv[1] : 0;*/
}
// Load the [[HaarClassifierCascade]]
cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name, 0, 0, 0 );
// Check whether the cascade has loaded successfully. Else report and error and quit
if( !cascade )
{
fprintf( stderr, "ERROR: Could not load classifier cascade\n" );
return -1;
}
// Allocate the memory storage
storage = cvCreateMemStorage(0);
// Find whether to detect the object from file or from camera.
if( !input_name || (isdigit(input_name[0]) && input_name[1] == '\0') )
capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
else
capture = cvCaptureFromAVI( input_name );
// Create a new named window with title: result
cvNamedWindow( "result", 1 );
// Find if the capture is loaded successfully or not.
// If loaded succesfully, then:
if( capture )
{
// Capture from the camera.
for(;;)
{
// Capture the frame and load it in [[IplImage]]
if( !cvGrabFrame( capture ))
break;
frame = cvRetrieveFrame( capture );
// If the frame does not exist, quit the loop
if( !frame )
break;
// Allocate framecopy as the same size of the frame
if( !frame_copy )
frame_copy = cvCreateImage( cvSize(frame->width,frame->height),
IPL_DEPTH_8U, frame->nChannels );
// Check the origin of image. If top left, copy the image frame to frame_copy.
if( frame->origin == IPL_ORIGIN_TL )
cvCopy( frame, frame_copy, 0 );
// Else flip and copy the image
else
cvFlip( frame, frame_copy, 0 );
// Call the function to detect and draw the face
// detect_and_draw( frame_copy );
//==========================================================================//
int scale = 1;
// Create a new image based on the input image
IplImage* temp = cvCreateImage( cvSize(frame_copy->width/scale,frame_copy->height/scale), 8, 3 );
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( cascade )
{
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* faces = cvHaarDetectObjects( frame_copy, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
//Serial to Arduino data transfer
int a=1,b=2,c=3,d=4;
if((pt1.x<320)&&(pt2.y<240)){
fprintf(file,"%d\n",a);
printf("a\n");
}
else if((pt1.x>320)&&(pt1.y<240)){
fprintf(file,"%d\n",b);
printf("b\n");
}
else if((pt1.x>320)&&(pt1.y>240)){
fprintf(file,"%d\n",c);
printf("c\n");
}
else if((pt1.x< 320)&&(pt1.y>240)){
fprintf(file,"%d\n",d);
printf("d\n");
}
// fclose(file);
// printf("x=%d y=%d\n", pt1.x, pt1.y);
// Draw the rectangle in the input image
cvRectangle( frame_copy, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
}
}
// Show the image in the window named "result"
cvShowImage( "result", frame_copy );
// Release the temp image created.
cvReleaseImage( &temp );
//=====================================================================//
// Wait for a while before proceeding to the next frame
if( cvWaitKey( 10 ) >= 0 )
break;
}
// Release the images, and capture memory
cvReleaseImage( &frame_copy );
cvReleaseCapture( &capture );
}
// If the capture is not loaded succesfully, then:
else
{
// Assume the image to be lena.jpg, or the input_name specified
const char* filename = input_name ? input_name : (char*)"lena.jpg";
// Load the image from that filename
IplImage* image = cvLoadImage( filename, 1 );
// If Image is loaded succesfully, then:
if( image )
{
// Detect and draw the face
// detect_and_draw( image );
// Wait for user input
cvWaitKey(0);
// Release the image memory
cvReleaseImage( &image );
}
}
fclose(file);
// Destroy the window previously created with filename: "result"
cvDestroyWindow("result");
// return 0 to indicate successfull execution of the program
return 0;
}
/*
// Function to detect and draw any faces that is present in an image
void detect_and_draw( IplImage* img )
{
int scale = 1;
// Create a new image based on the input image
IplImage* temp = cvCreateImage( cvSize(img->width/scale,img->height/scale), 8, 3 );
// Create two points to represent the face locations
CvPoint pt1, pt2;
int i;
// Clear the memory storage which was used before
cvClearMemStorage( storage );
// Find whether the cascade is loaded, to find the faces. If yes, then:
if( cascade )
{
// There can be more than one face in an image. So create a growable sequence of faces.
// Detect the objects and store them in the sequence
CvSeq* faces = cvHaarDetectObjects( img, cascade, storage,
1.1, 2, CV_HAAR_DO_CANNY_PRUNING,
cvSize(40, 40) );
// Loop the number of faces found.
for( i = 0; i < (faces ? faces->total : 0); i++ )
{
// Create a new rectangle for drawing the face
CvRect* r = (CvRect*)cvGetSeqElem( faces, i );
// Find the dimensions of the face,and scale it if necessary
pt1.x = r->x*scale;
pt2.x = (r->x+r->width)*scale;
pt1.y = r->y*scale;
pt2.y = (r->y+r->height)*scale;
//Serial to Arduino data transfer
int a=1,b=2,c=3,d=4;
if((pt1.x<320)&&(pt2.y<240)){
fprintf(file,"%d",a);
printf("a\n");
}
else if((pt1.x>320)&&(pt1.y<240)){
fprintf(file,"%d",b);
printf("b\n");
}
else if((pt1.x>320)&&(pt1.y>240)){
fprintf(file,"%d",c);
printf("c\n");
}
else if((pt1.x< 320)&&(pt1.y>240)){
fprintf(file,"%d",d);
printf("d\n");
}
// fclose(file);
// printf("x=%d y=%d\n", pt1.x, pt1.y);
// Draw the rectangle in the input image
cvRectangle( img, pt1, pt2, CV_RGB(255,0,0), 3, 8, 0 );
}
}
// Show the image in the window named "result"
cvShowImage( "result", img );
// Release the temp image created.
cvReleaseImage( &temp );
}
*/
==================================================
==================================================
ARDUINO CODE:
....>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<...............
#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;
}
}
}
==================================================
==================================================
VIDEO DOKUMENTASI:
Sumber:
OpenCV 2.4.9 Tutorials (User Guide)
Object Tracking pan/Tilt Camera OpenCV 2.4.9 pada Ubuntu 14.04 dengan Arduino Mega
By : M yunusPeralatan 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::
/*****************************************************************************************
* Name : Fast object tracking using the OpenCV library *
* Author : Lior Chen <chen.lior@gmail.com> *
* Notice : Copyright (c) Jun 2010, Lior Chen, All Rights Reserved *
* : *
* Site : http://www.lirtex.com *
* WebPage : http://www.lirtex.com/robotics/fast-object-tracking-robot-computer-vision *
* : *
* Version : 1.0 *
* Notes : By default this code will open the first connected camera. *
* : In order to change to another camera, change *
* : CvCapture* capture = cvCaptureFromCAM( 0 ); to 1,2,3, etc. *
* : Also, the code is currently configured to tracking RED objects. *
* : This can be changed by changing the hsv_min and hsv_max vectors *
* : *
* License : This program is free software: you can redistribute it and/or modify *
* : it under the terms of the GNU General Public License as published by *
* : the Free Software Foundation, either version 3 of the License, or *
* : (at your option) any later version. *
* : *
* : This program is distributed in the hope that it will be useful, *
* : but WITHOUT ANY WARRANTY; without even the implied warranty of *
* : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* : GNU General Public License for more details. *
* : *
* : You should have received a copy of the GNU General Public License *
* : along with this program. If not, see <http://www.gnu.org/licenses/> *
******************************************************************************************/
#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>
int main(int argc, char* argv[])
{
// Default capture size - 640x480
CvSize size = cvSize(640,480);
// Open capture device. 0 is /dev/video0, 1 is /dev/video1, etc.
CvCapture* capture = cvCaptureFromCAM( 0 );
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 );
printf("Ball! x=%f y=%f r=%f\n\r",p[0],p[1],p[2] );
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 );
}
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;
}
Tutorial OpenCV Linux: #2 Load, Modify, and Save an Image
By : M yunusPada tutorial OpenCV kali ini anda akan mempelajari:
1. megambil gambar dari direktori anda dengan imread
2. merubah tampilan RGB menjadi keabuan (gratscale) dengan cvtColor
3. menyimpan hasil ubahan gambar dengan imwrite
Membuat file .cpp
buka terminal dan lakukan perintah berikut, setelah selesai menulis code simpan dengan perintah ctrl+x dan shift+y.
sudo nano RGBtoGrayscale.cpp
Code program
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main(int argc, char ** argv){
char* imageName = argv[1];
Mat image;
image= imread( imageName, 1);
if(argc!=2 || !image.data)
{
printf( " No image data\n");
return -1;
}
Mat gray_image;
cvtColor( image, gray_image, CV_BGR2GRAY);
imwrite("/home/OPENCVlat/Gray_Image.jpg",gray_image);
namedWindow( imageName, CV_WINDOW_AUTOSIZE);
namedWindow("Gray_image", CV_WINDOW_AUTOSIZE);
imshow( imageName, image);
imshow( "Gray image", gray_image);
waitKey(0);
return 0;
}
Penjelasan Code Program:
terlebih dahulu masukan modul/library yang digunakan untuk mentransformasi image RGB ke Grayscale.
#include <cv.h>
#include <highgui.h>
pada program utama terlebih dahulu kita mengambil gambar pada direktori dan menyimpanya pada variabel dengan perintah imread.
Mat image;
image= imread( imageName, 1);
kita buat variabel lain untuk menyimpan gambar hasil transformasi ke grayscale dan dengan perintah cvtColor
Mat gray_image;
cvtColor( image, gray_image, CV_BGR2GRAY);
kita simpan hasil transformasi pada direktori anda dengan perintah imwrite
imwrite("/home/OPENCVlat/Gray_Image.jpg",gray_image);
kita akan menampilkan gambar RGB (asli) dengan gambar Grayscale (transformasi) pada window .
namedWindow( imageName, CV_WINDOW_AUTOSIZE);
namedWindow("Gray_image", CV_WINDOW_AUTOSIZE);
imshow( imageName, image);
imshow( "Gray image", gray_image);
Kompilasi program:
catatan:
pastikan pada folder anda membuat code program ini terdapat bash scrip build_all.sh. jika tidak copy paste saja dari direktori OpenCV anda pada folder samples/c/build_all.sh
lakukan perintah ini pada terminal:
chmod +x build_all.sh
./build_all.sh
jika tidak ada error maka anda dapat menjalankan code anda dengan perintah. (image name) --> sesuaikan dengan nama gambay yang anda dunakan dan pastikan berada dalam satu direktori yang sama dengan code program.
./RGBtoGrayscale (imagename).jpg
setelah itu anda akan mendapatkan gambar hasil transformasi dan gambar RGB seperti berikut.
Sumber:
Tutorials Opencv-2.4.9.pdf
Tutorial OpenCV Linux: #1 Installasi OpenCV di UBUNTU 14.04
By : M yunus
| sudo apt-get update |
| sudo apt-get upgrade |
sudo
apt-get
install
build-essential
libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev
cmake python-dev python-numpy python-tk libtbb-dev libeigen3-dev yasm
libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev
libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev
sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev
libavcodec-dev libavformat-dev libswscale-dev default-jdk ant
libvtk5-qt4-dev
Sekarang kita akan men-download OpenCV.
| cd ~ |
| unzip opencv-2.4.9.zip |
| cd opencv-2.4.9 |
| 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 -D WITH_VTK=ON .. |
| make |
sudo make install |
sudo
nano /etc/ld.so.conf.d/opencv.conf
setelah itu tambahkan script ini di dalamnya, save dan exit dengan ctrl+x , shift+y
/usr/local/lib
berikan perintah berikutsudo ldconfig
Buatlah bash script dengan perintah berikutsudo gedit /etc/bash.bashrc
setelah itu tambahkan script ini di dalamnya
PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH
kita buka folder sample, adabanyak pilihan bahasa pemograman yang digunakan, pilih folder c dengan perintah beriut dan compile script.c dan .cpp dalam folder tersebut
cd ~/opencv-2.4.9/samples/c
chmod +x build_all.sh
./build_all.sh
berikut adalah contoh image processing untuk face detection file berekstensi .c./facedetect --cascade="/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml" --scale=1.5 lena.jpg
Contoh lainya
./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
Untuk menggunakan file cpp lakukan perintah berikut
~/opencv-2.4.9/build/bin/cpp-example-grabcut ~/opencv-2.4.9/samples/cpp/lena.jpg
Contoh lainya
~/opencv-2.4.9/build/bin/cpp-example-calibration_artificial
Untuk menggunakan file python OpenCV, gunakan perintah berikut.python ~/opencv-2.4.9/samples/python2/turing.py
Untuk menggunakan file Java OpenCV, gunakan perintah berikut.
cd ~/opencv-2.4.9/samples/java/ant
ant -DocvJarDir=/home/samontab/opencv-2.4.9/build/bin -DocvLibDir=/home/samontab/opencv-2.4.9/build/lib
sekarang kita menbaca sebuah video dan menggunakan OpenGL dengan Qt untuk membangun citra 3D cd ~/opencv-2.4.9/samples/cpp/Qt_sample
mkdir build
cd build
cmake ..
make
./OpenGL_Qt_Binding
Untuk membuat visualisasi 3D animasi lakukan perintah berikut cd ~/opencv-2.4.9/samples/cpp/tutorial_code/viz
g++ -o widget_pose `pkg-config opencv --cflags` widget_pose.cpp `pkg-config opencv --libs`
./widget_pose
Sumber:http://www.samontab.com/web/2014/06/installing-opencv-2-4-9-in-ubuntu-14-04-lts/ |
OUR OFFICE
By : M yunusLab. Mikrokontroler
Departemen Fisika
Fakultas Matematika Dan Ilmu Pengetahuan Alam
Institut Pertanian Bogor
Hp : 083820864843
Email : yunusmuhammad007@gmail.com
Facebook: Muhammad Yunus