Created
November 8, 2015 02:13
-
-
Save minhoolee/b0e3ed0b31cbe51078d5 to your computer and use it in GitHub Desktop.
MVRT Vision Trainings Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
using namespace std; | |
using namespace cv; | |
/// Global Variables | |
int DELAY_CAPTION = 1500; | |
int DELAY_BLUR = 100; | |
int MAX_KERNEL_LENGTH = 31; | |
Mat src; Mat dst; | |
char window_name[] = "Filter Demo 1"; | |
/// Function headers | |
int display_caption( char* caption ); | |
int display_dst( int delay ); | |
/** | |
* function main | |
*/ | |
int main( int argc, char** argv ) | |
{ | |
namedWindow( window_name, CV_WINDOW_AUTOSIZE ); | |
/// Load the source image | |
src = imread( "../images/lena.jpg", 1 ); | |
if( display_caption( "Original Image" ) != 0 ) { return 0; } | |
dst = src.clone(); | |
if( display_dst( DELAY_CAPTION ) != 0 ) { return 0; } | |
/// Applying Homogeneous blur | |
if( display_caption( "Homogeneous Blur" ) != 0 ) { return 0; } | |
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) | |
{ blur( src, dst, Size( i, i ), Point(-1,-1) ); | |
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } | |
/// Applying Gaussian blur | |
if( display_caption( "Gaussian Blur" ) != 0 ) { return 0; } | |
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) | |
{ GaussianBlur( src, dst, Size( i, i ), 0, 0 ); | |
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } | |
/// Applying Median blur | |
if( display_caption( "Median Blur" ) != 0 ) { return 0; } | |
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) | |
{ medianBlur ( src, dst, i ); | |
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } | |
/// Applying Bilateral Filter | |
if( display_caption( "Bilateral Blur" ) != 0 ) { return 0; } | |
for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ) | |
{ bilateralFilter ( src, dst, i, i*2, i/2 ); | |
if( display_dst( DELAY_BLUR ) != 0 ) { return 0; } } | |
/// Wait until user press a key | |
display_caption( "End: Press a key!" ); | |
waitKey(0); | |
return 0; | |
} | |
int display_caption( char* caption ) | |
{ | |
dst = Mat::zeros( src.size(), src.type() ); | |
putText( dst, caption, | |
Point( src.cols/4, src.rows/2), | |
CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255) ); | |
imshow( window_name, dst ); | |
int c = waitKey( DELAY_CAPTION ); | |
if( c >= 0 ) { return -1; } | |
return 0; | |
} | |
int display_dst( int delay ) | |
{ | |
imshow( window_name, dst ); | |
int c = waitKey ( delay ); | |
if( c >= 0 ) { return -1; } | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
#include <stdlib.h> | |
#include <stdio.h> | |
using namespace cv; | |
/// Global variables | |
Mat src, src_gray; | |
Mat dst, detected_edges; | |
int edgeThresh = 1; | |
int lowThreshold; | |
int const max_lowThreshold = 100; | |
int ratio = 3; | |
int kernel_size = 3; | |
char* window_name = "Edge Map"; | |
/** | |
* @function CannyThreshold | |
* @brief Trackbar callback - Canny thresholds input with a ratio 1:3 | |
*/ | |
void CannyThreshold(int, void*) | |
{ | |
/// Reduce noise with a kernel 3x3 | |
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); | |
src.copyTo( dst, detected_edges); | |
imshow( window_name, dst ); | |
} | |
/** @function main */ | |
int main( int argc, char** argv ) | |
{ | |
/// Load an image | |
src = imread( argv[1] ); | |
if( !src.data ) | |
{ return -1; } | |
/// Create a matrix of the same type and size as src (for dst) | |
dst.create( src.size(), src.type() ); | |
/// Convert the image to grayscale | |
cvtColor( src, src_gray, CV_BGR2GRAY ); | |
/// Create a window | |
namedWindow( window_name, CV_WINDOW_AUTOSIZE ); | |
/// Create a Trackbar for user to enter threshold | |
createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, CannyThreshold ); | |
/// Show the image | |
CannyThreshold(0, 0); | |
/// Wait until user exit program by pressing a key | |
waitKey(0); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
#include "highgui.h" | |
#include <stdlib.h> | |
#include <stdio.h> | |
using namespace cv; | |
/// Global variables | |
Mat src, erosion_dst, dilation_dst; | |
int erosion_elem = 0; | |
int erosion_size = 0; | |
int dilation_elem = 0; | |
int dilation_size = 0; | |
int const max_elem = 2; | |
int const max_kernel_size = 21; | |
/** Function Headers */ | |
void Erosion( int, void* ); | |
void Dilation( int, void* ); | |
/** @function main */ | |
int main( int argc, char** argv ) | |
{ | |
/// Load an image | |
src = imread( argv[1] ); | |
if( !src.data ) | |
{ return -1; } | |
/// Create windows | |
namedWindow( "Erosion Demo", CV_WINDOW_AUTOSIZE ); | |
namedWindow( "Dilation Demo", CV_WINDOW_AUTOSIZE ); | |
cvMoveWindow( "Dilation Demo", src.cols, 0 ); | |
/// Create Erosion Trackbar | |
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Erosion Demo", | |
&erosion_elem, max_elem, | |
Erosion ); | |
createTrackbar( "Kernel size:\n 2n +1", "Erosion Demo", | |
&erosion_size, max_kernel_size, | |
Erosion ); | |
/// Create Dilation Trackbar | |
createTrackbar( "Element:\n 0: Rect \n 1: Cross \n 2: Ellipse", "Dilation Demo", | |
&dilation_elem, max_elem, | |
Dilation ); | |
createTrackbar( "Kernel size:\n 2n +1", "Dilation Demo", | |
&dilation_size, max_kernel_size, | |
Dilation ); | |
/// Default start | |
Erosion( 0, 0 ); | |
Dilation( 0, 0 ); | |
waitKey(0); | |
return 0; | |
} | |
/** @function Erosion */ | |
void Erosion( int, void* ) | |
{ | |
int erosion_type; | |
if( erosion_elem == 0 ){ erosion_type = MORPH_RECT; } | |
else if( erosion_elem == 1 ){ erosion_type = MORPH_CROSS; } | |
else if( erosion_elem == 2) { erosion_type = MORPH_ELLIPSE; } | |
Mat element = getStructuringElement( erosion_type, | |
Size( 2*erosion_size + 1, 2*erosion_size+1 ), | |
Point( erosion_size, erosion_size ) ); | |
/// Apply the erosion operation | |
erode( src, erosion_dst, element ); | |
imshow( "Erosion Demo", erosion_dst ); | |
} | |
/** @function Dilation */ | |
void Dilation( int, void* ) | |
{ | |
int dilation_type; | |
if( dilation_elem == 0 ){ dilation_type = MORPH_RECT; } | |
else if( dilation_elem == 1 ){ dilation_type = MORPH_CROSS; } | |
else if( dilation_elem == 2) { dilation_type = MORPH_ELLIPSE; } | |
Mat element = getStructuringElement( dilation_type, | |
Size( 2*dilation_size + 1, 2*dilation_size+1 ), | |
Point( dilation_size, dilation_size ) ); | |
/// Apply the dilation operation | |
dilate( src, dilation_dst, element ); | |
imshow( "Dilation Demo", dilation_dst ); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "opencv2/imgproc/imgproc.hpp" | |
#include "opencv2/highgui/highgui.hpp" | |
#include <stdlib.h> | |
#include <stdio.h> | |
using namespace cv; | |
/** @function main */ | |
int main( int argc, char** argv ) | |
{ | |
Mat src, src_gray; | |
Mat grad; | |
char* window_name = "Sobel Demo - Simple Edge Detector"; | |
int scale = 1; | |
int delta = 0; | |
int ddepth = CV_16S; | |
int c; | |
/// Load an image | |
src = imread( argv[1] ); | |
if( !src.data ) | |
{ return -1; } | |
GaussianBlur( src, src, Size(3,3), 0, 0, BORDER_DEFAULT ); | |
/// Convert it to gray | |
cvtColor( src, src_gray, CV_BGR2GRAY ); | |
/// Create window | |
namedWindow( window_name, CV_WINDOW_AUTOSIZE ); | |
/// 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 ); | |
waitKey(0); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment