├── Panorama └── README.md ├── .gitignore ├── demo.jpg ├── sample.jpg ├── Cartoonifier_Report.pdf ├── Similarity_Images ├── blur ├── blur_taarak.png ├── taarak_mehta.jpg ├── blurred_taarak_figure.png ├── taarak_original_figure.png ├── links_references.txt └── Histogram_comparison.py ├── images_samples ├── demo.jpg ├── sample.jpg └── sample2.jpg ├── template_matching └── output_threshold_0.55_.png ├── rect_demo.cpp ├── window_image.py ├── LICENSE ├── canny_edge.cpp ├── blurring_.cpp ├── difference_images.cpp ├── README.md ├── Without_TAPI_OpenCV.cpp ├── TAPI_opencv.cpp ├── filter_specific_color.py ├── imMatrix.cpp ├── Color Filtering.ipynb ├── background_subtraction.cpp ├── screenshot_cpp.cpp ├── cartoonification.cpp └── draw_rectangle.cpp /Panorama/README.md: -------------------------------------------------------------------------------- 1 | Link: soon. 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | changeQuantisationGray.cpp 2 | blurring_.cpp 3 | -------------------------------------------------------------------------------- /demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/demo.jpg -------------------------------------------------------------------------------- /sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/sample.jpg -------------------------------------------------------------------------------- /Cartoonifier_Report.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/Cartoonifier_Report.pdf -------------------------------------------------------------------------------- /Similarity_Images/blur: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/Similarity_Images/blur -------------------------------------------------------------------------------- /images_samples/demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/images_samples/demo.jpg -------------------------------------------------------------------------------- /images_samples/sample.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/images_samples/sample.jpg -------------------------------------------------------------------------------- /images_samples/sample2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/images_samples/sample2.jpg -------------------------------------------------------------------------------- /Similarity_Images/blur_taarak.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/Similarity_Images/blur_taarak.png -------------------------------------------------------------------------------- /Similarity_Images/taarak_mehta.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/Similarity_Images/taarak_mehta.jpg -------------------------------------------------------------------------------- /Similarity_Images/blurred_taarak_figure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/Similarity_Images/blurred_taarak_figure.png -------------------------------------------------------------------------------- /Similarity_Images/taarak_original_figure.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/Similarity_Images/taarak_original_figure.png -------------------------------------------------------------------------------- /template_matching/output_threshold_0.55_.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/krshrimali/OpenCV_Work/master/template_matching/output_threshold_0.55_.png -------------------------------------------------------------------------------- /rect_demo.cpp: -------------------------------------------------------------------------------- 1 | #include "cv.h" 2 | #include "highgui.h" 3 | #include 4 | 5 | using namespace std; 6 | int main(int argc, char** argv) { 7 | if(argc == 1) { 8 | cerr << "Image not given. " << endl; 9 | exit(1); 10 | } 11 | else { 12 | IplImage* img = cvLoadImage( argv[1] ); 13 | cvRectange(img, cvPoint(5,10), cvPoint(20,30), cvScalar(255, 255, 255) 14 | ); 15 | } 16 | cvNamedWindow("Image"); 17 | cvShowImage("Image", img); 18 | cvReleaseImage(&img); 19 | 20 | cvWaitKey(0); 21 | cvDestroyWindow("Image"); 22 | return 0; 23 | } 24 | -------------------------------------------------------------------------------- /Similarity_Images/links_references.txt: -------------------------------------------------------------------------------- 1 | 1) https://www.researchgate.net/post/How_can_we_measure_similarities_between_two_images 2 | 2) https://ece.uwaterloo.ca/~z70wang/research/ssim/ 3 | 3) https://link.springer.com/chapter/10.1007%2F978-1-4471-2458-0_2 4 | 4) https://ieeexplore.ieee.org/document/1284395/ 5 | 5) http://docdingle.com/teaching/cs545/assigns/cs545_A03_Compare.pdf 6 | 6) https://arxiv.org/pdf/1710.02726.pdf 7 | 7) https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_feature2d/py_surf_intro/py_surf_intro.html 8 | 8) http://note.sonots.com/SciSoftware/SIFT.html 9 | 9) https://www.bc.edu/bc-web/schools/mcas/departments/computer-science.html 10 | 10) https://ieeexplore.ieee.org/document/1453520/?arnumber=1453520&tag=1 11 | 11) http://people.csail.mit.edu/torralba/code/spatialenvelope/s 12 | -------------------------------------------------------------------------------- /window_image.py: -------------------------------------------------------------------------------- 1 | ''' 2 | AIM : create a window of NxN dimension, and slide it over the whole image 3 | @author: Kushashwa Ravi Shrimali 4 | ''' 5 | import cv2 6 | import sys 7 | 8 | def filter(region): 9 | rows = region.shape[0] 10 | cols = region.shape[1] 11 | print(rows, cols) 12 | for i in range(rows): 13 | for j in range(cols): 14 | # print(region[i][j]) 15 | region[i][j] = [0, 0, 0] 16 | 17 | # return region 18 | img = cv2.imread(sys.argv[1], 1) # read image - color mode 19 | cv2.imshow(str(sys.argv[1]), img) 20 | cv2.waitKey(0) 21 | cv2.destroyAllWindows() 22 | 23 | rows = img.shape[0] 24 | cols = img.shape[1] 25 | 26 | N = int(sys.argv[2]) 27 | print(int(cols/N)) 28 | i = 0 29 | j = 0 30 | while(True): 31 | # img[i:i+N] = filter(img[i:i+Ni,:, :]) 32 | filter(img[i:i+N, j:j+N, :]) 33 | # filter(img[:, j:j+N, :]) 34 | i = i + N 35 | j = j + N 36 | 37 | 38 | cv2.imshow(str(sys.argv[1]), img) 39 | cv2.waitKey(0) 40 | cv2.destroyAllWindows() 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Kushashwa Ravi Shrimali 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /canny_edge.cpp: -------------------------------------------------------------------------------- 1 | // output image - canny edge 2 | 3 | #include "highgui.h" 4 | #include "cv.h" 5 | #include 6 | #include 7 | 8 | using namespace std; 9 | 10 | IplImage* doCanny( 11 | IplImage* in, 12 | double lowThresh, 13 | double highThresh, 14 | double aperture 15 | ) { 16 | // if(in->nChannels != 1){ 17 | // cerr << "Channels should be 1 of the image " << endl; 18 | // return (0./); // Canny needs only gray scale images 19 | // } 20 | 21 | IplImage* out = cvCreateImage( 22 | cvSize(in->width, in->height), 23 | IPL_DEPTH_8U, 24 | 1 25 | ); 26 | cvCanny(in, out, lowThresh, highThresh, aperture); 27 | return (out); 28 | }; 29 | 30 | int main(int argc, char** argv) { 31 | if(argc == 1) 32 | exit(0); 33 | IplImage* image = cvLoadImage(argv[1]); 34 | // cout << image->width << image->height << endl; 35 | int low_thresh = atoi(argv[2]); 36 | int high_thresh = atoi(argv[3]); 37 | 38 | cout << low_thresh << high_thresh << endl; 39 | 40 | IplImage* output_image = doCanny(image, low_thresh, high_thresh, 3); 41 | cvNamedWindow("Input Image"); 42 | cvNamedWindow("Output Image"); 43 | cvShowImage("Input Image", image); 44 | cvShowImage("Output Image", output_image); 45 | 46 | cvReleaseImage(&image); 47 | cvReleaseImage(&output_image); 48 | cvWaitKey(0); 49 | cvDestroyWindow("Input Image"); 50 | cvDestroyWindow("Output Image"); 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /blurring_.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/imgproc/imgproc.hpp" 2 | #include "opencv2/highgui/highgui.hpp" 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | void invert_color(Mat& src, Mat& dst); 8 | 9 | int main( int argc, char** argv ){ 10 | int MAX_KERNEL_LENGTH = 31; 11 | 12 | Mat src; Mat dst; 13 | char window_name[] = "Original Image"; 14 | char window_name_1[] = "Blured Image"; 15 | char window_name_2[] = "Detailed Image"; 16 | 17 | namedWindow( window_name, CV_WINDOW_AUTOSIZE ); 18 | namedWindow(window_name_1, CV_WINDOW_AUTOSIZE); 19 | namedWindow(window_name_2, CV_WINDOW_AUTOSIZE); 20 | 21 | /// Load the source image 22 | src = imread( argv[1], 1 ); 23 | 24 | while(true) { 25 | imshow(window_name, src); 26 | 27 | dst = src.clone(); 28 | for ( int i = 1; i < MAX_KERNEL_LENGTH; i = i + 2 ){ 29 | blur( src, dst, Size( i, i ), Point(-1, -1)); 30 | } 31 | imshow(window_name_1, dst); 32 | 33 | Mat diff = src - dst; 34 | // invert_color(diff, diff); 35 | 36 | imshow(window_name_2, diff); 37 | 38 | int c = waitKey( 20 ); 39 | if( c == 27 ) { break; } 40 | } 41 | return 0; 42 | } 43 | 44 | void invert_color(Mat src, Mat dst) { 45 | CV_Assert ( src.type() == CV_8UC3 ); 46 | dst = src.clone(); 47 | for(int row=0; row(row,col)[channel] = 255 - src.at(row,col)[channel]; 51 | } 52 | } 53 | } 54 | // return dst; 55 | 56 | } -------------------------------------------------------------------------------- /difference_images.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/imgproc/imgproc.hpp" 2 | #include "opencv2/highgui/highgui.hpp" 3 | #include 4 | 5 | using namespace std; 6 | using namespace cv; 7 | 8 | void invert_color(Mat src, Mat dst); 9 | 10 | int main( int argc, char** argv ){ 11 | Mat src; Mat dst; 12 | char window_name[] = "Original Image"; 13 | char window_name_1[] = "New Image"; 14 | char window_name_2[] = "Difference Image"; 15 | 16 | namedWindow(window_name, CV_WINDOW_AUTOSIZE); 17 | namedWindow(window_name_1, CV_WINDOW_AUTOSIZE); 18 | namedWindow(window_name_2, CV_WINDOW_AUTOSIZE); 19 | 20 | src = imread( argv[1], 1 ); 21 | dst = imread( argv[2], 1 ); 22 | 23 | cout << src.size() << endl; 24 | resize(dst, dst, src.size(), INTER_LINEAR); 25 | 26 | cout << dst.size() << endl; 27 | 28 | while (true) 29 | { 30 | imshow(window_name, src); 31 | imshow(window_name_1, dst); 32 | 33 | Mat diff = src - dst; 34 | invert_color(diff, diff); 35 | imshow(window_name_2, diff); 36 | 37 | int c = waitKey( 20 ); 38 | if( c == 27 ) { break; } 39 | } 40 | } 41 | 42 | void invert_color(Mat src, Mat dst) { 43 | CV_Assert ( src.type() == CV_8UC3 ); 44 | dst = src.clone(); 45 | for(int row=0; row(row,col)[channel] = 255 - src.at(row,col)[channel]; 49 | } 50 | } 51 | } 52 | imshow("dst", dst); 53 | // return dst; 54 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenCV_Work 2 | OpenCV Work, including interesting problem statements [following OpenCV Tutorials - Python] 3 | 4 | Digital Image Processing is the step-1 to start with Computer Vision. Think of a problem of scene recognition, what is the first thing that comes in mind after seeing a scene? Several objects, background, some moving and some static. 5 | 6 | A problem can be subdivided into sub-problems, and sub-problems into their sub-problems. In this way, if carried on, the basic problems come down to - Processing the images according to our need. What is our need in detecting features? A grayscaled image? Thresholded? Mean blurred? Edge detected? There are many more ideas to this, and this has been covered up in this project. 7 | 8 | Two goals: 9 | 1) Object Recognition / Scene Understanding. 10 | 2) Template Matching. 11 | 12 | How the goals have been targetted? 13 | Both of the goals are nothing but complex computer vision problems, active research topics, but they are nothing more than simple problems for Humans. I've tried to think about them as a problem for my eyes, and how will I tend to solve this? For that, the filters I thought will be required, for the completion of goals, have been implemented - tested - compared. In this way, getting better knowledge of the filters and pre-processing of images. 14 | 15 | Files: 16 | 1. blurring_.cpp, difference_images.cpp, screenshot_cpp.cpp: 17 | Includes blurring methods, detailed images, and screenshot taking code respectively. 18 | 19 | 2. cartoonification.cpp, draw_rectangle.cpp 20 | These are long programs, of cartoonification of an image and choosing ROI and applying filters & methods on it, respectively. 21 | 22 | Cartoonification Project Report has been uploaded and can be checked for further details. 23 | 24 | 25 | Suggestions, feedback, commits - welcome! 26 | -------------------------------------------------------------------------------- /Without_TAPI_OpenCV.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Using OpenCV Transparent API, to make execution faster. 3 | Use only if advanced and complex functions are used, else 4 | over head of moving image to GPU dominates timing. 5 | [Reference: learnopencv.com/opencv-transparent-api/] 6 | 7 | Usage: 8 | g++ filename.cpp `pkg-config --libs opencv --cflags` 9 | 10 | Functions: 11 | Applys gaussian filter and edge filter detectors. 12 | 13 | Credits: 14 | learopencv and Kushashwa Ravi Shrimali 15 | */ 16 | #include 17 | #include 18 | // for recording time 19 | #include 20 | 21 | // to verify data types of image objects 22 | #include 23 | 24 | using namespace cv; 25 | using namespace std; 26 | 27 | // functions to convert from Mat to UMat 28 | UMat convert_mat_UMat(Mat); 29 | UMat convert_mat_UMat_copy(Mat); 30 | 31 | // linux alternate to getTickCount() 32 | /*unsigned getTickCount() { 33 | struct timeval tv; 34 | if(gettimeofday(&tv, NULL) != 0) 35 | return 0; 36 | return (tv.tv_sec * 1000) + (tv.tv_sec/1000); 37 | }*/ 38 | 39 | int main(int argc, char** argv) { 40 | // create image objects 41 | // OpenCV Transparent API way - UMat matrix used. 42 | // Options : 43 | // Convert Mat to UMat 44 | double startT = (double)getTickCount(); 45 | cout << startT/getTickFrequency() << endl; 46 | Mat img, gray, edge_filtered, gaussian_filtered; 47 | 48 | // read the image in color mode 49 | // copy the read image to img, OpenCV Transparent API way 50 | imread(argv[1], IMREAD_COLOR).copyTo(img); 51 | UMat img2 = convert_mat_UMat(img); 52 | cout << typeid(img2).name() << endl; 53 | 54 | 55 | // resize the image, optional, if higher resolution 56 | // convert image to grayscale, store into gray object 57 | cvtColor(img2, gray, COLOR_BGR2GRAY); 58 | 59 | // apply gaussian blur filter 60 | GaussianBlur(gray, gaussian_filtered, Size(7, 7), 1.5); 61 | 62 | // apply canny edge detection 63 | Canny(gaussian_filtered, edge_filtered, 0, 100); 64 | 65 | imshow("edge_filtered", edge_filtered); 66 | imshow("gaussian_filtered", gaussian_filtered); 67 | imshow("img", img); 68 | imshow("gray", gray); 69 | 70 | imwrite("./images/edge_filtered_2.jpg", edge_filtered); 71 | double endT = (double)getTickCount(); 72 | cout << endT/getTickFrequency()<< endl; 73 | cout << "Time taken: " << (endT - startT)/getTickFrequency() << endl; 74 | waitKey(0); 75 | 76 | } 77 | 78 | // flags - ? 79 | UMat convert_mat_UMat(Mat img) { 80 | return img.getUMat(ACCESS_FAST); 81 | } 82 | 83 | UMat convert_mat_UMat_copy(Mat img) { 84 | UMat umat; 85 | img.copyTo(umat); 86 | return umat; 87 | } -------------------------------------------------------------------------------- /TAPI_opencv.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Using OpenCV Transparent API, to make execution faster. 3 | Use only if advanced and complex functions are used, else 4 | over head of moving image to GPU dominates timing. 5 | [Reference: learnopencv.com/opencv-transparent-api/] 6 | 7 | Usage: 8 | g++ filename.cpp `pkg-config --libs opencv --cflags` 9 | 10 | Functions: 11 | Applys gaussian filter and edge filter detectors. 12 | 13 | Credits: 14 | learopencv and Kushashwa Ravi Shrimali 15 | */ 16 | #include 17 | #include 18 | // for recording time 19 | #include 20 | 21 | // to verify data types of image objects 22 | // #include 23 | 24 | using namespace cv; 25 | using namespace std; 26 | 27 | // functions to convert from Mat to UMat 28 | UMat convert_mat_UMat(Mat); 29 | UMat convert_mat_UMat_copy(Mat); 30 | 31 | // linux alternate to getTickCount() 32 | /*unsigned getTickCount() { 33 | struct timeval tv; 34 | if(gettimeofday(&tv, NULL) != 0) 35 | return 0; 36 | return (tv.tv_sec * 1000) + (tv.tv_sec/1000); 37 | }*/ 38 | 39 | int main(int argc, char** argv) { 40 | // create image objects 41 | // OpenCV Transparent API way - UMat matrix used. 42 | // Options : 43 | // Convert Mat to UMat 44 | double startT = (double)getTickCount(); 45 | cout << startT/getTickFrequency() << endl; 46 | Mat img, gray, edge_filtered, gaussian_filtered; 47 | 48 | // read the image in color mode 49 | // copy the read image to img, OpenCV Transparent API way 50 | imread(argv[1], IMREAD_COLOR).copyTo(img); 51 | // UMat img2 = convert_mat_UMat(img); 52 | // cout << typeid(img2).name() << endl; 53 | 54 | 55 | // resize the image, optional, if higher resolution 56 | // convert image to grayscale, store into gray object 57 | cvtColor(img, gray, COLOR_BGR2GRAY); 58 | 59 | // apply gaussian blur filter 60 | GaussianBlur(gray, gaussian_filtered, Size(7, 7), 1.5); 61 | 62 | // apply canny edge detection 63 | Canny(gaussian_filtered, edge_filtered, 0, 100); 64 | 65 | imshow("edge_filtered", edge_filtered); 66 | imshow("gaussian_filtered", gaussian_filtered); 67 | imshow("img", img); 68 | imshow("gray", gray); 69 | 70 | imwrite("./images/edge_filtered_2.jpg", edge_filtered); 71 | double endT = (double)getTickCount(); 72 | cout << endT/getTickFrequency()<< endl; 73 | cout << "Time taken: " << (endT - startT)/getTickFrequency() << endl; 74 | waitKey(0); 75 | } 76 | 77 | // flags - ? 78 | UMat convert_mat_UMat(Mat img) { 79 | return img.getUMat(ACCESS_FAST); 80 | } 81 | 82 | UMat convert_mat_UMat_copy(Mat img) { 83 | UMat umat; 84 | img.copyTo(umat); 85 | return umat; 86 | } 87 | 88 | 89 | -------------------------------------------------------------------------------- /Similarity_Images/Histogram_comparison.py: -------------------------------------------------------------------------------- 1 | # Resource: https://docs.opencv.org/3.1.0/d1/db7/tutorial_py_histogram_begins.html 2 | 3 | import cv2 4 | from matplotlib import pyplot as plt 5 | import numpy as np 6 | import sys 7 | 8 | path = '/home/krshrimali/Pictures/' 9 | 10 | def help(): 11 | print("----------------------------------------------------------") 12 | print("Parameters: ") 13 | print("1. image paths") 14 | print("----------------------------------------------------------") 15 | print("Result will be histogram in R, G, B Channel of the image. ") 16 | 17 | print("Author: Kushashwa Ravi Shrimali") 18 | 19 | 20 | def show(img, path=1): 21 | ''' 22 | function to show image read by the user 23 | parameter: image path or image read and path variable 24 | path = 1 if path given 25 | path = 0 if read image given 26 | returns image read by the user 27 | ''' 28 | if(path): 29 | img2 = cv2.imread(img) 30 | cv2.imshow(str(img), img2) 31 | else: 32 | cv2.imshow("Image", img) 33 | 34 | cv2.waitKey(0) 35 | cv2.destroyAllWindows() 36 | 37 | if(path): 38 | return img2 39 | else: 40 | return img 41 | 42 | def draw_hist(imgPath, img, full = None): 43 | print("Drawing Histogram") 44 | print("You want histograms of R, G, B Channels in one image or separate?") 45 | decide = int(input("separate or single? 0/1 ")) 46 | flag = 0 47 | color = ('b', 'g', 'r') 48 | for i, col in enumerate(color): 49 | hist = cv2.calcHist([img], [i], full, [256], [0,256]) 50 | plt.plot(hist, color = col) 51 | plt.xlim([0, 256]) 52 | if(decide == 0): 53 | plt.show() 54 | plt.savefig(imgPath + '_' + col + '.png') 55 | flag += 1 56 | print(flag) 57 | if(flag != 3): 58 | plt.show() 59 | plt.savefig(imgPath + '.png') 60 | 61 | # number of image paths entered 62 | argc = len(sys.argv) - 1 63 | 64 | def create_mask(img): 65 | mask = np.zeros(img.shape[:2], np.uint8) 66 | mask[100:300, 100:400] = 255 67 | masked_img = cv2.bitwise_and(img, img, mask = mask) 68 | show(masked_img, 0) 69 | draw_hist(img, mask) 70 | 71 | if(argc == 0): 72 | print("You have to enter image paths in order to view their histograms") 73 | help() 74 | sys.exit(0) 75 | 76 | print("Number of images: " + str(argc)) 77 | 78 | # display histograms of all the images entered 79 | # for R, G, B Channels 80 | for i in range(argc): 81 | print(sys.argv[i+1]) 82 | img_path = path + sys.argv[i+1] 83 | 84 | print("Reading image: " + img_path) 85 | 86 | img = show(img_path) 87 | draw_hist(img_path, img) 88 | 89 | # create_mask(img) 90 | -------------------------------------------------------------------------------- /filter_specific_color.py: -------------------------------------------------------------------------------- 1 | ''' 2 | @author: Kushashwa Ravi Shrimali 3 | Program to filter out particular color from the image. 4 | 5 | On way to make a drawing tool, with hands. 6 | 7 | Arguments: 8 | [Note: currently it takes green channel sample, will be changed soon. On experimental basis] 9 | 10 | Usage: python3 try_1.py sample2.jpg 240 11 | [example] - image uploaded on GitHub repository 12 | 13 | To Do: A lot ;) 14 | ''' 15 | import cv2 16 | import sys 17 | 18 | 19 | def read_and_show(img): 20 | ''' 21 | function: shows and returns an image 22 | argument: 23 | ''' 24 | image = cv2.imread(img, 1) # reading in color mode 25 | 26 | # showing image now 27 | cv2.imshow(str(img), image) 28 | cv2.waitKey(0) 29 | cv2.destroyAllWindows() 30 | return image 31 | 32 | def show_webcam(threshold, filter_ = 0, mirror = False): 33 | ''' 34 | function: shows filtered output image, doesn't show original image as of now - to save cost and efficiency 35 | 36 | argument: (int) (int) (bool) 37 | 38 | usage: show_webcam(240, 1) 39 | [Example] 40 | ''' 41 | cam = cv2.VideoCapture(0) # 0th ID let's say, change if another camera attached 42 | while True: 43 | ret_val, frame = cam.read() 44 | # if ret_val is false, it means camera is not functioning 45 | 46 | if mirror: 47 | frame = cv2.flip('My Webcam', frame) 48 | 49 | if(filter_ == 1): 50 | frame_ = filter(frame, threshold) 51 | 52 | cv2.imshow('Filtered', frame_) 53 | 54 | if cv2.waitKey(1) == 27: 55 | break 56 | else: 57 | cv2.imshow('My Webcam', frame) 58 | 59 | if cv2.waitKey(1) == 27: 60 | break 61 | 62 | cv2.destroyAllWindows() 63 | 64 | def filter(dest_gray, threshold): 65 | ''' 66 | function: 67 | above threshold - white 68 | below threshold - black 69 | 70 | argument: (int) 71 | 72 | usage: filter(image, 240) 73 | 74 | return value: returns filtered frame 75 | ''' 76 | # dest_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 77 | list_ = [] 78 | 79 | ''' 80 | for j in range(dest_gray.shape[1]): 81 | for k in range(dest_gray.shape[0]): 82 | list_.append(dest_gray[k][j]) 83 | 84 | print(set(list_)) 85 | ''' 86 | 87 | for j in range(dest_gray.shape[1]): 88 | for k in range(dest_gray.shape[0]): 89 | if(dest_gray[k][j][1] < threshold): 90 | # print(dest_gray[k][j]) 91 | dest_gray[k][j][0] = 0 92 | dest_gray[k][j][2] = 0 93 | dest_gray[k][j][1] = 0 94 | else: 95 | dest_gray[k][j][0] = 0 96 | dest_gray[k][j][2] = 0 97 | dest_gray[k][j][1] = 255 98 | 99 | # read_and_show(dest_gray) 100 | # orig = cv2.imread(str(original), 1) 101 | return dest_gray 102 | 103 | def main(): 104 | img = sys.argv[1] 105 | threshold = int(sys.argv[2]) 106 | image = read_and_show(img) # reads and shows image at the same time. 107 | show_webcam(threshold, 1) # mirroring is disabled as of now 108 | # filter(image, img, threshold) 109 | 110 | if __name__ == '__main__': 111 | main() 112 | -------------------------------------------------------------------------------- /imMatrix.cpp: -------------------------------------------------------------------------------- 1 | // Program to convert image to pixel matrix 2 | // Experiments on blurred images (mean blur) - to understand it's working and effects on pixel intensity 3 | // Credits: Kushashwa Ravi Shrimali 4 | // Dr. Shyama Prasad Mukherjee International Institute of Information Technology, Naya Raipur 5 | // Contact: kushashwa16100@iiitnr.edu.in 6 | #include "cv.h" 7 | #include 8 | #include "highgui.h" 9 | #include 10 | #include 11 | 12 | using namespace std; 13 | using namespace cv; 14 | 15 | CvMat* create_matrix(char*, Mat&); 16 | 17 | CvMat* create_matrix(char* name, Mat& dst) { 18 | CvMat* mat = cvCreateMat( dst.rows, dst.cols, CV_32FC1 ); 19 | cout << dst.rows << endl; 20 | cout << dst.cols << endl; 21 | 22 | cout << "Pixel Matrix for given image: " << name << endl; 23 | for(int row = 0; row < dst.rows; row++) { 24 | for(int col = 0; col < dst.cols; col++) { 25 | Vec3b intensity = dst.at(row, col); 26 | // cout << "Row is: " << row << " Col is: " << col << endl; 27 | // for(int i = 0; i < 1; i++) { 28 | int element = (int)intensity.val[0]; 29 | *((int*)CV_MAT_ELEM_PTR(*mat, row, col) ) = element; 30 | // cout << (int)intensity.val[i] << endl; 31 | // } 32 | } 33 | } 34 | 35 | for(int i = 0; i < dst.rows; i++) { 36 | for(int j = 0; j < dst.cols; j++) { 37 | cout << CV_MAT_ELEM(*mat, int, i, j) << "\t"; 38 | } 39 | cout << endl; 40 | } 41 | cout << "Printed" << endl; 42 | return mat; 43 | } 44 | int main(int argc, char** argv) { 45 | if(argc < 2) { 46 | cerr << "Incorrect arguments. Input image address." << endl; 47 | return -1; 48 | } 49 | Mat src = imread(argv[1]); 50 | // cout << src.size() << endl; 51 | Size size = src.size(); 52 | // cout << size[0] << size[1] << endl; 53 | cout << size<< endl; 54 | 55 | Size new_size; 56 | new_size.width = src.size().width/40; 57 | new_size.height = src.size().height/40; 58 | 59 | Mat new_image = Mat(new_size, src.type()); 60 | resize(src, new_image, new_size, 0, 0, INTER_LINEAR); 61 | 62 | cout << src.size() << endl; 63 | cout << new_image.size() << endl; 64 | 65 | Mat dst = new_image.clone(); 66 | for ( int i = 1; i < 31; i = i + 2 ){ 67 | blur( new_image, dst, Size( i, i ), Point(-1, -1)); 68 | } 69 | 70 | // CvMat* mat1 = create_matrix("new image", new_image); 71 | 72 | CvMat* mat = create_matrix("blurred image", dst); 73 | 74 | /* creating a blank image (black) 75 | writing matrix onto it 76 | in progress 77 | */ 78 | Mat blank = Mat(new_image.size().width*2, new_image.size().height*2, new_image.type(), Scalar(0, 0, 0)); 79 | 80 | for(int i = 0; i < new_image.rows/2; i++){ 81 | for(int j = 0; j < new_image.cols/2; j++){ 82 | int k = i + 10; 83 | int l = j + 10; 84 | int element = CV_MAT_ELEM(*mat, int, i, j); 85 | 86 | string s = to_string(element); 87 | putText(blank, s, cvPoint(k,l), 88 | FONT_HERSHEY_COMPLEX_SMALL, 1.0, cvScalar(255, 255, 255), 1, CV_AA); 89 | } 90 | } 91 | 92 | cout << "Value is: " << CV_MAT_ELEM(*mat, int, 10, 10) << endl; 93 | 94 | imwrite("/home/kushashwaravishrimali/Documents/projects/December2k17/opencv_cpp/opencv_work/blank_image.png", blank); 95 | 96 | while(true) { 97 | imshow("new_image", new_image); 98 | imshow("dst", dst); 99 | imshow("blank", blank); 100 | 101 | int c = waitKey(20); 102 | if(c == 27) { 103 | break; 104 | } 105 | } 106 | // return 0; 107 | } -------------------------------------------------------------------------------- /Color Filtering.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "code", 5 | "execution_count": 112, 6 | "metadata": { 7 | "collapsed": true 8 | }, 9 | "outputs": [], 10 | "source": [ 11 | "try:\n", 12 | " import cv2\n", 13 | "except ImportError:\n", 14 | " print('cv2 not found')\n", 15 | "\n", 16 | "try:\n", 17 | " import matplotlib.pyplot as plt\n", 18 | "except ImportError:\n", 19 | " print('matplotlib not found')\n", 20 | " \n", 21 | "try:\n", 22 | " import numpy as np\n", 23 | "except ImportError:\n", 24 | " print('numpy not found')\n", 25 | "\n", 26 | "%matplotlib inline" 27 | ] 28 | }, 29 | { 30 | "cell_type": "code", 31 | "execution_count": 96, 32 | "metadata": { 33 | "collapsed": true 34 | }, 35 | "outputs": [], 36 | "source": [ 37 | "# utility functions\n", 38 | "def show(img):\n", 39 | " try:\n", 40 | " cv2.imshow('img', img)\n", 41 | " cv2.waitKey(0)\n", 42 | " cv2.destroyAllWindows()\n", 43 | " except ModuleNotFoundError:\n", 44 | " print('Module not found, try and import it again.')" 45 | ] 46 | }, 47 | { 48 | "cell_type": "code", 49 | "execution_count": 130, 50 | "metadata": {}, 51 | "outputs": [], 52 | "source": [ 53 | "cap = cv2.VideoCapture(0)\n", 54 | "count = 0\n", 55 | "# exit_ = 'NO'\n", 56 | "while(True):\n", 57 | " ret, frame = cap.read()\n", 58 | " if(ret == False):\n", 59 | " print(\"Unable to read frame from the camera\")\n", 60 | " \n", 61 | " hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)\n", 62 | "# (avg(hsv))\n", 63 | " if(count == 0):\n", 64 | " gap = (avg(frame[200:300, 100:300]))\n", 65 | "# print(gap)\n", 66 | " count += 1\n", 67 | " lower_red = np.array([gap[0] - 85, gap[1] - 85, gap[2] - 85])\n", 68 | " upper_red = np.array([gap[0] + 85, gap[1] + 85, gap[2] + 85])\n", 69 | " \n", 70 | " mask = cv2.inRange(hsv, lower_red, upper_red)\n", 71 | "# show(frame)\n", 72 | "# show(mask)\n", 73 | " rest = cv2.bitwise_and(frame, frame, mask = mask)\n", 74 | " \n", 75 | " cv2.imshow('frame', frame[200:300, 100:300])\n", 76 | " cv2.imshow('rest', rest)\n", 77 | " cv2.imshow('mask', mask)\n", 78 | " \n", 79 | " k = cv2.waitKey(5) & 0xFF\n", 80 | " if(k == 27):\n", 81 | " break\n", 82 | "# print(mask)\n", 83 | " \n", 84 | "# exit_ = str(input())\n", 85 | "cv2.destroyAllWindows()\n", 86 | "cap.release()" 87 | ] 88 | }, 89 | { 90 | "cell_type": "code", 91 | "execution_count": 129, 92 | "metadata": { 93 | "collapsed": true 94 | }, 95 | "outputs": [], 96 | "source": [ 97 | "cap.release()" 98 | ] 99 | }, 100 | { 101 | "cell_type": "code", 102 | "execution_count": 113, 103 | "metadata": { 104 | "collapsed": true 105 | }, 106 | "outputs": [], 107 | "source": [ 108 | "def avg(arr):\n", 109 | " count = 0\n", 110 | " sum_x = 0\n", 111 | " sum_y = 0\n", 112 | " sum_z = 0\n", 113 | "# count = \n", 114 | " for i in range(img.shape[0]):\n", 115 | " for j in range(img.shape[1]):\n", 116 | "# print(img[i][j])\n", 117 | " count += 1\n", 118 | " sum_x += img[i][j][0]\n", 119 | " sum_y += img[i][j][1]\n", 120 | " sum_z += img[i][j][2]\n", 121 | "# print(sum_x)\n", 122 | "# print(count)\n", 123 | " \n", 124 | " return(sum_x/count, sum_y/count, sum_z/count)\n", 125 | "# for i in img:\n", 126 | "# for j in i:\n", 127 | "# print(j[0])\n", 128 | "# sum_x += j[0]\n", 129 | "# sum_x += j[0]\n", 130 | "# print(j[0][0])\n", 131 | "# print(sum_x/len(arr))\n", 132 | "# sum_x = 0\n", 133 | "# sum_y = 0\n", 134 | "# sum_z = 0\n", 135 | "# for i in arr[0]:\n", 136 | "# sum_x += i\n", 137 | "# for i in arr[1]:\n", 138 | "# sum_y += i\n", 139 | "# for i in arr[2]:\n", 140 | "# sum_z += i\n", 141 | "# print(sum_x)\n", 142 | "# print(sum_y)\n", 143 | "# print(sum_z)" 144 | ] 145 | }, 146 | { 147 | "cell_type": "code", 148 | "execution_count": null, 149 | "metadata": { 150 | "collapsed": true 151 | }, 152 | "outputs": [], 153 | "source": [] 154 | } 155 | ], 156 | "metadata": { 157 | "kernelspec": { 158 | "display_name": "Python 3", 159 | "language": "python", 160 | "name": "python3" 161 | }, 162 | "language_info": { 163 | "codemirror_mode": { 164 | "name": "ipython", 165 | "version": 3 166 | }, 167 | "file_extension": ".py", 168 | "mimetype": "text/x-python", 169 | "name": "python", 170 | "nbconvert_exporter": "python", 171 | "pygments_lexer": "ipython3", 172 | "version": "3.6.1" 173 | } 174 | }, 175 | "nbformat": 4, 176 | "nbformat_minor": 2 177 | } 178 | -------------------------------------------------------------------------------- /background_subtraction.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | #include 8 | #include 9 | 10 | using namespace cv; 11 | using namespace std; 12 | 13 | // Global variables 14 | Mat frame; 15 | Mat fgMaskMOG2; // foreground mask generated by MOG2 method 16 | Ptr pMOG2; // MOG2 Background Subtractor 17 | 18 | void help(); 19 | void processVideo(char* videoFileName); 20 | void processImages(char* firstframeFilename); 21 | 22 | void help(){ 23 | cout 24 | << "--------------------------------------------------------------------------" << endl 25 | << " OpenCV. You can process both videos (-vid) and images (-img)." << endl 26 | << "Usage:" << endl 27 | << "./bg_sub {-vid