├── .gitignore ├── CMakeLists.txt ├── plain_greyscale.cpp ├── plain_rgb.cpp ├── Makefile ├── Makefile.bak ├── README.md ├── split_mirror.cpp ├── bgr_hsv.cpp ├── tri_color.cpp ├── double_image.cpp ├── mirror_image.cpp ├── video_feed.cpp ├── binary_image.cpp ├── image_open.cpp ├── chessboard.cpp ├── histogram.cpp ├── bgr_grayscale_conv_trackbar.cpp ├── binary_image_trackbars.cpp ├── bgr_greyscale_conversion.cpp ├── linear_filter_lib_func.cpp ├── linear_filter.cpp ├── edge_detection.cpp ├── canny_filter.cpp ├── color_extractor.cpp ├── hough_transform_lib.cpp ├── edge_detection_simple.cpp ├── dilation_erosion.cpp ├── blob_detection.cpp └── who_wins.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *.jpg 2 | *.png 3 | *.jpeg 4 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | find_package( OpenCV REQUIRED ) 3 | project( image ) 4 | add_executable( image image.cpp) 5 | target_link_libraries(image ${OpenCV_LIBS}) 6 | -------------------------------------------------------------------------------- /plain_greyscale.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main(){ 8 | Mat image(200, 400, CV_8UC1, Scalar(45)); 9 | imshow("Display Window", image); 10 | 11 | waitKey(0); 12 | return 0; 13 | } 14 | -------------------------------------------------------------------------------- /plain_rgb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(){ 9 | Mat image(200, 400, CV_8UC3, Scalar(0,255,0)); 10 | imshow("Display Window", image); 11 | 12 | waitKey(0); 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS = -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g 2 | LIBS = -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching 3 | CC = g++ 4 | % : %.cpp 5 | $(CC) $(CFLAGS) -o $@ $< $(LIBS) 6 | -------------------------------------------------------------------------------- /Makefile.bak: -------------------------------------------------------------------------------- 1 | CFLAGS = -I/usr/local/include/opencv -I/usr/local/include/opencv2 -L/usr/local/lib/ -g 2 | LIBS = -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_stitching 3 | CC = g++ 4 | % : %.cpp 5 | $(CC) $(CFLAGS) -o $@ $< $(LIBS) 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | These are the programs that I have written during my Winter Workshop 2 | at IIT Kharagpur of Image Processing. 3 | 4 | ## How to compile a code: 5 | Let's say you want to compile the program `chessboard.cpp`, then all 6 | you have to do is to execute this command:
7 | `make chessboard` 8 | 9 | This will create a binary with the name 'chessboard' which can be easily 10 | run by `./chessboard`. 11 | -------------------------------------------------------------------------------- /split_mirror.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | int main(int argc, char **argv){ 5 | Mat image = imread(argv[1], CV_LOAD_IMAGE_COLOR); 6 | 7 | int i,j; 8 | for(i = 0; i < image.rows; i++) 9 | for(j = 0; j < image.cols; j++) 10 | image.at(i, n - j - 1) = im.at(i, j); 11 | 12 | namedWindow("Split Mirror", WINDOW_AUTOSIZE); 13 | imshow("Split Mirror", image); 14 | waitKey(0); 15 | return 0; 16 | } 17 | -------------------------------------------------------------------------------- /bgr_hsv.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace cv; 6 | using namespace std; 7 | 8 | int main(int argc, char **argv){ 9 | Mat img; 10 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 11 | Mat img_hsv(img.rows, img.cols, CV_8UC3, Scalar(0, 0, 0)); 12 | cvtColor(img, img_hsv, CV_BGR2HSV); 13 | 14 | namedWindow("BGR", WINDOW_AUTOSIZE); 15 | namedWindow("HSV", WINDOW_AUTOSIZE); 16 | imshow("BGR", img); 17 | imshow("HSV", img_hsv); 18 | waitKey(0); 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /tri_color.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main(){ 8 | Mat image(120, 300, CV_8UC3, Scalar(0, 0, 0)); 9 | 10 | int i, j; 11 | for(i = 0; i < image.cols; i++){ 12 | for(j = 0; j < image.rows/3; j++) 13 | image.at(j, i)[2] = 255; 14 | for(; j < image.rows*2/3; j++) 15 | image.at(j, i)[0] = 255; 16 | for(;j < image.rows; j++) 17 | image.at(j, i)[1] = 255; 18 | } 19 | 20 | imshow("Tri Color", image); 21 | waitKey(0); 22 | 23 | return 0; 24 | } 25 | -------------------------------------------------------------------------------- /double_image.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace cv; 5 | using namespace std; 6 | 7 | int main(int argc, char **argv){ 8 | Mat img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 9 | Mat img_d(img.rows*2, img.cols*2, CV_8UC3, Scalar(0, 0, 0)); 10 | 11 | int x, y, i, j; 12 | for(x = 0; x < img.rows; x++) 13 | for(y = 0; y < img.cols; y++) 14 | for(i = 0; i < 2; i++) 15 | for(j = 0; j < 2; j++) 16 | img_d.at(x*2+i, y*2+j) = img.at(x, y); 17 | 18 | namedWindow("Single", WINDOW_AUTOSIZE); 19 | namedWindow("Double", WINDOW_AUTOSIZE); 20 | imshow("Single", img); 21 | imshow("Double", img_d); 22 | 23 | waitKey(0); 24 | return 0; 25 | } 26 | -------------------------------------------------------------------------------- /mirror_image.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(int argc, char **argv){ 9 | Mat straight, mirror; 10 | straight = imread(argv[1], CV_LOAD_IMAGE_COLOR); 11 | mirror = imread(argv[1], CV_LOAD_IMAGE_COLOR); 12 | 13 | namedWindow("Straight", WINDOW_AUTOSIZE); 14 | imshow("Straight", straight); 15 | 16 | int i,j; 17 | for(i = 0; i < mirror.rows; i++){ 18 | for(j = 0; j < mirror.cols; j++){ 19 | mirror.at(i, j) = straight.at(i, mirror.cols-j - 1); 20 | } 21 | } 22 | 23 | namedWindow("Mirror", WINDOW_AUTOSIZE); 24 | imshow("Mirror", mirror); 25 | 26 | waitKey(0); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /video_feed.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace std; 10 | using namespace cv; 11 | 12 | int main(int argc, char **argv){ 13 | VideoCapture cap(argv[1]); 14 | 15 | // Check validity 16 | if(!cap.isOpened()){ 17 | cout << "Sorry there is some problem with the video feed." << endl; 18 | return -1; 19 | } 20 | int i = 0; 21 | namedWindow("Video", WINDOW_AUTOSIZE); 22 | Mat frameReference, frameUnderTest; 23 | while(1){ 24 | cap >> frameReference; 25 | cap.set(CV_CAP_PROP_POS_MSEC, 3000 * i); 26 | imshow("Video", frameReference); 27 | if(waitKey(50) == 'q') 28 | break; 29 | i++; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /binary_image.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(int argc, char **argv){ 9 | Mat img; 10 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 11 | Mat img1(img.rows, img.cols, CV_8UC1, Scalar(0)); 12 | Mat img_binary(img.rows, img.cols, CV_8UC1, Scalar(0)); 13 | cvtColor(img, img1, CV_BGR2GRAY); 14 | 15 | int i, j; 16 | for(i = 0; i < img.rows; i++) 17 | for(j = 0; j < img.cols; j++) 18 | if(img1.at(i, j) > 127) 19 | img_binary.at(i, j) = 255; 20 | 21 | namedWindow("Normal", WINDOW_AUTOSIZE); 22 | imshow("Normal", img); 23 | namedWindow("Binary", WINDOW_AUTOSIZE); 24 | imshow("Binary", img_binary); 25 | 26 | waitKey(0); 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /image_open.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace cv; 6 | using namespace std; 7 | 8 | int main( int argc, char** argv ) 9 | { 10 | if( argc != 2) 11 | { 12 | cout <<" Usage: display_image ImageToLoadAndDisplay" << endl; 13 | return -1; 14 | } 15 | 16 | Mat image; 17 | image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file 18 | 19 | if(! image.data ) // Check for invalid input 20 | { 21 | cout << "Could not open or find the image" << std::endl ; 22 | return -1; 23 | } 24 | 25 | namedWindow( "Display window", WINDOW_AUTOSIZE );// Create a window for display. 26 | imshow( "Display window", image ); // Show our image inside it. 27 | 28 | waitKey(0); // Wait for a keystroke in the window 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /chessboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(){ 9 | Mat board(900, 900, CV_8UC1, Scalar(0)); 10 | int i, j, x, y; 11 | x = y = 0; 12 | 13 | for(x = 0; x < board.rows; x += board.rows/15) 14 | for(y = 0; y < board.cols; y += board.cols/15) 15 | for(i = 0; i < 30; i++) 16 | for(j = 0; j < 30; j++) 17 | board.at(x + i, y + j) = 255; 18 | 19 | for(x = 30; x < board.rows; x += board.rows/15) 20 | for(y = 30; y < board.cols; y += board.cols/15) 21 | for(i = 0; i < 30; i++) 22 | for(j = 0; j < 30; j++) 23 | board.at(x + i, y + j) = 255; 24 | 25 | 26 | 27 | namedWindow("Chess Board", WINDOW_AUTOSIZE); 28 | imshow("Chess Board", board); 29 | waitKey(0); 30 | 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /histogram.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace cv; 8 | 9 | int main(int argc, char **argv){ 10 | Mat img; 11 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 12 | Mat img1(img.rows, img.cols, CV_8UC1, Scalar(0)); 13 | cvtColor(img, img1, CV_BGR2GRAY); 14 | Mat histo(500, 500, CV_8UC1, Scalar(255)); 15 | 16 | long intensity[256]; 17 | int i, j; 18 | 19 | 20 | for(i = 0; i < 256; i++) 21 | intensity[i] = 0; 22 | 23 | for(i = 0; i < img1.rows; i++) 24 | for(j = 0; j < img1.cols; j++) 25 | intensity[img1.at(i, j)]++; 26 | 27 | for(i = 0; i < 256; i++) 28 | intensity[i] /= 100; 29 | 30 | for(i = 0; i < 256; i++) 31 | histo.at(400-intensity[i], i) = 0; 32 | 33 | namedWindow("Histogram", WINDOW_AUTOSIZE); 34 | imshow("Histogram", histo); 35 | 36 | waitKey(0); 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /bgr_grayscale_conv_trackbar.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main(int argc, char **argv){ 8 | Mat img; 9 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 10 | Mat final_img(img.rows, img.cols, CV_8UC3, Scalar(0, 0, 0)); 11 | 12 | namedWindow("Hey", WINDOW_AUTOSIZE); 13 | int a, b, c; 14 | a = b = c = 1; 15 | int i, j; 16 | createTrackbar("blue", "Hey", &a, 100); 17 | createTrackbar("green", "Hey", &b, 100); 18 | createTrackbar("red", "Hey", &c, 100); 19 | while(1){ 20 | for(i = 0; i < img.rows; i++){ 21 | for(j = 0; j < img.cols; j++){ 22 | final_img.at(i, j)[0] = a*img.at(i, j)[0]/100; 23 | final_img.at(i, j)[1] = b*img.at(i, j)[1]/100; 24 | final_img.at(i, j)[2] = c*img.at(i, j)[2]/100; 25 | } 26 | } 27 | imshow("Hey", final_img); 28 | if(waitKey(10) == 'q') 29 | break; 30 | } 31 | 32 | return 0; 33 | } 34 | -------------------------------------------------------------------------------- /binary_image_trackbars.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(int argc, char **argv){ 9 | Mat img; 10 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 11 | Mat img1(img.rows, img.cols, CV_8UC1, Scalar(0)); 12 | cvtColor(img, img1, CV_BGR2GRAY); 13 | Mat final_img(img.rows, img.cols, CV_8UC1, Scalar(0)); 14 | 15 | namedWindow("Binary Image", WINDOW_AUTOSIZE); 16 | int threshold = 127; 17 | createTrackbar("track1", "Binary Image", &threshold, 255); 18 | int i, j; 19 | while(1){ 20 | for(i = 0; i < img1.rows; i++){ 21 | for(j = 0; j < img1.cols; j++){ 22 | if(img1.at(i, j) > threshold) 23 | final_img.at(i, j) = 255; 24 | else 25 | final_img.at(i, j) = 0; 26 | } 27 | } 28 | imshow("Binary Image", final_img); 29 | if(waitKey(10) == 'q') 30 | break; 31 | } 32 | 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /bgr_greyscale_conversion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main(int argc, char **argv){ 8 | Mat img1; 9 | img1 = imread(argv[1], CV_LOAD_IMAGE_COLOR); 10 | Mat img2(img1.rows, img1.cols, CV_8UC1, Scalar(0)); 11 | Mat img3(img1.rows, img1.cols, CV_8UC1, Scalar(0)); 12 | 13 | int i, j; 14 | for(i = 0; i < img1.rows; i++){ 15 | for(j = 0; j < img1.cols; j++){ 16 | img2.at(i, j) = (img1.at(i, j)[0] + img1.at(i, j)[1] + img1.at(i, j)[2]) / 3; 17 | } 18 | } 19 | for(i = 0; i < img1.rows; i++){ 20 | for(j = 0; j < img1.cols; j++){ 21 | img3.at(i, j) = 0.11*img1.at(i, j)[0] + 0.59*img1.at(i, j)[1] + 0.3*img1.at(i, j)[2]; 22 | } 23 | } 24 | 25 | namedWindow("Normal", WINDOW_AUTOSIZE); 26 | imshow("Normal", img1); 27 | namedWindow("Average", WINDOW_AUTOSIZE); 28 | imshow("Average", img2); 29 | namedWindow("Weighted Average", WINDOW_AUTOSIZE); 30 | imshow("Weighted Average", img3); 31 | 32 | waitKey(0); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /linear_filter_lib_func.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/imgproc/imgproc.hpp" 2 | #include "opencv2/highgui/highgui.hpp" 3 | #include 4 | #include 5 | 6 | using namespace cv; 7 | 8 | /** @function main */ 9 | int main ( int argc, char** argv ) 10 | { 11 | /// Declare variables 12 | Mat src, dst; 13 | 14 | Mat kernel; 15 | Point anchor; 16 | double delta; 17 | int ddepth; 18 | int kernel_size; 19 | char* window_name = "filter2D Demo"; 20 | 21 | int c; 22 | 23 | /// Load an image 24 | src = imread( argv[1] ); 25 | 26 | if( !src.data ) 27 | { return -1; } 28 | 29 | /// Create window 30 | namedWindow( window_name, CV_WINDOW_AUTOSIZE ); 31 | 32 | /// Initialize arguments for the filter 33 | anchor = Point( -1, -1 ); 34 | delta = 0; 35 | ddepth = -1; 36 | 37 | /// Loop - Will filter the image with different kernel sizes each 0.5 seconds 38 | int ind = 0; 39 | while( true ) 40 | { 41 | c = waitKey(500); 42 | /// Press 'ESC' to exit the program 43 | if( (char)c == 27 ) 44 | { break; } 45 | 46 | /// Update kernel size for a normalized box filter 47 | kernel_size = 3 + 2*( ind%5 ); 48 | kernel = Mat::ones( kernel_size, kernel_size, CV_32F )/ (float)(kernel_size*kernel_size); 49 | 50 | /// Apply filter 51 | filter2D(src, dst, ddepth , kernel, anchor, delta, BORDER_DEFAULT ); 52 | imshow( window_name, dst ); 53 | ind++; 54 | } 55 | 56 | return 0; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /linear_filter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(int argc, char **argv){ 9 | Mat img; 10 | img = imread("bikini.jpg", CV_LOAD_IMAGE_COLOR); 11 | //Mat final_img(img.rows, img.cols, CV_8UC3, Scalar(0, 0, 0)); 12 | int kernel_size = 3; 13 | //float kernel = (kernel_size*kernel_size); 14 | float sumb, sumg, sumr; 15 | int i, j, x, y; 16 | float kernel[3][3]={{1.0/16.0, 1.0/8.0, 1.0/16.0}, 17 | {1.0/8.0, 1.0/4.0, 1.0/8.0}, 18 | {1.0/16.0, 1.0/8.0, 1.0/16.0}}; 19 | 20 | for(i = 1; i < img.rows-1; i++){ 21 | for(j = 1; j < img.cols-1; j++){ 22 | sumb = sumg = sumr = 0.0; 23 | for(x = -1; x < kernel_size-1; x++){ 24 | for(y = -1; y < kernel_size-1; y++){ 25 | sumb += (int)(img.at(i + x, j + y)[0] * kernel[1+x][y+1]); 26 | sumg += (int)(img.at(i + x, j + y)[1] * kernel[1+x][1+y]); 27 | sumr += (int)(img.at(i + x, j + y)[2] * kernel[1+x][1+y]); 28 | } 29 | } 30 | img.at(i, j)[0] = sumb; 31 | img.at(i, j)[1] = sumg; 32 | img.at(i, j)[2] = sumr; 33 | } 34 | } 35 | 36 | namedWindow("Filter", WINDOW_AUTOSIZE); 37 | imshow("Filter", img); 38 | waitKey(0); 39 | } 40 | -------------------------------------------------------------------------------- /edge_detection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace cv; 8 | 9 | int main(int argc, char **argv){ 10 | Mat img; 11 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 12 | Mat final_img(img.rows, img.cols, CV_8UC3, Scalar(0, 0, 0)); 13 | Mat img1(img.rows, img.cols, CV_8UC1, Scalar(0)); 14 | cvtColor(img, img1, CV_BGR2GRAY); 15 | 16 | int gx, gy, g; 17 | gx = gy = g = 0; 18 | int i, j, x, y; 19 | int kernel1[3][3]={{-1, 0, +1}, 20 | {-2, 0, +2}, 21 | {-1, 0, +1}}; 22 | int kernel2[3][3]={{1, 2, 1}, 23 | {0, 0, 0}, 24 | {-1, -2, -1}}; 25 | 26 | for(i = 1; i < img.rows-1; i++){ 27 | for(j = 1; j < img.cols-1; j++){ 28 | gx = gy = g = 0; 29 | for(x = -1; x < 2; x++){ 30 | for(y = -1; y < 2; y++){ 31 | gx += img1.at(i + x, j + y) * kernel1[1+x][y+1]; 32 | gy += img1.at(i + x, j + y) * kernel2[1+x][y+1]; 33 | } 34 | } 35 | g = sqrt(gx*gx + gy*gy); 36 | if(g > 255) 37 | g = 255; 38 | final_img.at(i, j) = g; 39 | } 40 | } 41 | 42 | namedWindow("Filter", WINDOW_AUTOSIZE); 43 | imshow("Filter", final_img); 44 | waitKey(0); 45 | } 46 | -------------------------------------------------------------------------------- /canny_filter.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace cv; 9 | 10 | Mat src, src_gray; 11 | Mat dst, detected_edges; 12 | 13 | int edgeThresh = 1; 14 | int lowThreshold; 15 | int const max_lowThreshold = 100; 16 | int ratio = 3; 17 | int kernel_size = 3; 18 | 19 | void CannyThreshold(int, void*){ 20 | // Reduce noise with a kernel 3x3 21 | blur(src_gray, detected_edges, Size(3, 3)); 22 | 23 | // Canny detector 24 | Canny(detected_edges, detected_edges, lowThreshold, lowThreshold*ratio, kernel_size); 25 | 26 | // Using Canny's out as a mask, we display our result 27 | dst = Scalar::all(0); 28 | 29 | src.copyTo(dst, detected_edges); 30 | imshow("Edge Map", dst); 31 | } 32 | 33 | int main(int argc, char **argv){ 34 | // Load an image 35 | src = imread(argv[1]); 36 | 37 | // Create a matrix of the same type and size as src (for dst) 38 | dst.create(src.size(), src.type()); 39 | 40 | // Convert the image to grayscale 41 | cvtColor(src, src_gray, CV_BGR2GRAY); 42 | 43 | // Create a window 44 | namedWindow("Edge Map", CV_WINDOW_AUTOSIZE); 45 | 46 | // Create a Trackbar for user to enter threshold 47 | createTrackbar("Min Threshold:", "Edge Map", &lowThreshold, max_lowThreshold, CannyThreshold); 48 | 49 | // Show the image 50 | CannyThreshold(0, 0); 51 | 52 | waitKey(0); 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /color_extractor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int range(int x, int y, int tol){ 8 | if(x > y - tol && x < y + tol) 9 | return 1; 10 | else if(y > x - tol && y < x + tol) 11 | return 1; 12 | else 13 | return 0; 14 | } 15 | 16 | int main(int argc, char **argv){ 17 | Mat img; 18 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 19 | Mat final_img(img.rows, img.cols, CV_8UC3, Scalar(0, 0, 0)); 20 | 21 | namedWindow("Color Extractor", WINDOW_AUTOSIZE); 22 | int red, blue, green, tol; 23 | red = blue = green = tol = 0; 24 | int i, j; 25 | 26 | createTrackbar("Blue", "Color Extractor", &blue, 255); 27 | createTrackbar("Green", "Color Extractor", &green, 255); 28 | createTrackbar("Red", "Color Extractor", &red, 255); 29 | createTrackbar("Tolerance", "Color Extractor", &tol, 50); 30 | while(1){ 31 | for(i = 0; i < img.rows; i++){ 32 | for(j = 0; j < img.cols; j++){ 33 | if(range(img.at(i, j)[0], blue, tol) && range(img.at(i, j)[1], green, tol) && range(img.at(i, j)[2], red, tol)){ 34 | final_img.at(i, j) = img.at(i, j); 35 | } 36 | else{ 37 | final_img.at(i, j)[0] = 255; 38 | final_img.at(i, j)[1] = 255; 39 | final_img.at(i, j)[2] = 255; 40 | } 41 | } 42 | } 43 | imshow("Color Extractor", final_img); 44 | if(waitKey(10) == 'q') 45 | break; 46 | } 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /hough_transform_lib.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/highgui/highgui.hpp" 2 | #include "opencv2/imgproc/imgproc.hpp" 3 | 4 | #include 5 | 6 | using namespace cv; 7 | using namespace std; 8 | 9 | void help() 10 | { 11 | cout << "\nThis program demonstrates line finding with the Hough transform.\n" 12 | "Usage:\n" 13 | "./houghlines , Default is pic1.jpg\n" << endl; 14 | } 15 | 16 | int main(int argc, char** argv) 17 | { 18 | const char* filename = argc >= 2 ? argv[1] : "pic1.jpg"; 19 | 20 | Mat src = imread(filename, 0); 21 | if(src.empty()) 22 | { 23 | help(); 24 | cout << "can not open " << filename << endl; 25 | return -1; 26 | } 27 | 28 | Mat dst, cdst; 29 | Canny(src, dst, 50, 200, 3); 30 | cvtColor(dst, cdst, CV_GRAY2BGR); 31 | 32 | #if 0 33 | vector lines; 34 | HoughLines(dst, lines, 1, CV_PI/180, 100, 0, 0 ); 35 | 36 | for( size_t i = 0; i < lines.size(); i++ ) 37 | { 38 | float rho = lines[i][0], theta = lines[i][1]; 39 | Point pt1, pt2; 40 | double a = cos(theta), b = sin(theta); 41 | double x0 = a*rho, y0 = b*rho; 42 | pt1.x = cvRound(x0 + 1000*(-b)); 43 | pt1.y = cvRound(y0 + 1000*(a)); 44 | pt2.x = cvRound(x0 - 1000*(-b)); 45 | pt2.y = cvRound(y0 - 1000*(a)); 46 | line( cdst, pt1, pt2, Scalar(0,0,255), 3, CV_AA); 47 | } 48 | #else 49 | vector lines; 50 | HoughLinesP(dst, lines, 1, CV_PI/180, 50, 50, 10 ); 51 | for( size_t i = 0; i < lines.size(); i++ ) 52 | { 53 | Vec4i l = lines[i]; 54 | line( cdst, Point(l[0], l[1]), Point(l[2], l[3]), Scalar(0,0,255), 3, CV_AA); 55 | } 56 | #endif 57 | imshow("source", src); 58 | imshow("detected lines", cdst); 59 | 60 | waitKey(); 61 | 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /edge_detection_simple.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | using namespace std; 7 | using namespace cv; 8 | 9 | int main(int argc, char **argv){ 10 | Mat img; 11 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 12 | Mat final_img(img.rows, img.cols, CV_8UC1, Scalar(255)); 13 | Mat img1(img.rows, img.cols, CV_8UC1, Scalar(0)); 14 | cvtColor(img, img1, CV_BGR2GRAY); 15 | 16 | int threshold = 50; 17 | int i, j, x, y; 18 | int max, min; 19 | namedWindow("Edge Detection", WINDOW_AUTOSIZE); 20 | namedWindow("Original", WINDOW_AUTOSIZE); 21 | imshow("Original", img1); 22 | createTrackbar("Threshold", "Edge Detection", &threshold, 255); 23 | while(1){ 24 | for(i = 1; i < img.rows - 1; i++){ 25 | for(j = 1; j < img.cols - 1; j++){ 26 | for(x = i - 1; x < i + 1; x++){ 27 | for(y = j -1; y < j + 1; y++){ 28 | max = img1.at(i - 1, j - 1); 29 | if(img1.at(x, y) > max) 30 | max = img1.at(x, y); 31 | min = img1.at(i - 1, j - 1); 32 | if(img1.at(x, y) < min) 33 | min = img1.at(x, y); 34 | } 35 | } 36 | if( (max - min) > threshold ) 37 | final_img.at(i, j) = 0; 38 | } 39 | } 40 | imshow("Edge Detection", final_img); 41 | if(waitKey(20) == 'q') 42 | break; 43 | for(i = 0; i < img.rows; i++) 44 | for(j = 0; j < img.cols; j++) 45 | final_img.at(i, j) = 255; 46 | } 47 | 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /dilation_erosion.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | int main(int argc, char **argv){ 9 | Mat img; 10 | img = imread(argv[1], WINDOW_AUTOSIZE); 11 | Mat img1(img.rows, img.cols, CV_8UC1, Scalar(0)); 12 | Mat img2(img.rows, img.cols, CV_8UC1, Scalar(0)); 13 | Mat final_img(img.rows, img.cols, CV_8UC1, Scalar(255)); 14 | cvtColor(img, img1, CV_BGR2GRAY); 15 | 16 | int i, j, x, y, white, black; 17 | // Binary image 18 | for(i = 0; i < img1.rows; i++) 19 | for(j = 0; j < img1.cols; j++) 20 | if(img1.at(i, j) > 127) 21 | img2.at(i, j) = 255; 22 | 23 | namedWindow("Binary", WINDOW_AUTOSIZE); 24 | imshow("Binary", img1); 25 | for(i = 1; i < img2.rows-1; i++){ 26 | for(j = 1; j < img2.cols-1; j++){ 27 | white = black = 0; 28 | for(x = -1; x < 2; x++){ 29 | for(y = -1; y < 2; y++){ 30 | if(img2.at(i + x, j + y) == 255) 31 | black++; 32 | else 33 | white++; 34 | } 35 | } 36 | if(white > black){ 37 | for(x = -1; x < 2; x++){ 38 | for(y = -1; y < 2; y++){ 39 | final_img.at(i + x, j + y) = 0; 40 | } 41 | } 42 | } 43 | } 44 | } 45 | for(i = 1; i < img2.rows-1; i++){ 46 | for(j = 1; j < img2.cols-1; j++){ 47 | white = black = 0; 48 | for(x = -1; x < 2; x++){ 49 | for(y = -1; y < 2; y++){ 50 | if(img2.at(i + x, j + y) == 255) 51 | black++; 52 | else 53 | white++; 54 | } 55 | } 56 | if(black > 0){ 57 | for(x = -1; x < 2; x++){ 58 | for(y = -1; y < 2; y++){ 59 | final_img.at(i + x, j + y) = 255; 60 | } 61 | } 62 | } 63 | } 64 | } 65 | 66 | namedWindow("Dilate", WINDOW_AUTOSIZE); 67 | imshow("Dilate", final_img); 68 | 69 | waitKey(0); 70 | return 0; 71 | } 72 | -------------------------------------------------------------------------------- /blob_detection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace cv; 9 | 10 | typedef struct{ 11 | int x; 12 | int y; 13 | } point; 14 | 15 | Mat img; 16 | int visited[1000][1000]; 17 | 18 | void DFS(point p){ 19 | int i, j; 20 | stack s; 21 | s.push(p); 22 | point temp; 23 | while(!s.empty()){ 24 | point A = s.top(); 25 | s.pop(); 26 | visited[A.x][A.y] = 1; 27 | 28 | // Finding other nodes 29 | for(i = -1; i < 2; i++){ 30 | for(j = -1; j < 2; j++){ 31 | temp.x = A.x + i; 32 | temp.y = A.x + j; 33 | if((img.at(temp.x, temp.y) == 0) && (visited[temp.x][temp.y] == 0)) 34 | s.push(temp); 35 | } 36 | } 37 | } 38 | } 39 | 40 | int main(int argc, char **argv){ 41 | img = imread(argv[1], CV_LOAD_IMAGE_COLOR); 42 | Mat img1(img.rows, img.cols, CV_8UC1, Scalar(0)); 43 | Mat img2(img.rows, img.cols, CV_8UC1, Scalar(0)); 44 | Mat final_img(img.rows, img.cols, CV_8UC3, Scalar(0, 0, 0)); 45 | cvtColor(img, img1, CV_BGR2GRAY); 46 | int i, j; 47 | 48 | namedWindow("Original", WINDOW_AUTOSIZE); 49 | imshow("Original", img); 50 | namedWindow("Binary", WINDOW_AUTOSIZE);; 51 | // Binary image 52 | for(i = 0; i < img1.rows; i++) 53 | for(j = 0; j < img1.cols; j++) 54 | if(img1.at(i, j) > 127) 55 | img2.at(i, j) = 255; 56 | imshow("Binary", img2); 57 | 58 | 59 | // Initialise a array named visited 60 | for(i = 0; i < img.rows; i++) 61 | for(j = 0; j < img.cols; j++) 62 | visited[i][j] = 0; 63 | 64 | // Applying DFS 65 | point p; 66 | for(i = 1; i < img2.rows - 1; i++){ 67 | for(j = 1; j < img2.cols - 1; j++){ 68 | p.x = i; 69 | p.y = j; 70 | if((img2.at(i, j) == 0) && visited[i][j] == 0) 71 | DFS(p); 72 | } 73 | } 74 | 75 | // Marking the blob red 76 | for(i = 0; i < img2.rows; i++) 77 | for(j = 0; j < img2.rows; j++) 78 | if(visited[i][j]) 79 | final_img.at(i, j)[2] = 255; 80 | 81 | namedWindow("YES", WINDOW_AUTOSIZE); 82 | imshow("YES", final_img); 83 | waitKey(0); 84 | return 0; 85 | } 86 | -------------------------------------------------------------------------------- /who_wins.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | using namespace std; 8 | using namespace cv; 9 | 10 | int range(int x, int y, int d){ 11 | if(x >= y - d && x <= y + d) 12 | return 1; 13 | else if(y >= x - d && y <= x + d) 14 | return 1; 15 | else 16 | return 0; 17 | } 18 | 19 | vector corrector(vector v){ 20 | vector l; 21 | int i, j, flag; 22 | for(i = 0; i < v.size(); i++){ 23 | flag = 1; 24 | for(j = 0; j < l.size(); j++){ 25 | if(range(l[j].x, v[i].x, 5)) 26 | flag = 0; 27 | } 28 | if(flag) 29 | l.push_back(v[i]); 30 | } 31 | 32 | 33 | return l; 34 | } 35 | 36 | int main(){ 37 | Mat src1, src2; 38 | Mat src1_gray, src2_gray; 39 | Mat canny1, canny2; 40 | vector > contours1, contours2; 41 | vector hierarchy1, hierarchy2; 42 | VideoCapture cap("video.avi"); 43 | float t1, t2; 44 | t1 = 900; 45 | t2 = 1500; 46 | 47 | if(!cap.isOpened()){ 48 | cout << "Sorry there was some problem playing the video" << endl; 49 | return -1; 50 | } 51 | 52 | // Store various instances of frames of video 53 | cap.set(CV_CAP_PROP_POS_MSEC, t1); 54 | cap >> src1; 55 | cap.set(CV_CAP_PROP_POS_MSEC, t2); 56 | cap >> src2; 57 | 58 | // Convert image to gray and blur it 59 | cvtColor(src1, src1_gray, CV_BGR2GRAY); 60 | cvtColor(src2, src2_gray, CV_BGR2GRAY); 61 | blur(src1_gray, src1_gray, Size(3, 3)); 62 | blur(src2_gray, src2_gray, Size(3, 3)); 63 | 64 | // Applying Canny for edge Detection 65 | Canny(src1_gray, canny1, 10, 30, 3); 66 | Canny(src2_gray, canny2, 10, 30, 3); 67 | 68 | // Find contours 69 | findContours(canny1, contours1, hierarchy1, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 70 | findContours(canny2, contours2, hierarchy2, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); 71 | 72 | // Get centres of contours 73 | int i, j; 74 | int tempx, tempy; 75 | vector centres1_, centres2_, centres1, centres2; 76 | for(i = 0; i < contours1.size(); i++){ 77 | tempx = tempy = 0; 78 | for(j = 0; j < contours1[i].size(); j++){ 79 | tempx += contours1[i][j].x; 80 | tempy += contours1[i][j].y; 81 | } 82 | centres1_.push_back(Point(tempx/j, tempy/j)); 83 | } 84 | for(i = 0; i < contours2.size(); i++){ 85 | tempx = tempy = 0; 86 | for(j = 0; j < contours2[i].size(); j++){ 87 | tempx += contours2[i][j].x; 88 | tempy += contours2[i][j].y; 89 | } 90 | centres2_.push_back(Point(tempx/j, tempy/j)); 91 | } 92 | 93 | centres1 = corrector(centres1_); 94 | centres2 = corrector(centres2_); 95 | 96 | vector a; 97 | vector u; 98 | float y1, y2; 99 | y1 = y2 = 0; 100 | for(i = 0; i < centres1.size(); i++){ 101 | y1 = centres1[i].y; 102 | y2 = centres2[i].y; 103 | u.push_back((y2*t1*t1 - y1*t2*t2)/t1/t2/(t1-t2)); 104 | a.push_back((y1*t2 - y2*t1)/t1/t2/(t2-t1)); 105 | } 106 | 107 | vector position; 108 | float t_final = 8000; 109 | for(i = 0; i < a.size(); i++) 110 | position.push_back(u[i]*t_final + t_final*t_final*0.5*a[i]); 111 | 112 | for(i = 0; i < a.size(); i++) 113 | cout << "x = " << position[i] << endl; 114 | 115 | for(i = 0; i < centres1.size(); i++) 116 | cout << centres1[i] << endl; 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | imshow("src1", canny1); 127 | imshow("src2", canny2); 128 | 129 | if(waitKey(0) == 'q') 130 | return 0; 131 | } 132 | --------------------------------------------------------------------------------