├── .gitignore ├── CMakeLists.txt ├── IplImage ├── BinaryThresholdIpl │ ├── BinaryThresholdIpl.cpp │ └── CMakeLists.txt ├── CMakeLists.txt ├── ConvertRGBImageToHSVImagesIpl │ ├── CMakeLists.txt │ └── ConvertRGBImageToHSVImagesIpl.cpp ├── ConvertRGBImageToYCbCrImagesIpl │ ├── CMakeLists.txt │ └── ConvertRGBImageToYCbCrImagesIpl.cpp ├── DisplayAnImageIpl │ ├── CMakeLists.txt │ └── DisplayAnImageIpl.cpp ├── DrawCircleIpl │ ├── CMakeLists.txt │ └── DrawCircleIpl.cpp ├── ExtractRGBComponentsIpl │ ├── CMakeLists.txt │ └── ExtractRGBComponentsIpl.cpp ├── GetImageSizeIpl │ ├── CMakeLists.txt │ └── GetImageSizeIpl.cpp ├── HoughCirclesIpl │ ├── CMakeLists.txt │ └── HoughCirclesIpl.cpp ├── MSERFeatureDetectionIpl │ ├── CMakeLists.txt │ └── MSERFeatureDetectionIpl.cpp ├── STARFeatureDetectionIpl │ ├── CMakeLists.txt │ └── STARFeatureDetectionIpl.cpp └── SURFFeatureDetectionIpl │ ├── CMakeLists.txt │ └── SURFFeatureDetectionIpl.cpp ├── NeuralNetwork ├── CMakeLists.txt └── NeuralNetwork.cpp └── cvMat ├── BinaryThreshold ├── BinaryThreshold.cpp └── CMakeLists.txt ├── CMakeLists.txt ├── CannyEdgeDetector ├── CMakeLists.txt └── CannyEdgeDetector.cpp ├── ConvertIplToMat ├── CMakeLists.txt └── ConvertIplToMat.cpp ├── ConvertRGBImageToHSVImages ├── CMakeLists.txt └── ConvertRGBImageToHSVImages.cpp ├── ConvertRGBImageToYCbCrImages ├── CMakeLists.txt └── ConvertRGBImageToYCbCrImages.cpp ├── DisplayAnImage ├── CMakeLists.txt └── DisplayAnImage.cpp ├── DisplayHistogram ├── CMakeLists.txt └── DisplayHistogram.cpp ├── DisplayRGBHistogram ├── CMakeLists.txt └── DisplayRGBHistogram.cpp ├── DrawCircle ├── CMakeLists.txt └── DrawCircle.cpp ├── ExtractRGBComponents ├── CMakeLists.txt └── ExtractRGBComponents.cpp ├── GetImageSize ├── CMakeLists.txt └── GetImageSize.cpp ├── GoodFeaturesToTrack ├── CMakeLists.txt └── GoodFeaturesToTrack.cpp ├── HoughCircles ├── CMakeLists.txt └── HoughCircles.cpp ├── HoughLines ├── CMakeLists.txt └── HoughLines.cpp ├── KMeansOnRGBImage ├── CMakeLists.txt └── KMeansOnRGBImage.cpp ├── MSERFeatureDetection ├── CMakeLists.txt └── MSERFeatureDetection.cpp ├── RotateAnImage ├── CMakeLists.txt └── RotateAnImage.cpp ├── STARFeatureDetection ├── CMakeLists.txt └── STARFeatureDetection.cpp ├── SURFFeatureDetection ├── CMakeLists.txt └── SURFFeatureDetection.cpp ├── Statistics ├── CMakeLists.txt ├── Entropy │ ├── CMakeLists.txt │ └── Entropy.cpp ├── Kurtosis │ ├── CMakeLists.txt │ ├── Kurtosis.cpp │ └── README.txt ├── Max │ ├── CMakeLists.txt │ └── Max.cpp ├── Mean │ ├── CMakeLists.txt │ └── Mean.cpp ├── Median │ ├── CMakeLists.txt │ └── Median.cpp ├── Min │ ├── CMakeLists.txt │ └── Min.cpp ├── Mode │ ├── CMakeLists.txt │ └── Mode.cpp ├── Skewness │ ├── CMakeLists.txt │ ├── README.txt │ └── Skewness.cpp ├── StandardDeviation │ ├── CMakeLists.txt │ └── StandardDeviation.cpp └── Variance │ ├── CMakeLists.txt │ └── Variance.cpp └── YCrCbSkinDetection ├── CMakeLists.txt └── YCrCbSkinDetection.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | build/ 2 | Build/ 3 | BUILD/ 4 | CMakeLists.txt.user* 5 | *~ 6 | *.bak 7 | *.orig 8 | *.swp 9 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( OpenCVExamples ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | set( CMAKE_RUNTIME_OUTPUT_DIRECTORY 8 | ${OpenCVExamples_BINARY_DIR}/bin 9 | ) 10 | 11 | add_subdirectory( IplImage ) 12 | add_subdirectory( cvMat ) 13 | 14 | # add_subdirectory( NeuralNetwork ) 15 | -------------------------------------------------------------------------------- /IplImage/BinaryThresholdIpl/BinaryThresholdIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* image = cvLoadImage( argv[1] ); 15 | if( !image ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | CvSize size = cvSize( image->width, image->height ); 22 | IplImage* image_gray = cvCreateImage( size, image->depth, 1 ); 23 | IplImage* dst = cvCreateImage( size, image->depth, 1 ); 24 | 25 | cvCvtColor( image, image_gray, CV_RGB2GRAY ); 26 | int threshold_value=100; 27 | int max_BINARY_value=200; 28 | 29 | cvThreshold( image_gray, dst, threshold_value, max_BINARY_value,0 ); 30 | 31 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 32 | cvShowImage( argv[1], dst ); 33 | 34 | cvSaveImage( "binaryThreshold.png", dst); 35 | 36 | cvReleaseImage( &image ); 37 | cvReleaseImage( &image_gray ); 38 | cvReleaseImage( &dst ); 39 | cv::waitKey(0); 40 | 41 | return EXIT_SUCCESS; 42 | } -------------------------------------------------------------------------------- /IplImage/BinaryThresholdIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( BinaryThresholdIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( BinaryThresholdIpl BinaryThresholdIpl.cpp ) 10 | target_link_libraries( BinaryThresholdIpl ${OpenCV_LIBS} ) -------------------------------------------------------------------------------- /IplImage/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory( GetImageSizeIpl ) 2 | add_subdirectory( DisplayAnImageIpl ) 3 | add_subdirectory( ConvertRGBImageToHSVImagesIpl ) 4 | add_subdirectory( ConvertRGBImageToYCbCrImagesIpl ) 5 | add_subdirectory( DrawCircleIpl ) 6 | add_subdirectory( SURFFeatureDetectionIpl ) 7 | add_subdirectory( MSERFeatureDetectionIpl ) 8 | add_subdirectory( STARFeatureDetectionIpl ) 9 | add_subdirectory( HoughCirclesIpl ) 10 | add_subdirectory( ExtractRGBComponentsIpl ) 11 | add_subdirectory( BinaryThresholdIpl ) 12 | -------------------------------------------------------------------------------- /IplImage/ConvertRGBImageToHSVImagesIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( ConvertRGBImageToHSVImagesIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( ConvertRGBImageToHSVImagesIpl ConvertRGBImageToHSVImagesIpl.cpp ) 10 | target_link_libraries( ConvertRGBImageToHSVImagesIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/ConvertRGBImageToHSVImagesIpl/ConvertRGBImageToHSVImagesIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cv.h" 5 | #include "highgui.h" 6 | 7 | int main( int argc, char* argv[] ) 8 | { 9 | if( argc != 2 ) 10 | { 11 | std::cerr << "Usage: " << argv[0] << "" << std::endl; 12 | return EXIT_FAILURE; 13 | } 14 | 15 | IplImage* img = cvLoadImage( argv[1] ); 16 | if( !img ) 17 | { 18 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 19 | return EXIT_FAILURE; 20 | } 21 | 22 | std::cout << img->width << " x " << img->height << std::endl; 23 | 24 | CvSize size = cvSize( img->width, img->height ); 25 | 26 | IplImage* imageHSV = cvCreateImage( size, img->depth, 3 ); 27 | cvCvtColor( img, imageHSV, CV_BGR2HSV ); 28 | 29 | IplImage* imageH = cvCreateImage( size, img->depth, 1 ); 30 | IplImage* imageS = cvCreateImage( size, img->depth, 1 ); 31 | IplImage* imageV = cvCreateImage( size, img->depth, 1 ); 32 | 33 | cvSplit( imageHSV, imageH, imageS, imageV, NULL ); 34 | cvReleaseImage( &imageHSV ); 35 | 36 | cvSaveImage( "hue.png", imageH ); 37 | cvSaveImage( "saturation.png", imageS ); 38 | cvSaveImage( "value.png", imageV ); 39 | 40 | cvReleaseImage( &imageH ); 41 | cvReleaseImage( &imageS ); 42 | cvReleaseImage( &imageV ); 43 | 44 | cvReleaseImage( &img ); 45 | 46 | return EXIT_SUCCESS; 47 | } 48 | -------------------------------------------------------------------------------- /IplImage/ConvertRGBImageToYCbCrImagesIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( ConvertRGBImageToYCbCrImagesIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( ConvertRGBImageToYCbCrImagesIpl ConvertRGBImageToYCbCrImagesIpl.cpp ) 10 | target_link_libraries( ConvertRGBImageToYCbCrImagesIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/ConvertRGBImageToYCbCrImagesIpl/ConvertRGBImageToYCbCrImagesIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cv.h" 5 | #include "highgui.h" 6 | 7 | int main( int argc, char* argv[] ) 8 | { 9 | if( argc != 2 ) 10 | { 11 | std::cerr << "Usage: " << argv[0] << "" << std::endl; 12 | return EXIT_FAILURE; 13 | } 14 | 15 | IplImage* img = cvLoadImage( argv[1] ); 16 | if( !img ) 17 | { 18 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 19 | return EXIT_FAILURE; 20 | } 21 | 22 | std::cout << img->width << " x " << img->height << std::endl; 23 | 24 | CvSize size = cvSize( img->width, img->height ); 25 | 26 | IplImage* imageYCrCb = cvCreateImage( size, img->depth, 3 ); 27 | cvCvtColor( img, imageYCrCb, CV_BGR2YCrCb ); 28 | 29 | IplImage* imageY = cvCreateImage( size, img->depth, 1 ); 30 | IplImage* imageCr = cvCreateImage( size, img->depth, 1 ); 31 | IplImage* imageCb = cvCreateImage( size, img->depth, 1 ); 32 | 33 | cvSplit( imageYCrCb, imageY, imageCr, imageCb, NULL ); 34 | cvReleaseImage( &imageYCrCb ); 35 | 36 | cvSaveImage( "y.png", imageY ); 37 | cvSaveImage( "Cr.png", imageCr ); 38 | cvSaveImage( "Cb.png", imageCb); 39 | 40 | cvReleaseImage( &imageY ); 41 | cvReleaseImage( &imageCr ); 42 | cvReleaseImage( &imageCb ); 43 | 44 | cvReleaseImage( &img ); 45 | 46 | return EXIT_SUCCESS; 47 | } 48 | -------------------------------------------------------------------------------- /IplImage/DisplayAnImageIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( DisplayAnImageIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( DisplayAnImageIpl DisplayAnImageIpl.cpp ) 10 | target_link_libraries( DisplayAnImageIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/DisplayAnImageIpl/DisplayAnImageIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* image = cvLoadImage( argv[1] ); 15 | if( !image ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 22 | cvShowImage( argv[1], image ); 23 | 24 | cvReleaseImage( &image ); 25 | cv::waitKey(0); 26 | 27 | return EXIT_SUCCESS; 28 | } 29 | -------------------------------------------------------------------------------- /IplImage/DrawCircleIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( DrawCircleIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( DrawCircleIpl DrawCircleIpl.cpp ) 10 | target_link_libraries( DrawCircleIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/DrawCircleIpl/DrawCircleIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 3 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* image = cvLoadImage( argv[1] ); 15 | if( !image ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | CvPoint center; 22 | center.x = image->width / 2; 23 | center.y = image->height / 2; 24 | int radius = 10; 25 | 26 | CvScalar color; 27 | color.val[0] = 255.; 28 | 29 | int thickness = atoi( argv[2] ); // if thickness < 0, the circle is filled 30 | 31 | cvCircle( image, center, radius, color, thickness ); 32 | 33 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 34 | cvShowImage( argv[1], image ); 35 | 36 | cvReleaseImage( &image ); 37 | cv::waitKey(0); 38 | 39 | return EXIT_SUCCESS; 40 | } 41 | -------------------------------------------------------------------------------- /IplImage/ExtractRGBComponentsIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( ExtractRGBComponentsIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( ExtractRGBComponentsIpl ExtractRGBComponentsIpl.cpp ) 10 | target_link_libraries( ExtractRGBComponentsIpl ${OpenCV_LIBS} ) -------------------------------------------------------------------------------- /IplImage/ExtractRGBComponentsIpl/ExtractRGBComponentsIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* image = cvLoadImage( argv[1] ); 15 | if( !image ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | IplImage *r = cvCreateImage(cvSize(image->width, image->height ),image->depth, 1); 22 | IplImage *g = cvCreateImage(cvSize(image->width, image->height ),image->depth, 1); 23 | IplImage *b = cvCreateImage(cvSize(image->width, image->height ),image->depth, 1); 24 | 25 | cvSplit( image, b, g, r, 0 ); 26 | 27 | cvSaveImage( "red.png", r ); 28 | cvSaveImage( "green.png", g ); 29 | cvSaveImage( "blue.png", b ); 30 | 31 | cvReleaseImage( &r ); 32 | cvReleaseImage( &g ); 33 | cvReleaseImage( &b ); 34 | 35 | cvReleaseImage( &image ); 36 | 37 | return EXIT_SUCCESS; 38 | } -------------------------------------------------------------------------------- /IplImage/GetImageSizeIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( GetImageSizeIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( GetImageSizeIpl GetImageSizeIpl.cpp ) 10 | target_link_libraries( GetImageSizeIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/GetImageSizeIpl/GetImageSizeIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cv.h" 5 | #include "highgui.h" 6 | 7 | int main( int argc, char* argv[] ) 8 | { 9 | if( argc != 2 ) 10 | { 11 | std::cerr << "Usage: " << argv[0] << "" << std::endl; 12 | return EXIT_FAILURE; 13 | } 14 | 15 | IplImage* img = cvLoadImage( argv[1] ); 16 | if( !img ) 17 | { 18 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 19 | return EXIT_FAILURE; 20 | } 21 | 22 | std::cout << img->width << " x " << img->height << std::endl; 23 | 24 | CvSize size = cvSize( img->width, img->height ); 25 | // std::cout << size << std::endl; 26 | 27 | cvReleaseImage( &img ); 28 | 29 | return EXIT_SUCCESS; 30 | } 31 | -------------------------------------------------------------------------------- /IplImage/HoughCirclesIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( HoughCirclesIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( HoughCirclesIpl HoughCirclesIpl.cpp ) 10 | target_link_libraries( HoughCirclesIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/HoughCirclesIpl/HoughCirclesIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* image = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE ); 15 | if( !image ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | int method = CV_HOUGH_GRADIENT; 22 | 23 | 24 | // The inverse ratio of the accumulator resolution to the image resolution. 25 | // For example, if dp=1 , the accumulator will have the same resolution as the 26 | // input image, if dp=2 - accumulator will have half as big width and height, etc 27 | double dp = 1.; 28 | 29 | // Minimum distance between the centers of the detected circles. If the parameter 30 | // is too small, multiple neighbor circles may be falsely detected in addition 31 | // to a true one. If it is too large, some circles may be missed 32 | double minDist = 5.; 33 | 34 | // The first method-specific parameter. in the case of CV_HOUGH_GRADIENT it is 35 | // the higher threshold of the two passed to Canny() edge detector (the lower 36 | // one will be twice smaller) 37 | double param1=100.; 38 | 39 | // The second method-specific parameter. in the case of CV_HOUGH_GRADIENT it is 40 | // the accumulator threshold at the center detection stage. The smaller it is, 41 | // the more false circles may be detected. Circles, corresponding to the larger 42 | // accumulator values, will be returned first 43 | double param2=20.; 44 | 45 | // Minimum circle radius 46 | int minRadius = 5; 47 | 48 | // Maximum circle radius 49 | int maxRadius = 100; 50 | 51 | CvMemStorage* storage = cvCreateMemStorage(0); 52 | 53 | CvSeq* results = cvHoughCircles( image, storage, method, dp, minDist, param1, param2, minRadius, maxRadius ); 54 | 55 | CvScalar color; 56 | color.val[0] = 255.; 57 | 58 | int thickness = 3; 59 | 60 | size_t i = 1; 61 | 62 | for( int i = 0; i < results->total; i++ ) 63 | { 64 | float* p = (float*) cvGetSeqElem( results, i ); 65 | std::cout << "feature point " << i << std::endl; 66 | std::cout << "position: [ " << p[0] << ", " << p[1] << "]" << std::endl; 67 | std::cout << "radius: " << p[2] << std::endl; 68 | std::cout << std::endl; 69 | 70 | cvCircle( image, cv::Point( p[0], p[1] ), p[2], color, thickness ); 71 | } 72 | 73 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 74 | cvShowImage( argv[1], image ); 75 | 76 | cvReleaseImage( &image ); 77 | cv::waitKey(0); 78 | 79 | return EXIT_SUCCESS; 80 | } 81 | -------------------------------------------------------------------------------- /IplImage/MSERFeatureDetectionIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( MSERFeatureDetectionIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( MSERFeatureDetectionIpl MSERFeatureDetectionIpl.cpp ) 10 | target_link_libraries( MSERFeatureDetectionIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/MSERFeatureDetectionIpl/MSERFeatureDetectionIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* image = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE ); 15 | if( !image ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | std::vector< std::vector< cv::Point > > featurePoints; 22 | 23 | /* 24 | ( int _delta, int _min_area, int _max_area, 25 | float _max_variation, float _min_diversity, 26 | int _max_evolution, double _area_threshold, 27 | double _min_margin, int _edge_blur_size ); 28 | */ 29 | 30 | cv::MSER detector; 31 | 32 | detector( image, featurePoints, cv::Mat() ); 33 | 34 | CvScalar color; 35 | color.val[0] = 255.; 36 | 37 | int thickness = -1; 38 | 39 | size_t i = 1; 40 | 41 | for( std::vector< std::vector< cv::Point > >::const_iterator it = featurePoints.begin(); 42 | it != featurePoints.end(); 43 | ++it, ++i ) 44 | { 45 | size_t j = 1; 46 | 47 | for( std::vector< cv::Point >::const_iterator pIt = it->begin(); 48 | pIt != it->end(); 49 | ++pIt, ++j ) 50 | { 51 | std::cout << "feature point " << i <<" * " << j << std::endl; 52 | std::cout << "position: [ " << pIt->x << ", " << pIt->y << "]" << std::endl; 53 | std::cout << std::endl; 54 | 55 | cvCircle( image, *pIt, 10, color, thickness ); 56 | } 57 | } 58 | 59 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 60 | cvShowImage( argv[1], image ); 61 | 62 | cvReleaseImage( &image ); 63 | cv::waitKey(0); 64 | 65 | return EXIT_SUCCESS; 66 | } 67 | -------------------------------------------------------------------------------- /IplImage/STARFeatureDetectionIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( STARFeatureDetectionIpl) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( STARFeatureDetectionIpl STARFeatureDetectionIpl.cpp ) 10 | target_link_libraries( STARFeatureDetectionIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/STARFeatureDetectionIpl/STARFeatureDetectionIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 7 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* image = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE ); 15 | if( !image ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | std::vector< cv::KeyPoint > featurePoints; 22 | 23 | // the full constructor initialized all the algorithm parameters: 24 | // maxSize - maximum size of the features. The following 25 | // values of the parameter are supported: 26 | // 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128 27 | 28 | // responseThreshold - threshold for the approximated laplacian, 29 | // used to eliminate weak features. The larger it is, 30 | // the less features will be retrieved 31 | 32 | // lineThresholdProjected - another threshold for the laplacian to 33 | // eliminate edges 34 | 35 | // lineThresholdBinarized - another threshold for the feature 36 | // size to eliminate edges. 37 | // The larger the 2 threshold, the more points you get. 38 | 39 | int maxSize = atoi( argv[2] ); 40 | int responseThreshold = atoi( argv[3] ); 41 | int lineThresholdProjected = atoi( argv[4] ); 42 | int lineThresholdBinarized = atoi( argv[5] ); 43 | int suppressNonmaxSize = atoi( argv[6] ); 44 | 45 | cv::StarDetector detector( maxSize, 46 | responseThreshold, 47 | lineThresholdProjected, 48 | lineThresholdBinarized, 49 | suppressNonmaxSize);; 50 | 51 | detector( image, featurePoints ); 52 | 53 | CvScalar color; 54 | color.val[0] = 255.; 55 | 56 | int thickness = -1; 57 | 58 | size_t i = 1; 59 | 60 | for( std::vector< cv::KeyPoint >::const_iterator it = featurePoints.begin(); 61 | it != featurePoints.end(); 62 | ++it, ++i ) 63 | { 64 | std::cout << "feature point " << i << std::endl; 65 | std::cout << "position: [ " << it->pt.x << ", " << it->pt.y << "]" << std::endl; 66 | std::cout << "size: " << it->size << std::endl; 67 | std::cout << "angle: " << it->angle << std::endl; 68 | std::cout << "response: " << it->response << std::endl; 69 | std::cout << "octave: " << it->octave << std::endl; 70 | 71 | std::cout << std::endl; 72 | 73 | cvCircle( image, it->pt, 10, color, thickness ); 74 | } 75 | 76 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 77 | cvShowImage( argv[1], image ); 78 | 79 | cvReleaseImage( &image ); 80 | cv::waitKey(0); 81 | 82 | return EXIT_SUCCESS; 83 | } 84 | -------------------------------------------------------------------------------- /IplImage/SURFFeatureDetectionIpl/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( SURFFeatureDetectionIpl ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( SURFFeatureDetectionIpl SURFFeatureDetectionIpl.cpp ) 10 | target_link_libraries( SURFFeatureDetectionIpl ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /IplImage/SURFFeatureDetectionIpl/SURFFeatureDetectionIpl.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 6 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | std::cerr << "nOctaves: 4" << std::endl; 12 | std::cerr << "nOctaveLayers: 2" << std::endl; 13 | std::cerr << "extended: 0" << std::endl; 14 | return EXIT_FAILURE; 15 | } 16 | 17 | IplImage* image = cvLoadImage( argv[1], CV_LOAD_IMAGE_GRAYSCALE ); 18 | if( !image ) 19 | { 20 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 21 | return EXIT_FAILURE; 22 | } 23 | 24 | std::vector< cv::KeyPoint > featurePoints; 25 | 26 | double hessianThreshold = atof( argv[2] ); 27 | int nOctaves = atoi( argv[3] ); // 4 28 | int nOctaveLayers = atoi( argv[4] );// 2; 29 | bool extended = ( atoi( argv[5] ) != 0 );// false; 30 | 31 | cv::SURF detector = cv::SURF(hessianThreshold, nOctaves, nOctaveLayers, extended ); 32 | detector( image, cv::Mat(), featurePoints ); 33 | 34 | std::cout << featurePoints.size() << std::endl; 35 | 36 | CvScalar color; 37 | color.val[0] = 255.; 38 | 39 | int thickness = -1; 40 | 41 | size_t i = 1; 42 | 43 | for( std::vector< cv::KeyPoint >::const_iterator it = featurePoints.begin(); 44 | it != featurePoints.end(); 45 | ++it, ++i ) 46 | { 47 | std::cout << "feature point " << i << std::endl; 48 | std::cout << "position: [ " << it->pt.x << ", " << it->pt.y << "]" << std::endl; 49 | std::cout << "size: " << it->size << std::endl; 50 | std::cout << "angle: " << it->angle << std::endl; 51 | std::cout << "response: " << it->response << std::endl; 52 | std::cout << "octave: " << it->octave << std::endl; 53 | 54 | std::cout << std::endl; 55 | 56 | cvCircle( image, it->pt, 10, color, thickness ); 57 | } 58 | 59 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 60 | cvShowImage( argv[1], image ); 61 | 62 | cvReleaseImage( &image ); 63 | cv::waitKey(0); 64 | 65 | return EXIT_SUCCESS; 66 | } 67 | -------------------------------------------------------------------------------- /NeuralNetwork/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( NeuralNetwork ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( NeuralNetwork NeuralNetwork.cpp ) 10 | target_link_libraries( NeuralNetwork ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /NeuralNetwork/NeuralNetwork.cpp: -------------------------------------------------------------------------------- 1 | // Example : neural network learning 2 | // usage: prog training_data_file testing_data_file 3 | 4 | // For use with test / training datasets : handwritten_ex 5 | 6 | // Author : Toby Breckon, toby.breckon@cranfield.ac.uk 7 | 8 | // Copyright (c) 2010 School of Engineering, Cranfield University 9 | // License : LGPL - http://www.gnu.org/licenses/lgpl.html 10 | 11 | #include "cv.h" // opencv general include file 12 | #include "ml.h" // opencv machine learning include file 13 | 14 | #include 15 | 16 | // N.B. classes are integer handwritten digits in range 0-9 17 | 18 | /******************************************************************************/ 19 | 20 | // loads the sample database from file (which is a CSV text file) 21 | 22 | int read_data_from_csv(const char* filename, CvMat* data, CvMat* classes, 23 | int n_samples ) 24 | { 25 | 26 | int classlabel; // the class label 27 | 28 | // if we can't read the input file then return 0 29 | FILE* f = fopen( filename, "r" ); 30 | if( !f ) 31 | { 32 | printf("ERROR: cannot read file %s\n", filename); 33 | return 0; // all not OK 34 | } 35 | 36 | // for each sample in the file 37 | 38 | for(int line = 0; line < n_samples; line++) 39 | { 40 | // for each attribute on the line in the file 41 | 42 | for(int attribute = 0; attribute < (attributes_per_sample + 1); attribute++) 43 | { 44 | if (attribute < attributes_per_sample) 45 | { 46 | // first 256 elements (0-255) in each line are the attributes 47 | fscanf(f, "%f,", &(CV_MAT_ELEM(*data, float, line, attribute))); 48 | // printf("%f,", CV_MAT_ELEM(*data, float, line, attribute)); 49 | } 50 | else if (attribute == attributes_per_sample) 51 | { 52 | // attribute 256 is the class label {0 ... 9} 53 | fscanf(f, "%i,", &classlabel); 54 | CV_MAT_ELEM(*classes, float, line, classlabel) = 1.0; 55 | // printf("%f\n", CV_MAT_ELEM(*classes, float, line, classlabel)); 56 | } 57 | } 58 | } 59 | 60 | fclose(f); 61 | 62 | return 1; // all OK 63 | } 64 | 65 | /******************************************************************************/ 66 | 67 | int main( int argc, char** argv ) 68 | { 69 | int number_of_training_samples = 797; 70 | int attributes_per_sample = 256; 71 | int number_of_testing_samples = 796; 72 | 73 | int number_of_classes = 10; 74 | 75 | // define training data storage matrices (one for attribute examples, one 76 | // for classifications) 77 | 78 | CvMat* training_data = cvCreateMat(number_of_training_samples, attributes_per_sample, CV_32FC1); 79 | 80 | CvMat* training_classifications = cvCreateMat(number_of_training_samples, number_of_classes, CV_32FC1); 81 | 82 | cvZero(training_classifications); 83 | 84 | // define testing data storage matrices 85 | CvMat* testing_data = cvCreateMat(number_of_testing_samples, attributes_per_sample, CV_32FC1); 86 | 87 | CvMat* testing_classifications = cvCreateMat(number_of_testing_samples, number_of_classes, CV_32FC1); 88 | 89 | cvZero(testing_classifications); 90 | 91 | // define classification output vector 92 | 93 | CvMat* classificationResult = cvCreateMat(1, number_of_classes, CV_32FC1); 94 | CvPoint max_loc = {0,0}; 95 | 96 | // load training and testing data sets 97 | 98 | if ( read_data_from_csv(argv[1], training_data, training_classifications, number_of_training_samples) && 99 | read_data_from_csv(argv[2], testing_data, testing_classifications, number_of_testing_samples)) 100 | { 101 | // define the parameters for the neural network (MLP) 102 | 103 | // set the network to be 3 layer 256->10->10 104 | // - one input node per attribute in a sample 105 | // - 10 hidden nodes 106 | // - one output node per class 107 | 108 | // note that the OpenCV neural network (MLP) implementation does not 109 | // support categorical variables explicitly. 110 | // So, instead of the output class label, we will use 111 | // a binary vector of {0,0 ... 1,0,0} components (one element by class) 112 | // for training and therefore, MLP will give us a vector of "probabilities" 113 | // at the prediction stage - the highest probability can be accepted 114 | // as the "winning" class label output by the network 115 | 116 | int layers_d[] = { attributes_per_sample, 10, number_of_classes}; 117 | CvMat* layers = cvCreateMatHeader(1,3,CV_32SC1); 118 | cvInitMatHeader(layers, 1,3,CV_32SC1, layers_d); 119 | 120 | // create the network using a sigmoid function with alpha and beta 121 | // parameters 0.6 and 1 specified respectively (refer to manual) 122 | 123 | CvANN_MLP* nnetwork = new CvANN_MLP; 124 | nnetwork->create(layers, CvANN_MLP::SIGMOID_SYM, 0.6, 1); 125 | 126 | // set the training parameters 127 | 128 | CvANN_MLP_TrainParams params = CvANN_MLP_TrainParams( 129 | // terminate the training after either 1000 130 | // iterations or a very small change in the 131 | // network wieghts below the specified value 132 | cvTermCriteria(CV_TERMCRIT_ITER+CV_TERMCRIT_EPS, 1000, 0.000001), 133 | 134 | // use backpropogation for training 135 | CvANN_MLP_TrainParams::BACKPROP, 136 | 137 | // co-efficents for backpropogation training 138 | // (refer to manual) 139 | 0.1, 140 | 0.1); 141 | 142 | // train the neural network (using training data) 143 | 144 | printf( "\nUsing training database: %s\n", argv[1]); 145 | 146 | int iterations = nnetwork->train(training_data, training_classifications, NULL, NULL, params); 147 | 148 | printf( "Training iterations: %i\n\n", iterations); 149 | 150 | // perform classifier testing and report results 151 | 152 | CvMat test_sample; 153 | int correct_class = 0; 154 | int wrong_class = 0; 155 | int false_positives [number_of_classes] = {0,0,0,0,0,0,0,0,0,0}; 156 | 157 | printf( "\nUsing testing database: %s\n\n", argv[2]); 158 | 159 | for (int tsample = 0; tsample < number_of_testing_samples; tsample++) 160 | { 161 | // extract a row from the testing matrix 162 | 163 | cvGetRow(testing_data, &test_sample, tsample ); 164 | 165 | // run neural network prediction 166 | 167 | nnetwork->predict(&test_sample, classificationResult); 168 | 169 | // The NN gives out a vector of probabilities for each class 170 | // We take the class with the highest "probability" 171 | // for simplicity (but we really should also check separation 172 | // of the different "probabilities" in this vector - what if 173 | // two classes have very similar values ?) 174 | 175 | cvMinMaxLoc(classificationResult, 0, 0, 0, &max_loc, 0 ); 176 | 177 | printf("Testing Sample %i -> class result (digit %d)\n", tsample, max_loc.x); 178 | 179 | // if the corresponding location in the testing classifications 180 | // is not "1" (i.e. this is the correct class) then record this 181 | 182 | if (!(CV_MAT_ELEM(*testing_classifications, float, tsample, max_loc.x))) 183 | { 184 | // if they differ more than floating point error => wrong class 185 | wrong_class++; 186 | 187 | false_positives[(int) max_loc.x]++; 188 | } 189 | else 190 | { 191 | // otherwise correct 192 | 193 | correct_class++; 194 | } 195 | } 196 | 197 | printf( "\nResults on the testing database: %s\n" 198 | "\tCorrect classification: %d (%g%%)\n" 199 | "\tWrong classifications: %d (%g%%)\n", 200 | argv[2], 201 | correct_class, (double) correct_class*100/number_of_testing_samples, 202 | wrong_class, (double) wrong_class*100/number_of_testing_samples); 203 | 204 | for (int i = 0; i < number_of_classes; i++) 205 | { 206 | printf( "\tClass (digit %d) false postives %d (%g%%)\n", i, 207 | false_positives[i], 208 | (double) false_positives[i]*100/number_of_testing_samples); 209 | } 210 | 211 | 212 | // free all memory 213 | 214 | cvReleaseMat( &training_data ); 215 | cvReleaseMat( &training_classifications ); 216 | cvReleaseMat( &testing_data ); 217 | cvReleaseMat( &testing_classifications ); 218 | cvReleaseMat( &classificationResult); 219 | 220 | // all OK : main returns 0 221 | 222 | return 0; 223 | } 224 | 225 | // not OK : main returns -1 226 | 227 | return -1; 228 | } 229 | /******************************************************************************/ 230 | 231 | -------------------------------------------------------------------------------- /cvMat/BinaryThreshold/BinaryThreshold.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1] ); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | cv::Mat image_gray, dst; 21 | 22 | cv::cvtColor( image, image_gray, CV_RGB2GRAY ); 23 | int threshold_value=100; 24 | int max_BINARY_value=200; 25 | 26 | cv::threshold( image_gray, dst, threshold_value, max_BINARY_value,0 ); 27 | 28 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 29 | cv::imshow( argv[1], dst ); 30 | 31 | cv::imwrite( "binaryThreshold.png", dst); 32 | 33 | cv::waitKey(0); 34 | 35 | return EXIT_SUCCESS; 36 | } -------------------------------------------------------------------------------- /cvMat/BinaryThreshold/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( BinaryThreshold ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( BinaryThreshold BinaryThreshold.cpp ) 10 | target_link_libraries( BinaryThreshold ${OpenCV_LIBS} ) -------------------------------------------------------------------------------- /cvMat/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory( KMeansOnRGBImage ) 2 | add_subdirectory( ExtractRGBComponents ) 3 | add_subdirectory( BinaryThreshold ) 4 | add_subdirectory( DisplayAnImage ) 5 | add_subdirectory( GetImageSize ) 6 | add_subdirectory( DisplayHistogram ) 7 | add_subdirectory( DisplayRGBHistogram ) 8 | add_subdirectory( ConvertRGBImageToHSVImages ) 9 | add_subdirectory( ConvertRGBImageToYCbCrImages ) 10 | add_subdirectory( DrawCircle ) 11 | add_subdirectory( HoughCircles ) 12 | add_subdirectory( HoughLines ) 13 | add_subdirectory( GoodFeaturesToTrack ) 14 | add_subdirectory( MSERFeatureDetection ) 15 | add_subdirectory( STARFeatureDetection ) 16 | add_subdirectory( SURFFeatureDetection ) 17 | add_subdirectory( ConvertIplToMat ) 18 | add_subdirectory( YCrCbSkinDetection ) 19 | add_subdirectory( Statistics ) 20 | add_subdirectory( CannyEdgeDetector ) 21 | add_subdirectory( RotateAnImage ) 22 | 23 | -------------------------------------------------------------------------------- /cvMat/CannyEdgeDetector/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( CannyEdgeDetector ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( CannyEdgeDetector CannyEdgeDetector.cpp ) 10 | target_link_libraries( CannyEdgeDetector ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/CannyEdgeDetector/CannyEdgeDetector.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/imgproc/imgproc.hpp" 2 | #include "opencv2/highgui/highgui.hpp" 3 | #include 4 | 5 | int main( int argc, char** argv ) 6 | { 7 | if( argc != 6 ) 8 | { 9 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 10 | return EXIT_FAILURE; 11 | } 12 | 13 | cv::Mat src = cv::imread( argv[1], 0 ); 14 | 15 | if( !src.data ) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | int lowThreshold = atoi( argv[3] ); 21 | int highThreshold = atoi( argv[4] ); 22 | int kernelSize = atoi( argv[5] ); 23 | 24 | cv::Mat output; 25 | 26 | cv::blur( src, output, cv::Size( kernelSize, kernelSize ) ); 27 | cv::Canny( output, output, lowThreshold, highThreshold, kernelSize ); 28 | 29 | cv::namedWindow( "canny", CV_WINDOW_AUTOSIZE ); 30 | cv::imshow( "canny", output ); 31 | 32 | cv::imwrite( argv[2], output ); 33 | 34 | cv::waitKey(0); 35 | 36 | return EXIT_SUCCESS; 37 | } 38 | -------------------------------------------------------------------------------- /cvMat/ConvertIplToMat/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( ConvertIplToMat ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( ConvertIplToMat ConvertIplToMat.cpp ) 10 | target_link_libraries( ConvertIplToMat ${OpenCV_LIBS} ) -------------------------------------------------------------------------------- /cvMat/ConvertIplToMat/ConvertIplToMat.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | IplImage* ipl_img = cvLoadImage( argv[1] ); 15 | if( !ipl_img ) 16 | { 17 | std::cerr << "Could not load image file: " << argv[1] << std::endl; 18 | return EXIT_FAILURE; 19 | } 20 | 21 | cv::Mat imgmat(ipl_img,false); 22 | 23 | return 0; 24 | } -------------------------------------------------------------------------------- /cvMat/ConvertRGBImageToHSVImages/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( ConvertRGBImageToHSVImages ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( ConvertRGBImageToHSVImages ConvertRGBImageToHSVImages.cpp ) 10 | target_link_libraries( ConvertRGBImageToHSVImages ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/ConvertRGBImageToHSVImages/ConvertRGBImageToHSVImages.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cv.h" 5 | #include "highgui.h" 6 | 7 | int main( int argc, char* argv[] ) 8 | { 9 | if( argc != 2 ) 10 | { 11 | std::cerr << "Usage: " << argv[0] << "" << std::endl; 12 | return EXIT_FAILURE; 13 | } 14 | 15 | cv::Mat image = cv::imread( argv[1],1); 16 | if(!image.data) 17 | { 18 | return EXIT_FAILURE; 19 | } 20 | 21 | cv::Mat hsv; 22 | 23 | cv::cvtColor( image, hsv, CV_BGR2HSV ); 24 | 25 | std::vector hsv_planes; 26 | cv::split( image, hsv_planes ); 27 | 28 | cv::imwrite( "hue.png", hsv_planes[0]); 29 | cv::imwrite( "saturation.png", hsv_planes[1]); 30 | cv::imwrite( "value.png", hsv_planes[2]); 31 | 32 | return EXIT_SUCCESS; 33 | } 34 | -------------------------------------------------------------------------------- /cvMat/ConvertRGBImageToYCbCrImages/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( ConvertRGBImageToYCbCrImages ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( ConvertRGBImageToYCbCrImages ConvertRGBImageToYCbCrImages.cpp ) 10 | target_link_libraries( ConvertRGBImageToYCbCrImages ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/ConvertRGBImageToYCbCrImages/ConvertRGBImageToYCbCrImages.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cv.h" 5 | #include "highgui.h" 6 | 7 | int main( int argc, char* argv[] ) 8 | { 9 | if( argc != 2 ) 10 | { 11 | std::cerr << "Usage: " << argv[0] << "" << std::endl; 12 | return EXIT_FAILURE; 13 | } 14 | 15 | cv::Mat image = cv::imread( argv[1],1); 16 | if(!image.data) 17 | { 18 | return EXIT_FAILURE; 19 | } 20 | 21 | cv::Mat YCrCb; 22 | 23 | cv::cvtColor( image, YCrCb, CV_BGR2YCrCb ); 24 | 25 | std::vector YCrCb_planes; 26 | cv::split( YCrCb, YCrCb_planes ); 27 | 28 | cv::imwrite( "Y.png", YCrCb_planes[0]); 29 | cv::imwrite( "Cr.png", YCrCb_planes[1]); 30 | cv::imwrite( "Cb.png", YCrCb_planes[2]); 31 | 32 | return EXIT_SUCCESS; 33 | } 34 | -------------------------------------------------------------------------------- /cvMat/DisplayAnImage/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( DisplayAnImage ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( DisplayAnImage DisplayAnImage.cpp ) 10 | target_link_libraries( DisplayAnImage ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/DisplayAnImage/DisplayAnImage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1] ); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 21 | cv::imshow( argv[1], image ); 22 | 23 | cv::waitKey(0); 24 | 25 | return EXIT_SUCCESS; 26 | } 27 | -------------------------------------------------------------------------------- /cvMat/DisplayHistogram/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( DisplayHistogram ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( DisplayHistogram DisplayHistogram.cpp ) 10 | target_link_libraries( DisplayHistogram ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/DisplayHistogram/DisplayHistogram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | //image loaded as a grayscale image 15 | cv::Mat image = cv::imread( argv[1],0 ); 16 | if(!image.data) 17 | { 18 | return EXIT_FAILURE; 19 | } 20 | 21 | 22 | int histSize = 256; 23 | float range[] = { 0, 256 } ; 24 | const float* histRange = { range }; 25 | bool uniform = true; 26 | bool accumulate = false; 27 | cv::Mat hist; 28 | 29 | cv::calcHist( &image, 1, 0, cv::Mat(), hist, 1, &histSize, &histRange, uniform, accumulate ); 30 | 31 | //Draw the histogram 32 | int hist_w = 512; int hist_h = 400; 33 | int bin_w = cvRound( (double) hist_w/histSize ); 34 | cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); 35 | 36 | normalize(hist, hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); 37 | 38 | for( int i = 1; i < histSize; i++ ) 39 | { 40 | cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(hist.at(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(hist.at(i)) ), cv::Scalar( 255, 255, 255), 2, 8, 0 ); 41 | } 42 | 43 | //Display histogram 44 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 45 | cv::imshow( argv[1], histImage ); 46 | 47 | cv::waitKey(0); 48 | 49 | return EXIT_SUCCESS; 50 | } 51 | -------------------------------------------------------------------------------- /cvMat/DisplayRGBHistogram/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( DisplayRGBHistogram ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( DisplayRGBHistogram DisplayRGBHistogram.cpp ) 10 | target_link_libraries( DisplayRGBHistogram ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/DisplayRGBHistogram/DisplayRGBHistogram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | 15 | cv::Mat image = cv::imread( argv[1],1); 16 | if(!image.data) 17 | { 18 | return EXIT_FAILURE; 19 | } 20 | 21 | std::vector bgr_planes; 22 | cv::split( image, bgr_planes ); 23 | 24 | int histSize = 256; 25 | float range[] = { 0, 256 } ; 26 | const float* histRange = { range }; 27 | bool uniform = true; 28 | bool accumulate = false; 29 | cv::Mat b_hist, g_hist, r_hist; 30 | 31 | cv::calcHist( &bgr_planes[0], 1, 0, cv::Mat(), b_hist, 1, &histSize, &histRange, uniform, accumulate ); 32 | cv::calcHist( &bgr_planes[1], 1, 0, cv::Mat(), g_hist, 1, &histSize, &histRange, uniform, accumulate ); 33 | cv::calcHist( &bgr_planes[2], 1, 0, cv::Mat(), r_hist, 1, &histSize, &histRange, uniform, accumulate ); 34 | 35 | //Draw the histograms 36 | int hist_w = 512; int hist_h = 400; 37 | int bin_w = cvRound( (double) hist_w/histSize ); 38 | cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); 39 | 40 | normalize(b_hist, b_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); 41 | normalize(g_hist, g_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); 42 | normalize(r_hist, r_hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); 43 | 44 | for( int i = 1; i < histSize; i++ ) 45 | { 46 | cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(b_hist.at(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(b_hist.at(i)) ), cv::Scalar( 255, 0, 0), 2, 8, 0 ); 47 | cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(g_hist.at(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(g_hist.at(i)) ), cv::Scalar( 0, 255, 0), 2, 8, 0 ); 48 | cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(r_hist.at(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(r_hist.at(i)) ), cv::Scalar( 0, 0, 255), 2, 8, 0 ); 49 | } 50 | 51 | //Display histogram 52 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 53 | cv::imshow( argv[1], histImage ); 54 | 55 | cv::waitKey(0); 56 | 57 | return EXIT_SUCCESS; 58 | } 59 | -------------------------------------------------------------------------------- /cvMat/DrawCircle/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( DrawCircle ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( DrawCircle DrawCircle.cpp ) 10 | target_link_libraries( DrawCircle ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/DrawCircle/DrawCircle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 3 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1],1); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | 21 | cv::Point center; 22 | center.x = image.rows/ 2; 23 | center.y = image.cols / 2; 24 | int radius = 10; 25 | 26 | cv::Scalar color; 27 | color.val[0] = 255.; 28 | 29 | int thickness = atoi( argv[2] ); // if thickness < 0, the circle is filled 30 | 31 | cv::circle( image, center, radius, color, thickness ); 32 | 33 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 34 | cv::imshow( argv[1], image ); 35 | cv::waitKey(0); 36 | 37 | return EXIT_SUCCESS; 38 | } 39 | -------------------------------------------------------------------------------- /cvMat/ExtractRGBComponents/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( ExtractRGBComponents ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( ExtractRGBComponents ExtractRGBComponents.cpp ) 10 | target_link_libraries( ExtractRGBComponents ${OpenCV_LIBS} ) -------------------------------------------------------------------------------- /cvMat/ExtractRGBComponents/ExtractRGBComponents.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat src_img = cv::imread( argv[1] ); 15 | if(!src_img.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | std::vector three_channels; 21 | cv::split(src_img,three_channels ); 22 | 23 | cv::imwrite( "red.png", three_channels[2] ); 24 | cv::imwrite( "green.png", three_channels[1] ); 25 | cv::imwrite( "blue.png", three_channels[0] ); 26 | 27 | 28 | return EXIT_SUCCESS; 29 | } -------------------------------------------------------------------------------- /cvMat/GetImageSize/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( GetImageSize ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( GetImageSize GetImageSize.cpp ) 10 | target_link_libraries( GetImageSize ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/GetImageSize/GetImageSize.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "cv.h" 5 | #include "highgui.h" 6 | 7 | int main( int argc, char* argv[] ) 8 | { 9 | if( argc != 2 ) 10 | { 11 | std::cerr << "Usage: " << argv[0] << "" << std::endl; 12 | return EXIT_FAILURE; 13 | } 14 | 15 | cv::Mat image = cv::imread( argv[1] ); 16 | if(!image.data) 17 | { 18 | return EXIT_FAILURE; 19 | } 20 | 21 | std::cout << image.rows << " x " << image.cols << std::endl; 22 | 23 | 24 | return EXIT_SUCCESS; 25 | } 26 | -------------------------------------------------------------------------------- /cvMat/GoodFeaturesToTrack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( GoodFeaturesToTrack ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( GoodFeaturesToTrack GoodFeaturesToTrack.cpp ) 10 | target_link_libraries( GoodFeaturesToTrack ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/GoodFeaturesToTrack/GoodFeaturesToTrack.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1],0 ); 15 | 16 | std::vector< cv::Point2f > corners; 17 | 18 | // maxCorners – The maximum number of corners to return. If there are more corners 19 | // than that will be found, the strongest of them will be returned 20 | int maxCorners = 10; 21 | 22 | // qualityLevel – Characterizes the minimal accepted quality of image corners; 23 | // the value of the parameter is multiplied by the by the best corner quality 24 | // measure (which is the min eigenvalue, see cornerMinEigenVal() , 25 | // or the Harris function response, see cornerHarris() ). 26 | // The corners, which quality measure is less than the product, will be rejected. 27 | // For example, if the best corner has the quality measure = 1500, 28 | // and the qualityLevel=0.01 , then all the corners which quality measure is 29 | // less than 15 will be rejected. 30 | double qualityLevel = 0.01; 31 | 32 | // minDistance – The minimum possible Euclidean distance between the returned corners 33 | double minDistance = 20.; 34 | 35 | // mask – The optional region of interest. If the image is not empty (then it 36 | // needs to have the type CV_8UC1 and the same size as image ), it will specify 37 | // the region in which the corners are detected 38 | cv::Mat mask; 39 | 40 | // blockSize – Size of the averaging block for computing derivative covariation 41 | // matrix over each pixel neighborhood, see cornerEigenValsAndVecs() 42 | int blockSize = 3; 43 | 44 | // useHarrisDetector – Indicates, whether to use operator or cornerMinEigenVal() 45 | bool useHarrisDetector = false; 46 | 47 | // k – Free parameter of Harris detector 48 | double k = 0.04; 49 | 50 | cv::goodFeaturesToTrack( image, corners, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k ); 51 | 52 | for( size_t i = 0; i < corners.size(); i++ ) 53 | { 54 | cv::circle( image, corners[i], 10, cv::Scalar( 255. ), -1 ); 55 | } 56 | 57 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 58 | cv::imshow( argv[1], image ); 59 | cv::waitKey(0); 60 | 61 | return EXIT_SUCCESS; 62 | } 63 | -------------------------------------------------------------------------------- /cvMat/HoughCircles/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( HoughCircles) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( HoughCircles HoughCircles.cpp ) 10 | target_link_libraries( HoughCircles ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/HoughCircles/HoughCircles.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1],0); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | std::vector< cv::Vec3f > circles; 21 | int method = CV_HOUGH_GRADIENT; 22 | 23 | // The inverse ratio of the accumulator resolution to the image resolution. 24 | // For example, if dp=1 , the accumulator will have the same resolution as the 25 | // input image, if dp=2 - accumulator will have half as big width and height, etc 26 | double dp = 1.; 27 | 28 | // Minimum distance between the centers of the detected circles. If the parameter 29 | // is too small, multiple neighbor circles may be falsely detected in addition 30 | // to a true one. If it is too large, some circles may be missed 31 | double minDist = 50; 32 | 33 | // The first method-specific parameter. in the case of CV_HOUGH_GRADIENT it is 34 | // the higher threshold of the two passed to Canny() edge detector (the lower 35 | // one will be twice smaller) 36 | double param1=100.; 37 | 38 | // The second method-specific parameter. in the case of CV_HOUGH_GRADIENT it is 39 | // the accumulator threshold at the center detection stage. The smaller it is, 40 | // the more false circles may be detected. Circles, corresponding to the larger 41 | // accumulator values, will be returned first 42 | double param2=20.; 43 | 44 | // Minimum circle radius 45 | int minRadius = 5; 46 | 47 | // Maximum circle radius 48 | int maxRadius = 100; 49 | cv::HoughCircles( image, circles, method, dp, minDist, param1, param2, minRadius, maxRadius ); 50 | 51 | cv::Scalar color; 52 | color.val[0] = 255.; 53 | 54 | int thickness = 3; 55 | 56 | size_t i = 1; 57 | 58 | for( std::vector< cv::Vec3f >::const_iterator it = circles.begin(); it != circles.end(); ++it, ++i ) 59 | { 60 | /* std::cout << "feature point " << i << std::endl; 61 | std::cout << "position: [ " << ( *it )[0] << ", " << ( *it )[1] << "]" << std::endl; 62 | std::cout << "radius: " << ( *it )[2] << std::endl; 63 | std::cout << std::endl;*/ 64 | 65 | cv::circle( image, cv::Point( ( *it )[0], ( *it )[1] ), (*it)[2], color, thickness ); 66 | } 67 | 68 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 69 | cv::imshow( argv[1], image ); 70 | cv::waitKey(0); 71 | 72 | return EXIT_SUCCESS; 73 | } 74 | -------------------------------------------------------------------------------- /cvMat/HoughLines/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( HoughLines ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( HoughLines HoughLines.cpp ) 10 | target_link_libraries( HoughLines ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/HoughLines/HoughLines.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1],0 ); 15 | 16 | std::vector< cv::Vec2f > lines; 17 | 18 | // rho – Distance resolution of the accumulator in pixels 19 | double rho = 1.; 20 | 21 | // theta – Angle resolution of the accumulator in radians 22 | double theta = CV_PI / 180.; 23 | 24 | // threshold – The accumulator threshold parameter. Only those lines are returned that get enough votes ( >\texttt{threshold} ) 25 | double threshold = 100.; 26 | 27 | // srn – For the multi-scale Hough transform it is the divisor for the distance 28 | // resolution rho .The coarse accumulator distance resolution will be rho and 29 | // the accurate accumulator resolution will be rho/srn . If both srn=0 and stn=0 30 | // then the classical Hough transform is used, otherwise both these parameters 31 | // should be positive. 32 | double srn = 0.; 33 | 34 | // stn – For the multi-scale Hough transform it is the divisor for the distance resolution theta 35 | double stn = 0.; 36 | 37 | cv::HoughLines( image, lines, rho, theta, threshold, srn, stn ); 38 | 39 | for( size_t i = 0; i < lines.size(); i++ ) 40 | { 41 | float rho = lines[i][0]; 42 | float theta = lines[i][1]; 43 | double a = cos(theta), b = sin(theta); 44 | double x0 = a*rho, y0 = b*rho; 45 | cv::Point pt1( cvRound(x0 + 1000*(-b)), cvRound(y0 + 1000*(a))); 46 | cv::Point pt2(cvRound( x0 - 1000*(-b)), cvRound(y0 - 1000*(a))); 47 | cv::line( image, pt1, pt2, cv::Scalar(0,0,255), 3, 8 ); 48 | } 49 | 50 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 51 | cv::imshow( argv[1], image ); 52 | cv::waitKey(0); 53 | 54 | return EXIT_SUCCESS; 55 | } 56 | -------------------------------------------------------------------------------- /cvMat/KMeansOnRGBImage/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( KMeansOnRGBImage ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( KMeansOnRGBImage KMeansOnRGBImage.cpp ) 10 | target_link_libraries( KMeansOnRGBImage ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/KMeansOnRGBImage/KMeansOnRGBImage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | int main(int argc, char **argv) 6 | { 7 | if( argc != 3 ) 8 | { 9 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 10 | return EXIT_FAILURE; 11 | } 12 | 13 | int NumberOfClusters = atoi( argv[2] ); 14 | 15 | // (1)load a specified file as a 3-channel color image 16 | cv::Mat src_img = cv::imread( argv[1] ); 17 | 18 | if(!src_img.data) 19 | { 20 | return EXIT_FAILURE; 21 | } 22 | 23 | // (2)reshape the image to be a 1 column matrix 24 | cv::Mat points; 25 | src_img.convertTo(points, CV_32FC3); 26 | points = points.reshape(3, src_img.rows*src_img.cols); 27 | 28 | // (3)run k-means clustering algorithm to segment pixels in RGB color space 29 | cv::Mat_ clusters(points.size(), CV_32SC1); 30 | cv::Mat centers; 31 | cv::kmeans(points, NumberOfClusters, clusters, 32 | cv::TermCriteria(CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 10, 1.0), 1, cv::KMEANS_PP_CENTERS, centers); 33 | 34 | // (4)make a each centroid represent all pixels in the cluster 35 | cv::Mat dst_img(src_img.size(), src_img.type()); 36 | cv::MatIterator_ itf = centers.begin(); 37 | cv::MatIterator_ itd = dst_img.begin(), itd_end = dst_img.end(); 38 | for(int i=0; itd != itd_end; ++itd, ++i) { 39 | cv::Vec3f color = itf[clusters(1,i)]; 40 | (*itd)[0] = cv::saturate_cast(color[0]); 41 | (*itd)[1] = cv::saturate_cast(color[1]); 42 | (*itd)[2] = cv::saturate_cast(color[2]); 43 | } 44 | 45 | // (5)show source and destination image, and quit when any key pressed 46 | cv::namedWindow("src_img", CV_WINDOW_AUTOSIZE); 47 | imshow("src_img", src_img); 48 | cv::namedWindow("dst_img", CV_WINDOW_AUTOSIZE); 49 | imshow("dst_img", dst_img); 50 | cv::waitKey(0); 51 | 52 | cv::imwrite( "kmeans_output.png", dst_img ); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /cvMat/MSERFeatureDetection/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( MSERFeatureDetection ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( MSERFeatureDetection MSERFeatureDetection.cpp ) 10 | target_link_libraries( MSERFeatureDetection ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/MSERFeatureDetection/MSERFeatureDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1],0); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | std::vector< std::vector< cv::Point > > featurePoints; 21 | 22 | /* 23 | ( int _delta, int _min_area, int _max_area, 24 | float _max_variation, float _min_diversity, 25 | int _max_evolution, double _area_threshold, 26 | double _min_margin, int _edge_blur_size ); 27 | */ 28 | 29 | cv::MSER detector; 30 | 31 | detector( image, featurePoints, cv::Mat() ); 32 | 33 | cv::Scalar color; 34 | color.val[0] = 255.; 35 | 36 | int thickness = -1; 37 | 38 | size_t i = 1; 39 | 40 | for( std::vector< std::vector< cv::Point > >::const_iterator it = featurePoints.begin(); 41 | it != featurePoints.end(); 42 | ++it, ++i ) 43 | { 44 | size_t j = 1; 45 | 46 | for( std::vector< cv::Point >::const_iterator pIt = it->begin(); 47 | pIt != it->end(); 48 | ++pIt, ++j ) 49 | { 50 | std::cout << "feature point " << i <<" * " << j << std::endl; 51 | std::cout << "position: [ " << pIt->x << ", " << pIt->y << "]" << std::endl; 52 | std::cout << std::endl; 53 | 54 | cv::circle( image, *pIt, 10, color, thickness ); 55 | } 56 | } 57 | 58 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 59 | cv::imshow( argv[1], image ); 60 | 61 | cv::waitKey(0); 62 | 63 | return EXIT_SUCCESS; 64 | } 65 | -------------------------------------------------------------------------------- /cvMat/RotateAnImage/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( RotateAnImage ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( RotateAnImage RotateAnImage.cpp ) 10 | target_link_libraries( RotateAnImage ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/RotateAnImage/RotateAnImage.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/highgui/highgui.hpp" 2 | #include "opencv2/imgproc/imgproc.hpp" 3 | 4 | int main( int argc, char* argv[] ) 5 | { 6 | cv::Mat src = cv::imread( argv[1] ); 7 | 8 | if( !src.data ) 9 | { 10 | return EXIT_FAILURE; 11 | } 12 | 13 | int Xcenter = atoi( argv[2] ); 14 | int Ycenter = atoi( argv[3] ); 15 | double angle = atof( argv[4] ); 16 | double scale = 1.; 17 | 18 | cv::Mat rotationMatrix = cv::getRotationMatrix2D( 19 | cv::Point( Xcenter, Ycenter ), 20 | angle, 21 | scale ); 22 | 23 | cv::Mat output; 24 | cv::warpAffine( src, output, rotationMatrix, cv::Size( src.rows, src.cols ) ); 25 | 26 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 27 | cv::imshow( argv[1], output ); 28 | 29 | cv::waitKey(0); 30 | 31 | return EXIT_SUCCESS; 32 | } 33 | -------------------------------------------------------------------------------- /cvMat/STARFeatureDetection/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( STARFeatureDetection) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( STARFeatureDetection STARFeatureDetection.cpp ) 10 | target_link_libraries( STARFeatureDetection ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/STARFeatureDetection/STARFeatureDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 7 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1],0); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | std::vector< cv::KeyPoint > featurePoints; 21 | 22 | // the full constructor initialized all the algorithm parameters: 23 | // maxSize - maximum size of the features. The following 24 | // values of the parameter are supported: 25 | // 4, 6, 8, 11, 12, 16, 22, 23, 32, 45, 46, 64, 90, 128 26 | 27 | // responseThreshold - threshold for the approximated laplacian, 28 | // used to eliminate weak features. The larger it is, 29 | // the less features will be retrieved 30 | 31 | // lineThresholdProjected - another threshold for the laplacian to 32 | // eliminate edges 33 | 34 | // lineThresholdBinarized - another threshold for the feature 35 | // size to eliminate edges. 36 | // The larger the 2 threshold, the more points you get. 37 | 38 | int maxSize = atoi( argv[2] ); 39 | int responseThreshold = atoi( argv[3] ); 40 | int lineThresholdProjected = atoi( argv[4] ); 41 | int lineThresholdBinarized = atoi( argv[5] ); 42 | int suppressNonmaxSize = atoi( argv[6] ); 43 | 44 | cv::StarDetector detector( maxSize, 45 | responseThreshold, 46 | lineThresholdProjected, 47 | lineThresholdBinarized, 48 | suppressNonmaxSize);; 49 | 50 | detector( image, featurePoints ); 51 | 52 | cv::Scalar color; 53 | color.val[0] = 255.; 54 | 55 | int thickness = -1; 56 | 57 | size_t i = 1; 58 | 59 | for( std::vector< cv::KeyPoint >::const_iterator it = featurePoints.begin(); 60 | it != featurePoints.end(); 61 | ++it, ++i ) 62 | { 63 | std::cout << "feature point " << i << std::endl; 64 | std::cout << "position: [ " << it->pt.x << ", " << it->pt.y << "]" << std::endl; 65 | std::cout << "size: " << it->size << std::endl; 66 | std::cout << "angle: " << it->angle << std::endl; 67 | std::cout << "response: " << it->response << std::endl; 68 | std::cout << "octave: " << it->octave << std::endl; 69 | 70 | std::cout << std::endl; 71 | 72 | cv::circle( image, it->pt, 10, color, thickness ); 73 | } 74 | 75 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 76 | cv::imshow( argv[1], image ); 77 | 78 | cv::waitKey(0); 79 | 80 | return EXIT_SUCCESS; 81 | } 82 | -------------------------------------------------------------------------------- /cvMat/SURFFeatureDetection/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( SURFFeatureDetection ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( SURFFeatureDetection SURFFeatureDetection.cpp ) 10 | target_link_libraries( SURFFeatureDetection ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/SURFFeatureDetection/SURFFeatureDetection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 6 ) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | std::cerr << "nOctaves: 4" << std::endl; 12 | std::cerr << "nOctaveLayers: 2" << std::endl; 13 | std::cerr << "extended: 0" << std::endl; 14 | return EXIT_FAILURE; 15 | } 16 | 17 | cv::Mat image = cv::imread( argv[1],0); 18 | if(!image.data) 19 | { 20 | return EXIT_FAILURE; 21 | } 22 | 23 | std::vector< cv::KeyPoint > featurePoints; 24 | 25 | double hessianThreshold = atof( argv[2] ); 26 | int nOctaves = atoi( argv[3] ); // 4 27 | int nOctaveLayers = atoi( argv[4] );// 2; 28 | bool extended = ( atoi( argv[5] ) != 0 );// false; 29 | 30 | cv::SURF detector = cv::SURF(hessianThreshold, nOctaves, nOctaveLayers, extended ); 31 | detector( image, cv::Mat(), featurePoints ); 32 | 33 | std::cout << featurePoints.size() << std::endl; 34 | 35 | cv::Scalar color; 36 | color.val[0] = 255.; 37 | 38 | int thickness = -1; 39 | 40 | size_t i = 1; 41 | 42 | for( std::vector< cv::KeyPoint >::const_iterator it = featurePoints.begin(); 43 | it != featurePoints.end(); 44 | ++it, ++i ) 45 | { 46 | std::cout << "feature point " << i << std::endl; 47 | std::cout << "position: [ " << it->pt.x << ", " << it->pt.y << "]" << std::endl; 48 | std::cout << "size: " << it->size << std::endl; 49 | std::cout << "angle: " << it->angle << std::endl; 50 | std::cout << "response: " << it->response << std::endl; 51 | std::cout << "octave: " << it->octave << std::endl; 52 | 53 | std::cout << std::endl; 54 | 55 | cv::circle( image, it->pt, 10, color, thickness ); 56 | } 57 | 58 | cv::namedWindow( argv[1], CV_WINDOW_NORMAL ); 59 | cv::imshow( argv[1], image ); 60 | cv::waitKey(0); 61 | 62 | return EXIT_SUCCESS; 63 | } 64 | -------------------------------------------------------------------------------- /cvMat/Statistics/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory( Median ) 2 | add_subdirectory( Mean ) 3 | add_subdirectory( Mode ) 4 | add_subdirectory( StandardDeviation ) 5 | add_subdirectory( Variance ) 6 | add_subdirectory( Skewness ) 7 | add_subdirectory( Kurtosis ) 8 | add_subdirectory( Entropy ) 9 | add_subdirectory( Min ) 10 | add_subdirectory( Max ) 11 | -------------------------------------------------------------------------------- /cvMat/Statistics/Entropy/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | project( Entropy ) 2 | 3 | cmake_minimum_required( VERSION 2.8 ) 4 | 5 | find_package( OpenCV REQUIRED ) 6 | 7 | include_directories( ${OpenCV_INCLUDE_DIRS} ) 8 | 9 | add_executable( Entropy Entropy.cpp ) 10 | target_link_libraries( Entropy ${OpenCV_LIBS} ) 11 | -------------------------------------------------------------------------------- /cvMat/Statistics/Entropy/Entropy.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | 7 | cv::Scalar Entropy (cv::Mat image) 8 | { 9 | 10 | std::vector channels; 11 | cv::split( image, channels ); 12 | 13 | int histSize = 256; 14 | float range[] = { 0, 256 } ; 15 | const float* histRange = { range }; 16 | bool uniform = true; 17 | bool accumulate = false; 18 | cv::Mat hist0, hist1, hist2; 19 | 20 | cv::calcHist( &channels[0], 1, 0, cv::Mat(), hist0, 1, &histSize, &histRange, uniform, accumulate ); 21 | cv::calcHist( &channels[1], 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate ); 22 | cv::calcHist( &channels[2], 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate ); 23 | 24 | //frequency 25 | float f0=0, f1=0, f2=0; 26 | for (int i=0;i(i); 29 | f1+=hist1.at(i); 30 | f2+=hist2.at(i); 31 | } 32 | 33 | //entropy 34 | cv::Scalar e; 35 | e.val[0]=0; 36 | e.val[1]=0; 37 | e.val[2]=0; 38 | // e0=0, e1=0, e2=0; 39 | float p0, p1, p2; 40 | 41 | for (int i=0;i(i))/f0; 44 | p1=abs(hist1.at(i))/f1; 45 | p2=abs(hist2.at(i))/f2; 46 | if (p0!=0) 47 | e.val[0]+=-p0*log10(p0); 48 | if (p1!=0) 49 | e.val[1]+=-p1*log10(p1); 50 | if (p2!=0) 51 | e.val[2]+=-p2*log10(p2); 52 | } 53 | 54 | return e; 55 | } 56 | 57 | int main( int argc, char* argv[] ) 58 | { 59 | if( argc != 2) 60 | { 61 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 62 | return EXIT_FAILURE; 63 | } 64 | 65 | cv::Mat image = cv::imread( argv[1] ); 66 | if(!image.data) 67 | { 68 | return EXIT_FAILURE; 69 | } 70 | 71 | cv::Scalar ent; //0:1st channel, 1:2nd channel and 2:3rd channel 72 | 73 | ent=Entropy(image); 74 | 75 | std::cout<<"Entropy: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | cv::Scalar Kurtosis (cv::Mat image) 7 | { 8 | cv::Scalar kurt,mean,stddev; 9 | kurt.val[0]=0; 10 | kurt.val[1]=0; 11 | kurt.val[2]=0; 12 | meanStdDev(image,mean,stddev,cv::Mat()); 13 | int sum0, sum1, sum2; 14 | int N=image.rows*image.cols; 15 | float den0=0,den1=0,den2=0; 16 | 17 | for (int i=0;i(i)[3*j]-mean.val[0]; 22 | sum1=image.ptr(i)[3*j+1]-mean.val[1]; 23 | sum2=image.ptr(i)[3*j+2]-mean.val[2]; 24 | 25 | kurt.val[0]+=sum0*sum0*sum0*sum0; 26 | kurt.val[1]+=sum1*sum1*sum1*sum1; 27 | kurt.val[2]+=sum2*sum2*sum2*sum2; 28 | den0+=sum0*sum0; 29 | den1+=sum1*sum1; 30 | den2+=sum2*sum2; 31 | } 32 | } 33 | 34 | kurt.val[0]= (kurt.val[0]*N*(N+1)*(N-1)/(den0*den0*(N-2)*(N-3)))-(3*(N-1)*(N-1)/((N-2)*(N-3))); 35 | kurt.val[1]= (kurt.val[1]*N/(den1*den1))-3; 36 | kurt.val[2]= (kurt.val[2]*N/(den2*den2))-3; 37 | 38 | return kurt; 39 | } 40 | 41 | 42 | int main( int argc, char* argv[] ) 43 | { 44 | if( argc != 2) 45 | { 46 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 47 | return EXIT_FAILURE; 48 | } 49 | 50 | cv::Mat image = cv::imread( argv[1] ); 51 | if(!image.data) 52 | { 53 | return EXIT_FAILURE; 54 | } 55 | 56 | cv::Scalar kurt; //0:1st channel, 1:2nd channel and 2:3rd channel 57 | 58 | kurt=Kurtosis(image); 59 | std::cout<<"Kurtosis: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | 7 | cv::Scalar Max (cv::Mat image) 8 | { 9 | cv::Scalar max; 10 | max.val[0]=image.ptr(0)[0]; 11 | max.val[1]=image.ptr(0)[1]; 12 | max.val[2]=image.ptr(0)[2]; 13 | 14 | for (int i=0;i(i)[3*j]) 18 | max.val[0]=image.ptr(i)[3*j]; 19 | if(max.val[1](i)[3*j+1]) 20 | max.val[1]=image.ptr(i)[3*j+1]; 21 | if(max.val[2](i)[3*j+2]) 22 | max.val[2]=image.ptr(i)[3*j+2]; 23 | } 24 | return max; 25 | } 26 | 27 | 28 | int main( int argc, char* argv[] ) 29 | { 30 | if( argc != 2) 31 | { 32 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 33 | return EXIT_FAILURE; 34 | } 35 | 36 | cv::Mat image = cv::imread( argv[1] ); 37 | if(!image.data) 38 | { 39 | return EXIT_FAILURE; 40 | } 41 | 42 | cv::Scalar max; //0:1st channel, 1:2nd channel and 2:3rd channel 43 | 44 | max=Max(image); 45 | 46 | std::cout<<"Maximum: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1] ); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | cv::Scalar mean; //0:1st channel, 1:2nd channel and 2:3rd channel 21 | 22 | mean=cv::mean(image); 23 | std::cout<<"Mean: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | cv::Scalar median (cv::Mat image) 7 | { 8 | double m=(image.rows*image.cols)/2; 9 | int bin0=0, bin1=0, bin2=0; 10 | cv::Scalar med; 11 | med.val[0]=-1; 12 | med.val[1]=-1; 13 | med.val[2]=-1; 14 | int histSize = 256; 15 | float range[] = { 0, 256 } ; 16 | const float* histRange = { range }; 17 | bool uniform = true; 18 | bool accumulate = false; 19 | cv::Mat hist0, hist1, hist2; 20 | std::vector channels; 21 | cv::split( image, channels ); 22 | cv::calcHist( &channels[0], 1, 0, cv::Mat(), hist0, 1, &histSize, &histRange, uniform, accumulate ); 23 | cv::calcHist( &channels[1], 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate ); 24 | cv::calcHist( &channels[2], 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate ); 25 | 26 | for (int i=0; i<256 && ( med.val[0]<0 || med.val[1]<0 || med.val[2]<0);i++) 27 | { 28 | bin0=bin0+cvRound(hist0.at(i)); 29 | bin1=bin1+cvRound(hist1.at(i)); 30 | bin2=bin2+cvRound(hist2.at(i)); 31 | if (bin0>m && med.val[0]<0) 32 | med.val[0]=i; 33 | if (bin1>m && med.val[1]<0) 34 | med.val[1]=i; 35 | if (bin2>m && med.val[2]<0) 36 | med.val[2]=i; 37 | } 38 | 39 | return med; 40 | } 41 | 42 | int main( int argc, char* argv[] ) 43 | { 44 | if( argc != 2) 45 | { 46 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 47 | return EXIT_FAILURE; 48 | } 49 | 50 | cv::Mat image = cv::imread( argv[1] ); 51 | if(!image.data) 52 | { 53 | return EXIT_FAILURE; 54 | } 55 | cv::Scalar med; //0:1st channel, 1:2nd channel and 2:3rd channel 56 | med=median(image); 57 | std::cout<<"Median: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | cv::Scalar Min (cv::Mat image) 7 | { 8 | cv::Scalar min; 9 | min.val[0]=image.ptr(0)[0]; 10 | min.val[1]=image.ptr(0)[1]; 11 | min.val[2]=image.ptr(0)[2]; 12 | 13 | for (int i=0;iimage.ptr(i)[3*j]) 17 | min.val[0]=image.ptr(i)[3*j]; 18 | if(min.val[1]>image.ptr(i)[3*j+1]) 19 | min.val[1]=image.ptr(i)[3*j+1]; 20 | if(min.val[2]>image.ptr(i)[3*j+2]) 21 | min.val[2]=image.ptr(i)[3*j+2]; 22 | } 23 | return min; 24 | } 25 | 26 | 27 | int main( int argc, char* argv[] ) 28 | { 29 | if( argc != 2) 30 | { 31 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 32 | return EXIT_FAILURE; 33 | } 34 | 35 | cv::Mat image = cv::imread( argv[1] ); 36 | if(!image.data) 37 | { 38 | return EXIT_FAILURE; 39 | } 40 | 41 | cv::Scalar min; //0:1st channel, 1:2nd channel and 2:3rd channel 42 | 43 | min=Min(image); 44 | 45 | std::cout<<"Minimum: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | cv::Scalar mode (cv::Mat image) 7 | { 8 | double m=(image.rows*image.cols)/2; 9 | int bin0=0, bin1=0, bin2=0; 10 | cv::Scalar mode; 11 | mode.val[0]=-1; 12 | mode.val[1]=-1; 13 | mode.val[2]=-1; 14 | int histSize = 256; 15 | float range[] = { 0, 256 } ; 16 | const float* histRange = { range }; 17 | bool uniform = true; 18 | bool accumulate = false; 19 | cv::Mat hist0, hist1, hist2; 20 | std::vector channels; 21 | cv::split( image, channels ); 22 | cv::calcHist( &channels[0], 1, 0, cv::Mat(), hist0, 1, &histSize, &histRange, uniform, accumulate ); 23 | cv::calcHist( &channels[1], 1, 0, cv::Mat(), hist1, 1, &histSize, &histRange, uniform, accumulate ); 24 | cv::calcHist( &channels[2], 1, 0, cv::Mat(), hist2, 1, &histSize, &histRange, uniform, accumulate ); 25 | 26 | for (int i=0; i<256 ;i++) 27 | { 28 | if (bin0(i))) 29 | { 30 | bin0=cvRound(hist0.at(i)); 31 | mode.val[0]=i; 32 | } 33 | if (bin1(i))) 34 | { 35 | bin1=cvRound(hist1.at(i)); 36 | mode.val[1]=i; 37 | } 38 | if (bin2(i))) 39 | { 40 | bin2=cvRound(hist2.at(i)); 41 | mode.val[2]=i; 42 | } 43 | } 44 | 45 | return mode; 46 | } 47 | 48 | 49 | int main( int argc, char* argv[] ) 50 | { 51 | if( argc != 2) 52 | { 53 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 54 | return EXIT_FAILURE; 55 | } 56 | 57 | cv::Mat image = cv::imread( argv[1] ); 58 | if(!image.data) 59 | { 60 | return EXIT_FAILURE; 61 | } 62 | 63 | cv::Scalar mod; //0:1st channel, 1:2nd channel and 2:3rd channel 64 | 65 | mod=mode(image); 66 | 67 | std::cout<<"Mode: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | cv::Scalar Skewness (cv::Mat image) 7 | { 8 | cv::Scalar skewness,mean,stddev; 9 | skewness.val[0]=0; 10 | skewness.val[1]=0; 11 | skewness.val[2]=0; 12 | meanStdDev(image,mean,stddev,cv::Mat()); 13 | int sum0, sum1, sum2; 14 | float den0=0,den1=0,den2=0; 15 | int N=image.rows*image.cols; 16 | 17 | for (int i=0;i(i)[3*j]-mean.val[0]; 22 | sum1=image.ptr(i)[3*j+1]-mean.val[1]; 23 | sum2=image.ptr(i)[3*j+2]-mean.val[2]; 24 | 25 | skewness.val[0]+=sum0*sum0*sum0; 26 | skewness.val[1]+=sum1*sum1*sum1; 27 | skewness.val[2]+=sum2*sum2*sum2; 28 | den0+=sum0*sum0; 29 | den1+=sum1*sum1; 30 | den2+=sum2*sum2; 31 | } 32 | } 33 | 34 | skewness.val[0]=skewness.val[0]*sqrt(N)/(den0*sqrt(den0)); 35 | skewness.val[1]=skewness.val[1]*sqrt(N)/(den1*sqrt(den1)); 36 | skewness.val[2]=skewness.val[2]*sqrt(N)/(den2*sqrt(den2)); 37 | 38 | return skewness; 39 | } 40 | 41 | 42 | int main( int argc, char* argv[] ) 43 | { 44 | if( argc != 2) 45 | { 46 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 47 | return EXIT_FAILURE; 48 | } 49 | 50 | cv::Mat image = cv::imread( argv[1] ); 51 | if(!image.data) 52 | { 53 | return EXIT_FAILURE; 54 | } 55 | 56 | cv::Scalar skew; //0:1st channel, 1:2nd channel and 2:3rd channel 57 | 58 | skew=Skewness(image); 59 | 60 | std::cout<<"Skewness: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1] ); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | cv::Scalar mean,stddev; //0:1st channel, 1:2nd channel and 2:3rd channel 21 | 22 | meanStdDev(image,mean,stddev,cv::Mat()); 23 | std::cout<<"Standard deviation: "< 2 | 3 | #include "cv.h" 4 | #include "highgui.h" 5 | 6 | int main( int argc, char* argv[] ) 7 | { 8 | if( argc != 2) 9 | { 10 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 11 | return EXIT_FAILURE; 12 | } 13 | 14 | cv::Mat image = cv::imread( argv[1] ); 15 | if(!image.data) 16 | { 17 | return EXIT_FAILURE; 18 | } 19 | 20 | cv::Scalar mean,stddev; //0:1st channel, 1:2nd channel and 2:3rd channel 21 | 22 | meanStdDev(image,mean,stddev,cv::Mat()); 23 | std::cout<<"Variance: "< 2 | #include 3 | #include 4 | #include "cv.h" 5 | #include "highgui.h" 6 | 7 | cv::Mat histogram(cv::Mat image, int threshMin, int threshMax,cv::Mat mask, int flag) 8 | { 9 | int rangeMax; 10 | if(flag==0) 11 | rangeMax=240; 12 | else 13 | rangeMax=235; 14 | int histSize = rangeMax-16; 15 | float range[] = { 16, rangeMax } ; 16 | const float* histRange = { range }; 17 | bool uniform = true; 18 | bool accumulate = false; 19 | cv::Mat hist; 20 | 21 | cv::calcHist( &image, 1, 0, mask, hist, 1, &histSize, &histRange, uniform, accumulate ); 22 | 23 | //Draw the histogram 24 | int hist_w = 512; int hist_h = 400; 25 | int bin_w = cvRound( (double) hist_w/histSize ); 26 | cv::Mat histImage( hist_h, hist_w, CV_8UC3, cv::Scalar( 0,0,0) ); 27 | 28 | normalize(hist, hist, 0, histImage.rows, cv::NORM_MINMAX, -1, cv::Mat() ); 29 | 30 | for( int i = 1; i < histSize; i++ ) 31 | { 32 | cv::line( histImage, cv::Point( bin_w*(i-1), hist_h - cvRound(hist.at(i-1)) ) , cv::Point( bin_w*(i), hist_h - cvRound(hist.at(i)) ), cv::Scalar( 255, 255, 255), 2, 8, 0 ); 33 | } 34 | cv::line( histImage, cv::Point( bin_w*threshMin, hist_h ) , cv::Point( bin_w*threshMin, 0 ), cv::Scalar( 0, 0, 255), 2, 5, 0 ); 35 | cv::line( histImage, cv::Point( bin_w*threshMax, hist_h ) , cv::Point( bin_w*threshMax, 0 ), cv::Scalar( 0, 255, 0), 2, 5, 0 ); 36 | 37 | return histImage; 38 | } 39 | 40 | int main( int argc, char* argv[] ) 41 | { 42 | if( argc < 3 ) 43 | { 44 | std::cerr << "Usage: " << argv[0] << " " << std::endl; 45 | std::cerr << "Options: " << std::endl; 46 | std::cerr << " " << std::endl; 47 | return EXIT_FAILURE; 48 | } 49 | 50 | cv::Mat image = cv::imread( argv[1] ); 51 | if(!image.data) 52 | { 53 | return EXIT_FAILURE; 54 | } 55 | 56 | cv::Mat YCrCb; 57 | cv::Mat dst=image; 58 | cv::Mat mask = cv::Mat::zeros(image.rows,image.cols, CV_8UC1); 59 | 60 | cv::cvtColor( image, YCrCb, CV_BGR2YCrCb ); 61 | 62 | //Default values 63 | int YMin=-1; 64 | int YMax=256; 65 | int CrMin=133; 66 | int CrMax=173; 67 | int CbMin=77; 68 | int CbMax=127; 69 | if(argc==9) 70 | { 71 | YMin=atoi(argv[3]); 72 | YMax=atoi(argv[4]); 73 | CrMin=atoi(argv[5]); 74 | CrMax=atoi(argv[6]); 75 | CbMin=atoi(argv[7]); 76 | CbMax=atoi(argv[8]); 77 | } 78 | 79 | for (int i=0; i(i)[3*j](i)[3*j]>YMin && YCrCb.ptr(i)[3*j+1](i)[3*j+1]>CrMin && YCrCb.ptr(i)[3*j+2](i)[3*j+2]>CbMin) 84 | { 85 | dst.ptr(i)[3*j]=0; 86 | dst.ptr(i)[3*j+1]=0; 87 | dst.ptr(i)[3*j+2]=0; 88 | mask.at(i,j)=255; 89 | } 90 | } 91 | 92 | std::vector YCrCb_planes; 93 | cv::split( YCrCb, YCrCb_planes ); 94 | 95 | cv::Mat histCr=histogram(YCrCb_planes[1],CrMin,CrMax,cv::Mat(),0); 96 | cv::Mat histCb=histogram(YCrCb_planes[2],CbMin,CbMax,cv::Mat(),0); 97 | //Binary mask histogram 98 | /*cv::Mat histCr=histogram(YCrCb_planes[1],CrMin,CrMax,mask,0); 99 | cv::Mat histCb=histogram(YCrCb_planes[2],CbMin,CbMax,mask,0);*/ 100 | 101 | //Y histogram 102 | /*cv::Mat histY=histogram(YCrCb_planes[0],YMin,YMax,cv::Mat(),1); 103 | cv::namedWindow( "Y histogram", CV_WINDOW_AUTOSIZE ); 104 | cv::imshow( "Y histogram", histY );*/ 105 | 106 | cv::namedWindow( argv[1], CV_WINDOW_AUTOSIZE ); 107 | cv::imshow( argv[1], dst ); 108 | cv::namedWindow( "Cr histogram", CV_WINDOW_AUTOSIZE ); 109 | cv::imshow( "Cr histogram", histCr ); 110 | cv::namedWindow( "Cb histogram", CV_WINDOW_AUTOSIZE ); 111 | cv::imshow( "Cb histogram", histCb ); 112 | //Display mask 113 | /*cv::namedWindow( "mask", CV_WINDOW_AUTOSIZE ); 114 | cv::imshow( "mask", mask );*/ 115 | cv::waitKey(0); 116 | 117 | cv::imwrite(argv[2],dst); 118 | return 0; 119 | } --------------------------------------------------------------------------------