Update 27 Desember 2012
Basic,
- Mat Image; //Membuat variabel image
- namedWindow( “Display window”, CV_WINDOW_AUTOSIZE ); //Membuat image
- imshow( “Display window”, image ); //Memperlihatkan image di console
- waitKey(0); //delay user key press
- image = imread(“robot.jpg”,1); // Read the file roi.create(1024,768,CV_8UC3); // create image
roi2.create(image.size(),CV_8UC3); // copy size image
Save & Load Image dari file,
- image = imread(argv[1], CV_LOAD_IMAGE_COLOR); //Membaca image
- CV_LOAD_IMAGE_UNCHANGED (<0) loads the image as is (including the alpha channel if present).
- CV_LOAD_IMAGE_GRAYSCALE ( 0) loads the image as an intensity one.
- CV_LOAD_IMAGE_COLOR (>0) loads the image in the RGB format
- imwrite( “../../images/Gray_Image.jpg”, gray_image ); //Menyimpan image
Basic Drawing,
- Point(titik)
Point pt;
pt.x = 10;
pt.y = 8;
atau,
Point pt = Point(10, 8); - Scalar( a, b, c );
- Mat src = Mat::zeros( 400, 400, CV_8UC3 ); //membuat blank image
- rectangle( src,Point(0, 0),Point( 0, 0),Scalar(0, 255, 255 ),1 ); //kotak
- circle(img, Point(100,100), 20, Scalar(0,255,0), 1); //lingkaran
- line(img, Point(100,100), Point(200,200), Scalar(0,255,0), 1); //garis
- ellipse( img,Point(100, 100 ),Size( 200, 200 ),angle,0,360,Scalar( 255, 0, 0 ),thickness,lineType );
- putText (image2,”My comment”,Point(200,400), 1,2, Scalar(255,255,0),1,8); //tulisan
Filtering,
- cvtColor( image, gray_image, CV_RGB2GRAY );
- erode( src, erosion_dst, element ); //erosion
- dilate( src, dilation_dst, element ); //dilation
- blur(InputArray src, OutputArray dst, Size ksize, Point anchor=Point(-1,-1), int borderType=BORDER_DEFAULT ) //smoothing
- GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT );
- morphologyEx( src, dst, operation, element ); //New filtering
- operation,
- Element
- int morph_elem = 0;
- int morph_size = 0;
- int morph_operator = 0;
- int const max_operator = 4;
- int const max_elem = 2;
- int const max_kernel_size = 21;
- Mat element = getStructuringElement( morph_elem, Size( 2*morph_size + 1, 2*morph_size+1 ), Point( morph_size, morph_size ) );
- Sobel
int scale = 1;
int delta = 0;
int ddepth = CV_16S;/// Generate grad_x and grad_y
Mat grad_x, grad_y;
Mat abs_grad_x, abs_grad_y;/// Gradient X
//Scharr( src_gray, grad_x, ddepth, 1, 0, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_x, abs_grad_x );/// Gradient Y
//Scharr( src_gray, grad_y, ddepth, 0, 1, scale, delta, BORDER_DEFAULT );
Sobel( src_gray, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT );
convertScaleAbs( grad_y, abs_grad_y );/// Total Gradient (approximate)
addWeighted( abs_grad_x, 0.5, abs_grad_y, 0.5, 0, grad );
imshow( window_name, grad );Gambar 1. sobel
- Laplace
int kernel_size = 3;
int scale = 1
;int delta = 0;
int ddepth = CV_16S;/// Apply Laplace function
Mat abs_dst;Laplacian( src_gray, dst, ddepth, kernel_size, scale, delta, BORDER_DEFAULT );
convertScaleAbs( dst, abs_dst ); //convert to CV_8U imageGambar 2. laplace
- Canny
int edgeThresh = 1;
int lowThreshold;
int const max_lowThreshold = 100;
int ratio = 3;
int kernel_size = 3;/// Reduce noise with a kernel 3×3
blur( src_gray, detected_edges, Size(3,3) );/// Canny detector
Canny( detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size );/// Using Canny’s output as a mask, we display our result
dst = Scalar::all(0);imshow( window_name, dst );
Gambar 3. Canny
Accessing Pixel,
- Scalar intensity = img.at(y, x); //Grayscale
- img.at(y, x) = 128; //Mengubah scale intensity
- Scalar intensity = img.at(Point(x, y)); //RGB
- Vec3b intensity = img.at(y, x); //uchar
uchar blue = intensity.val[0];
uchar green = intensity.val[1];
uchar red = intensity.val[2]; - Vec3f intensity = img.at(y, x); //float
float blue = intensity.val[0];
float green = intensity.val[1];
float red = intensity.val[2];
- Vec3b intensity = img.at(y, x); //uchar
- int dataimg = image.at(y,x); //Ambil data pixel
Scaling/Resize image,
- pyrUp( tmp, dst, Size( tmp.cols*2, tmp.rows*2 ); //Zoom Out
- pyrDown( tmp, dst, Size( tmp.cols/2, tmp.rows/2 ); //Zoom In
Memory Managemant, <Penting!!!>
- Mat img = imread(“image.jpg”);
Mat img1 = img.clone(); //clone image - Mat img2
img.copyTo(img2); //copy image - src.convertTo(dst, CV_32F); //convert image
- CV_8U, CV_8S, CV_16U, CV_16S, CV_32S, CV_32F, CV_64F.
Converter, <Penting!!!>
-
///converting IplImage to cv::Mat Mat image=cvarrToMat(img);
-
///Converting Mat to IplImage
(you have cv::Mat old) IplImage copy = old; IplImage* new_image = ©