├── Chapter02 ├── CheckOpenCVBuildInfo │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CheckOpenCVBuildInfoPy │ └── CheckOpenCVBuildInfoPy.py ├── HelloOpenCV │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp └── HelloOpenCVPy │ └── HelloOpenCVPy.py ├── Chapter03 ├── CvDft │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvDftPy │ └── CvDftPy.py ├── CvLut │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp └── CvLutPy │ └── CvLutPy.py ├── Chapter04 ├── CvDraw │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp └── ResizableCircle │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── Chapter05 ├── CvHistBarChartGray │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvHistCompare │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvHistGraphColor │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvHistGraphGray │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvHueVisualizer │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp └── CvShiftBlue │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── Chapter06 ├── CvBackSegm │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvMouseTrack │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvTrack │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp └── CvTrackKalman │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── Chapter07 ├── CvCanny │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvContours │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvFeature │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvGFTT │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvHarris │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvHoughLines │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvLineDetect │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp └── CvTemplateMatch │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── Chapter08 ├── CvCascade │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── CvDNN │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp └── CvSVM │ ├── .gitignore │ ├── CMakeLists.txt │ └── main.cpp ├── Circles.png ├── FilterTest.png ├── LICENSE ├── Mask.png ├── Overlay.png ├── README.md ├── Sign.png ├── Test.png └── Test2.png /Chapter02/CheckOpenCVBuildInfo/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter02/CheckOpenCVBuildInfo/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CheckOpenCVBuildInfo) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter02/CheckOpenCVBuildInfo/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | cout << cv::getBuildInformation(); 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /Chapter02/CheckOpenCVBuildInfoPy/CheckOpenCVBuildInfoPy.py: -------------------------------------------------------------------------------- 1 | # To be able to omit cv2 in code, use the following import statement (not recommended) 2 | #from cv2 import * 3 | 4 | # Otherwise use the following 5 | import cv2 6 | 7 | print(cv2.getBuildInformation()) -------------------------------------------------------------------------------- /Chapter02/HelloOpenCV/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter02/HelloOpenCV/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(HelloOpenCV) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter02/HelloOpenCV/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | Mat image = imread("Test.png"); 10 | if(!image.empty()) 11 | { 12 | imshow("image", image); 13 | waitKey(); 14 | } 15 | else 16 | { 17 | cout << "Empty image!" << endl; 18 | } 19 | 20 | return 0; 21 | } 22 | -------------------------------------------------------------------------------- /Chapter02/HelloOpenCVPy/HelloOpenCVPy.py: -------------------------------------------------------------------------------- 1 | # To be able to omit cv2 in code, use the following import statement (not recommended) 2 | #from cv2 import * 3 | 4 | # Otherwise use the following 5 | import cv2 6 | 7 | image = cv2.imread("Test.png") 8 | if image is not None : 9 | cv2.imshow("image", image) 10 | cv2.waitKey() 11 | 12 | else: 13 | print("Empty image!") -------------------------------------------------------------------------------- /Chapter03/CvDft/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter03/CvDft/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvDft) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter03/CvDft/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | 10 | // read image grayscale 11 | Mat image = imread("Test.png", IMREAD_GRAYSCALE); 12 | if(image.empty()) 13 | { 14 | cout << "Epmty image!"; 15 | return -1; // error 16 | } 17 | 18 | // calculate optimal size for DFT calculation 19 | int optRows = getOptimalDFTSize( image.rows ); 20 | int optCols = getOptimalDFTSize( image.cols ); 21 | 22 | // resize and add zero (black) pixels 23 | Mat resizedImg; 24 | copyMakeBorder(image, 25 | resizedImg, 26 | 0, 27 | optRows - image.rows, 28 | 0, 29 | optCols - image.cols, 30 | BORDER_CONSTANT, 31 | Scalar::all(0)); 32 | 33 | /* create two channels, one containing the resize image 34 | * the other containing zeros in a matrix with same size */ 35 | vector channels = {Mat_(resizedImg), 36 | Mat::zeros(resizedImg.size(), CV_32F)}; 37 | 38 | // Construct a single image using 2 channels 39 | Mat complexImage; 40 | merge(channels, complexImage); 41 | 42 | // perform DFT and save the result in the same place 43 | dft(complexImage, complexImage); 44 | 45 | /* Split the result to its channels again, 46 | * since in the complex matrix result, 47 | * first channel contains real parts and, 48 | * second channel contains imaginary parts. */ 49 | split(complexImage, channels); 50 | 51 | Mat mag; 52 | magnitude(channels[0], channels[1], mag); 53 | 54 | // switch the magnitudes to logarithmic scale 55 | mag += Scalar::all(1); 56 | log(mag, mag); 57 | 58 | // rearrange the quadrants 59 | int cx = mag.cols/2; 60 | int cy = mag.rows/2; 61 | 62 | Mat Q1(mag, Rect(0, 0, cx, cy)); 63 | Mat Q2(mag, Rect(cx, 0, cx, cy)); 64 | Mat Q3(mag, Rect(0, cy, cx, cy)); 65 | Mat Q4(mag, Rect(cx, cy, cx, cy)); 66 | 67 | Mat tmp; 68 | Q1.copyTo(tmp); 69 | Q4.copyTo(Q1); 70 | tmp.copyTo(Q4); 71 | 72 | Q2.copyTo(tmp); 73 | Q3.copyTo(Q2); 74 | tmp.copyTo(Q3); 75 | 76 | // normalize to the displayable range of values 77 | normalize(mag, mag, 0.0, 1.0, CV_MINMAX); 78 | 79 | if(!image.empty() && !mag.empty()) 80 | { 81 | imshow("image", image); 82 | imshow("output", mag); 83 | waitKey(); 84 | } 85 | else 86 | { 87 | cout << "Empty images detected!" << endl; 88 | } 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /Chapter03/CvDftPy/CvDftPy.py: -------------------------------------------------------------------------------- 1 | # To be able to omit cv2 in code, use the following import statement (not recommended) 2 | #from cv2 import * 3 | 4 | # Otherwise use the following 5 | import cv2 6 | import numpy 7 | 8 | image = cv2.imread("Test.png", cv2.IMREAD_GRAYSCALE) 9 | if image is not None: 10 | # calculate optimal size for DFT calculation 11 | optRows = cv2.getOptimalDFTSize( image.shape[0] ) 12 | optCols = cv2.getOptimalDFTSize( image.shape[1] ) 13 | 14 | # resize and add zero (black) pixels 15 | resizedImg = cv2.copyMakeBorder(image, 16 | 0, 17 | optRows - image.shape[0], 18 | 0, 19 | optCols - image.shape[1], 20 | cv2.BORDER_CONSTANT, 21 | 0, 22 | 0) 23 | 24 | # create two channels, one containing the resize image 25 | # the other containing zeros in a matrix with same size 26 | channels = [ numpy.mat(resizedImg, numpy.float) , numpy.zeros(resizedImg.shape, numpy.float) ] 27 | 28 | # Construct a single image using 2 channels 29 | complexImg = cv2.merge( channels ) 30 | 31 | # perform DFT and save the result in the same place 32 | cv2.dft(complexImg, complexImg) 33 | 34 | # Split the result to its channels again, 35 | # since in the complex matrix result, 36 | # first channel contains real parts and, 37 | # second channel contains imaginary parts. 38 | cv2.split(complexImg, channels) 39 | 40 | mag = cv2.magnitude(channels[0], channels[1]) 41 | 42 | # switch the magnitudes to logarithmic scale 43 | mag += 1 44 | cv2.log(mag, mag) 45 | 46 | # rearrange the quadrants 47 | cx = int(mag.shape[1] / 2) 48 | cy = int(mag.shape[0] / 2) 49 | 50 | mag_rows, mag_cols = mag.shape 51 | # crop the spectrum, if it has an odd number of rows or columns 52 | mag = mag[0:(mag_rows & -2), 0:(mag_cols & -2)] 53 | cx = int(mag_rows/2) 54 | cy = int(mag_cols/2) 55 | 56 | Q1 = mag[0:cx, 0:cy] 57 | Q2 = mag[cx:cx+cx, 0:cy] 58 | Q3 = mag[0:cx, cy:cy+cy] 59 | Q4 = mag[cx:cx+cx, cy:cy+cy] 60 | 61 | tmp = numpy.copy(Q1) 62 | mag[0:cx, 0:cy] = Q4 63 | mag[cx:cx + cx, cy:cy + cy] = tmp 64 | 65 | tmp = numpy.copy(Q2) 66 | mag[cx:cx + cx, 0:cy] = Q3 67 | mag[0:cx, cy:cy + cy] = tmp 68 | 69 | 70 | 71 | # normalize to the displayable range of values 72 | cv2.normalize(mag, mag, 0.0, 1.0, cv2.NORM_MINMAX) 73 | 74 | if mag is not None: 75 | cv2.imshow("image", image) 76 | cv2.imshow("output", mag) 77 | cv2.waitKey() 78 | 79 | else: 80 | print("Empty image!") 81 | -------------------------------------------------------------------------------- /Chapter03/CvLut/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter03/CvLut/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvLut) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter03/CvLut/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | // read image grayscale 10 | Mat image = imread("Test.png"); 11 | 12 | if(image.empty()) 13 | { 14 | cout << "Empty input image!"; 15 | return -1; 16 | } 17 | 18 | Mat lut(1, 256, CV_8UC1); 19 | for(int i=0; i<256; i++) 20 | { 21 | if(i < 125) 22 | lut.at(0, i) = 0; 23 | else if(i > 175) 24 | lut.at(0, i) = 255; 25 | else 26 | lut.at(0, i) = i; 27 | } 28 | 29 | Mat result; 30 | LUT(image, lut, result); 31 | 32 | if(!image.empty() || !result.empty()) 33 | { 34 | imshow("image", image); 35 | imshow("output", result); 36 | waitKey(); 37 | } 38 | else 39 | { 40 | cout << "Empty images detected!" << endl; 41 | } 42 | 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /Chapter03/CvLutPy/CvLutPy.py: -------------------------------------------------------------------------------- 1 | # To be able to omit cv2 in code, use the following import statement (not recommended) 2 | #from cv2 import * 3 | 4 | # Otherwise use the following 5 | import cv2 6 | import numpy 7 | 8 | image = cv2.imread("Test.png") 9 | if image is not None : 10 | 11 | lut = numpy.zeros((1, 256), numpy.uint8) 12 | for i in range(0, 255): 13 | if i < 125: 14 | lut[0, i] = 0 15 | elif i > 175: 16 | lut[0, i] = 255 17 | else : 18 | lut[0, i] = i 19 | 20 | result = cv2.LUT(image, lut) 21 | 22 | cv2.imshow("image", result) 23 | cv2.waitKey() 24 | 25 | else: 26 | print("Empty image!") -------------------------------------------------------------------------------- /Chapter04/CvDraw/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter04/CvDraw/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvDraw) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter04/CvDraw/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | Mat image = imread("Test.png"); 10 | if(image.empty()) 11 | { 12 | cout << "Empty input image!"; 13 | return -1; 14 | } 15 | 16 | // ***************************** Arrowed line ***************************** 17 | Point pt1(25, image.rows/2); 18 | Point pt2(image.cols/2 - 25, image.rows/2); 19 | Scalar color = Scalar(0,255,0); 20 | int thickness = 5; 21 | int lineType = LINE_AA; 22 | int shift = 0; 23 | double tipLength = 0.2; 24 | 25 | arrowedLine(image, 26 | pt1, 27 | pt2, 28 | color, 29 | thickness, 30 | lineType, 31 | shift, 32 | tipLength); 33 | 34 | // ***************************** Circle ***************************** 35 | Point center(image.cols/2, 36 | image.rows/2); 37 | int radius = 200; 38 | circle(image, 39 | center, 40 | radius, 41 | color, 42 | thickness, 43 | lineType, 44 | shift); 45 | 46 | // ***************************** Rectangle ***************************** 47 | Rect rect(pt1,pt2); 48 | rectangle(image, 49 | rect, 50 | color, 51 | thickness, 52 | lineType, 53 | shift); 54 | 55 | // ***************************** Ellipse ***************************** 56 | Size axes(200, 100); 57 | double angle = 20.0; 58 | double startAngle = 0.0; 59 | double endAngle = 360.0; 60 | ellipse(image, 61 | center, 62 | axes, 63 | angle, 64 | startAngle, 65 | endAngle, 66 | color, 67 | thickness, 68 | lineType, 69 | shift); 70 | 71 | // ***************************** Polyline ***************************** 72 | vector pts; 73 | pts.push_back(Point(100, 100)); 74 | pts.push_back(Point(50, 150)); 75 | pts.push_back(Point(50, 200)); 76 | pts.push_back(Point(150, 200)); 77 | pts.push_back(Point(150, 150)); 78 | bool isClosed = true; 79 | polylines(image, 80 | pts, 81 | isClosed, 82 | color, 83 | thickness, 84 | lineType, 85 | shift); 86 | 87 | imshow("Image", image); 88 | waitKey(); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /Chapter04/ResizableCircle/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter04/ResizableCircle/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(ResizableCircle) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter04/ResizableCircle/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | string window = "Image"; 8 | string trackbar = "Radius"; 9 | 10 | Mat image = imread("Test.png"); 11 | Point center(image.cols/2, image.rows/2); 12 | int radius = 25; 13 | Scalar color = Scalar(0, 255, 0); 14 | int thickness = 2; 15 | LineTypes lineType = LINE_AA; 16 | int shift = 0; 17 | 18 | void drawCircle(int, void*) 19 | { 20 | Mat temp = image.clone(); 21 | 22 | circle(temp, 23 | center, 24 | radius, 25 | color, 26 | thickness, 27 | lineType, 28 | shift); 29 | 30 | imshow(window, temp); 31 | } 32 | 33 | int main() 34 | { 35 | namedWindow(window); 36 | 37 | createTrackbar(trackbar, 38 | window, 39 | &radius, 40 | min(image.rows, image.cols) / 2, 41 | drawCircle); 42 | 43 | setTrackbarMin(trackbar, window, 25); 44 | setTrackbarMax(trackbar, window, min(image.rows, image.cols) / 2); 45 | 46 | drawCircle(0,0); 47 | waitKey(); 48 | 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /Chapter05/CvHistBarChartGray/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter05/CvHistBarChartGray/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvHistBarChartGray) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter05/CvHistBarChartGray/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | Mat image = imread("Test.png"); 10 | if(image.empty()) 11 | { 12 | cout << "Empty input image!"; 13 | return -1; 14 | } 15 | 16 | Mat grayImg; 17 | cvtColor(image, grayImg, COLOR_BGR2GRAY); 18 | 19 | int bins = 30; 20 | int nimages = 1; 21 | int channels[] = {0}; 22 | Mat mask; 23 | int dims = 1; 24 | int histSize[] = { bins }; 25 | float rangeGS[] = {0, 256}; 26 | const float* ranges[] = { rangeGS }; 27 | bool uniform = true; 28 | bool accumulate = false; 29 | Mat histogram; 30 | calcHist(&grayImg, 31 | nimages, 32 | channels, 33 | mask, 34 | histogram, 35 | dims, 36 | histSize, 37 | ranges, 38 | uniform, 39 | accumulate); 40 | 41 | double maxVal = 0; 42 | minMaxLoc(histogram, 43 | 0, 44 | &maxVal, 45 | 0, 46 | 0); 47 | 48 | int gRows = 533; // height 49 | int gCols = 800; // width 50 | Scalar backGroundColor = Scalar(0, 255, 255); // yellow 51 | Scalar graphColor = Scalar(0, 0, 0); // black 52 | LineTypes lineType = LINE_AA; 53 | 54 | Mat theGraph(gRows, gCols, CV_8UC(3), backGroundColor); 55 | 56 | Point p1(0,0), p2(0, theGraph.rows-1); 57 | for(int i=0; i(i,0); 60 | value *= 0.95f; // 5% empty at top 61 | value = maxVal - value; // invert 62 | value = value / (maxVal) * theGraph.rows; // scale 63 | p1.y = value; 64 | p2.x = float(i+1) * float(theGraph.cols) / float(bins); 65 | rectangle(theGraph, 66 | p1, 67 | p2, 68 | graphColor, 69 | CV_FILLED, 70 | lineType); 71 | p1.x = p2.x; 72 | } 73 | 74 | imshow("Image", grayImg); 75 | imshow("Histogram", theGraph); 76 | waitKey(); 77 | 78 | return 0; 79 | } 80 | -------------------------------------------------------------------------------- /Chapter05/CvHistCompare/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter05/CvHistCompare/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvHistCompare) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter05/CvHistCompare/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | void getHistogram(const Mat &grayImg, Mat &histogram) 8 | { 9 | int bins = 256; 10 | int nimages = 1; 11 | int channels[] = {0}; 12 | Mat mask; 13 | int dims = 1; 14 | int histSize[] = { bins }; 15 | float rangeGS[] = {0, 256}; 16 | const float* ranges[] = { rangeGS }; 17 | bool uniform = true; 18 | bool accumulate = false; 19 | calcHist(&grayImg, 20 | nimages, 21 | channels, 22 | mask, 23 | histogram, 24 | dims, 25 | histSize, 26 | ranges, 27 | uniform, 28 | accumulate); 29 | } 30 | 31 | int main() 32 | { 33 | Mat image1 = imread("Test.png"); 34 | Mat image2 = imread("Test2.png"); 35 | if(image1.empty() || image2.empty()) 36 | { 37 | cout << "Empty input image!"; 38 | return -1; 39 | } 40 | 41 | Mat grayImg1, grayImg2, histogram1, histogram2; 42 | cvtColor(image1, grayImg1, COLOR_BGR2GRAY); 43 | cvtColor(image2, grayImg2, COLOR_BGR2GRAY); 44 | 45 | getHistogram(grayImg2, histogram1); 46 | getHistogram(grayImg1, histogram2); 47 | 48 | cout << "HISTCMP_CORREL: " << compareHist(histogram1, histogram2, HISTCMP_CORREL) << endl; 49 | cout << "HISTCMP_CHISQR: " << compareHist(histogram1, histogram2, HISTCMP_CHISQR) << endl; 50 | cout << "HISTCMP_INTERSECT: " << compareHist(histogram1, histogram2, HISTCMP_INTERSECT) << endl; 51 | cout << "HISTCMP_BHATTACHARYYA: " << compareHist(histogram1, histogram2, HISTCMP_BHATTACHARYYA) << endl; 52 | cout << "HISTCMP_HELLINGER: " << compareHist(histogram1, histogram2, HISTCMP_HELLINGER) << endl; 53 | cout << "HISTCMP_CHISQR_ALT: " << compareHist(histogram1, histogram2, HISTCMP_CHISQR_ALT) << endl; 54 | cout << "HISTCMP_KL_DIV: " << compareHist(histogram1, histogram2, HISTCMP_KL_DIV) << endl; 55 | 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /Chapter05/CvHistGraphColor/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter05/CvHistGraphColor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvHistGraphColor) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter05/CvHistGraphColor/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | Mat image = imread("Test.png"); 10 | if(image.empty()) 11 | { 12 | cout << "Empty input image!"; 13 | return -1; 14 | } 15 | 16 | Mat imgChannels[3]; 17 | Mat histograms[3]; 18 | split(image, imgChannels); 19 | 20 | int bins = 256; 21 | int nimages = 1; 22 | int channels[] = {0}; 23 | Mat mask; 24 | int dims = 1; 25 | int histSize[] = { bins }; 26 | float rangeGS[] = {0, 256}; 27 | const float* ranges[] = { rangeGS }; 28 | bool uniform = true; 29 | bool accumulate = false; 30 | 31 | int gRows = 533; // height 32 | int gCols = 800; // width 33 | Scalar backGroundColor = Scalar(0, 0, 0); 34 | int thickness = 3; 35 | LineTypes lineType = LINE_AA; 36 | 37 | Mat theGraph(gRows, gCols, CV_8UC(3), backGroundColor); 38 | 39 | for(int i=0; i<3; i++) 40 | { 41 | calcHist(&imgChannels[i], 42 | nimages, 43 | channels, 44 | mask, 45 | histograms[i], 46 | dims, 47 | histSize, 48 | ranges, 49 | uniform, 50 | accumulate); 51 | 52 | double maxVal = 0; 53 | minMaxLoc(histograms[i], 54 | 0, 55 | &maxVal, 56 | 0, 57 | 0); 58 | 59 | 60 | Scalar graphColor = Scalar(i == 0 ? 255 : 0, 61 | i == 1 ? 255 : 0, 62 | i == 2 ? 255 : 0); 63 | 64 | Point p1(0,0), p2(0,0); 65 | for(int j=0; j(j,0); 68 | value = maxVal - value; // invert 69 | value = value / maxVal * theGraph.rows; // scale 70 | line(theGraph, 71 | p1, 72 | Point(p1.x,value), 73 | graphColor, 74 | thickness, 75 | lineType); 76 | p1.y = p2.y = value; 77 | p2.x = float(j+1) * float(theGraph.cols) / float(bins); 78 | line(theGraph, 79 | p1, p2, 80 | Scalar(0,0,0)); 81 | p1.x = p2.x; 82 | } 83 | } 84 | 85 | imshow("Image", image); 86 | imshow("Histogram", theGraph); 87 | 88 | waitKey(); 89 | 90 | return 0; 91 | } 92 | -------------------------------------------------------------------------------- /Chapter05/CvHistGraphGray/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter05/CvHistGraphGray/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvHistGraphGray) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter05/CvHistGraphGray/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | int main() 8 | { 9 | Mat image = imread("Test.png"); 10 | if(image.empty()) 11 | { 12 | cout << "Empty input image!"; 13 | return -1; 14 | } 15 | 16 | Mat grayImg; 17 | cvtColor(image, grayImg, COLOR_BGR2GRAY); 18 | 19 | int bins = 256; 20 | int nimages = 1; 21 | int channels[] = {0}; 22 | Mat mask; 23 | int dims = 1; 24 | int histSize[] = { bins }; 25 | float rangeGS[] = {0, 256}; 26 | const float* ranges[] = { rangeGS }; 27 | bool uniform = true; 28 | bool accumulate = false; 29 | Mat histogram; 30 | calcHist(&grayImg, 31 | nimages, 32 | channels, 33 | mask, 34 | histogram, 35 | dims, 36 | histSize, 37 | ranges, 38 | uniform, 39 | accumulate); 40 | 41 | double maxVal = 0; 42 | minMaxLoc(histogram, 43 | 0, 44 | &maxVal, 45 | 0, 46 | 0); 47 | 48 | int gRows = 533; // height 49 | int gCols = 800; // width 50 | Scalar backGroundColor = Scalar(0, 255, 255); // yellow 51 | Scalar graphColor = Scalar(0, 0, 0); // black 52 | int thickness = 2; 53 | LineTypes lineType = LINE_AA; 54 | 55 | Mat theGraph(gRows, gCols, CV_8UC(3), backGroundColor); 56 | 57 | Point p1(0,0), p2(0,0); 58 | for(int i=0; i(i,0); 61 | value = maxVal - value; // invert 62 | value = value / maxVal * theGraph.rows; // scale 63 | line(theGraph, 64 | p1, 65 | Point(p1.x,value), 66 | graphColor, 67 | thickness, 68 | lineType); 69 | p1.y = p2.y = value; 70 | p2.x = float(i+1) * float(theGraph.cols) / float(bins); 71 | line(theGraph, 72 | p1, p2, 73 | Scalar(0,0,0)); 74 | p1.x = p2.x; 75 | } 76 | 77 | imshow("Image", grayImg); 78 | imshow("Histogram", theGraph); 79 | waitKey(); 80 | 81 | return 0; 82 | } 83 | -------------------------------------------------------------------------------- /Chapter05/CvHueVisualizer/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter05/CvHueVisualizer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvHueVisualizer) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter05/CvHueVisualizer/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | int main() 7 | { 8 | Mat image = imread("Test2.png"); 9 | if(image.empty()) 10 | { 11 | cout << "Empty input image!"; 12 | return -1; 13 | } 14 | 15 | Mat imgHsv, hue; 16 | vector hsvChannels; 17 | cvtColor(image, imgHsv, COLOR_BGR2HSV); 18 | split(imgHsv, hsvChannels); 19 | hue = hsvChannels[0]; 20 | 21 | int bins = 36; 22 | int histSize[] = {bins}; 23 | int nimages = 1; 24 | int dims = 1; 25 | int channels[] = {0}; 26 | float rangeHue[] = {0, 180}; 27 | const float* ranges[] = {rangeHue}; 28 | bool uniform = true; 29 | bool accumulate = false; 30 | Mat histogram, mask; 31 | 32 | calcHist(&hue, 33 | nimages, 34 | channels, 35 | mask, 36 | histogram, 37 | dims, 38 | histSize, 39 | ranges, 40 | uniform, 41 | accumulate); 42 | 43 | double maxVal; 44 | minMaxLoc(histogram, 45 | 0, 46 | &maxVal, 47 | 0, 48 | 0); 49 | 50 | int gW = 800, gH = 100; 51 | Mat theGraph(gH, gW, CV_8UC3, Scalar::all(0)); 52 | 53 | Mat colors(1, bins, CV_8UC3); 54 | for(int i=0; i(i) = 57 | Vec3b(saturate_cast( 58 | (i+1)*180.0/bins), 255, 255); 59 | } 60 | cvtColor(colors, colors, COLOR_HSV2BGR); 61 | Point p1(0,0), p2(0,theGraph.rows-1); 62 | for(int i=0; i(i,0); 65 | value = maxVal - value; // invert 66 | value = value / maxVal * theGraph.rows; // scale 67 | p1.y = value; 68 | p2.x = float(i+1) * float(theGraph.cols) / float(bins); 69 | rectangle(theGraph, 70 | p1, 71 | p2, 72 | Scalar(colors.at(i)), 73 | CV_FILLED); 74 | p1.x = p2.x; 75 | } 76 | 77 | imshow("Image", image); 78 | imshow("Graph", theGraph); 79 | 80 | waitKey(); 81 | 82 | return 0; 83 | } 84 | -------------------------------------------------------------------------------- /Chapter05/CvShiftBlue/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter05/CvShiftBlue/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1) 2 | 3 | project(CvShiftBlue) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter05/CvShiftBlue/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | int main() 7 | { 8 | Mat image = imread("Test2.png"); 9 | if(image.empty()) 10 | { 11 | cout << "Empty input image!"; 12 | return -1; 13 | } 14 | 15 | Mat imgHsv, hue; 16 | vector hsvChannels; 17 | cvtColor(image, imgHsv, COLOR_BGR2HSV); 18 | split(imgHsv, hsvChannels); 19 | hue = hsvChannels[0]; 20 | 21 | int bins = 360; 22 | int blueHue = 240; 23 | int hueOffset = 50; 24 | Mat histogram(bins, 1, CV_32FC1); 25 | for(int i=0; i(i, 0) = (i > blueHue - hueOffset) && (i < blueHue + hueOffset) ? 255.0 : 0.0; 28 | } 29 | 30 | double maxVal = 255.0; 31 | 32 | int gW = 800, gH = 100; 33 | Mat theGraph(gH, gW, CV_8UC3, Scalar::all(0)); 34 | 35 | Mat colors(1, bins, CV_8UC3); 36 | for(int i=0; i(i) = 39 | Vec3b(saturate_cast( 40 | (i+1)*180.0/bins), 255, 255); 41 | } 42 | cvtColor(colors, colors, COLOR_HSV2BGR); 43 | Point p1(0,0), p2(0,theGraph.rows-1); 44 | for(int i=0; i(i,0); 47 | value = maxVal - value; // invert 48 | value = value / maxVal * theGraph.rows; // scale 49 | p1.y = value; 50 | p2.x = float(i+1) * float(theGraph.cols) / float(bins); 51 | rectangle(theGraph, 52 | p1, 53 | p2, 54 | Scalar(colors.at(i)), 55 | CV_FILLED); 56 | p1.x = p2.x; 57 | } 58 | 59 | int nimages = 1; 60 | int channels[] = {0}; 61 | Mat backProject; 62 | float rangeHue[] = {0, 180}; 63 | const float* ranges[] = {rangeHue}; 64 | double scale = 1.0; 65 | bool uniform = true; 66 | calcBackProject(&hue, 67 | nimages, 68 | channels, 69 | histogram, 70 | backProject, 71 | ranges, 72 | scale, 73 | uniform); 74 | 75 | int shift = -50; 76 | for(int i=0; i(i, j)) 81 | { 82 | imgHsv.at(i,j)[0] += shift; 83 | } 84 | } 85 | } 86 | Mat imgHueShift; 87 | cvtColor(imgHsv, imgHueShift, CV_HSV2BGR); 88 | 89 | imshow("Image", image); 90 | imshow("Graph", theGraph); 91 | imshow("Backproj", backProject); 92 | imshow("Hue SHift", imgHueShift); 93 | 94 | waitKey(); 95 | 96 | return 0; 97 | 98 | } 99 | -------------------------------------------------------------------------------- /Chapter06/CvBackSegm/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter06/CvBackSegm/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvBackSegm) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter06/CvBackSegm/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | int main() 7 | { 8 | VideoCapture cam(0); 9 | if(!cam.isOpened()) 10 | return -1; 11 | 12 | int history = 500; 13 | double dist2Threshold = 400.0; 14 | bool detectShadows = true; 15 | Ptr bgs = 16 | createBackgroundSubtractorKNN(history, 17 | dist2Threshold, 18 | detectShadows); 19 | 20 | while(true) 21 | { 22 | Mat frame; 23 | cam >> frame; 24 | if(frame.empty()) 25 | break; 26 | 27 | Mat fgMask; 28 | bgs->apply(frame, 29 | fgMask); 30 | 31 | medianBlur(fgMask,fgMask,3); 32 | 33 | Mat fg; 34 | bitwise_and(frame, frame, fg, fgMask); 35 | 36 | Mat bg; 37 | bgs->getBackgroundImage(bg); 38 | 39 | imshow("Video", frame); 40 | imshow("Background", bg); 41 | imshow("Foreground Mask", fgMask); 42 | imshow("Foreground Image", fg); 43 | 44 | int key = waitKey(10); 45 | if(key == 27) // escape key 46 | break; 47 | } 48 | 49 | cam.release(); 50 | 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /Chapter06/CvMouseTrack/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter06/CvMouseTrack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvMouseTrack) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter06/CvMouseTrack/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | Point objectPos; 7 | 8 | void onMouse(int, int x, int y, int, void*) 9 | { 10 | objectPos.x = x; 11 | objectPos.y = y; 12 | } 13 | 14 | int main() 15 | { 16 | KalmanFilter kalman(4,2); 17 | 18 | Mat_ tm(4, 4); // transition matrix 19 | tm << 1,0,1,0, // next x = 1x+0y+1x'+0y' 20 | 0,1,0,1, // next x'= 0x+1y+0x'+1y' 21 | 0,0,1,0, // next y = 0x+0y+1x'+0y 22 | 0,0,0,1; // next y'= 0x+0y+0x'+1y' 23 | kalman.transitionMatrix = tm; 24 | Mat_ pos(2,1); 25 | pos.at(0) = 0; 26 | pos.at(1) = 0; 27 | 28 | kalman.statePre.at(0) = 0; // init x 29 | kalman.statePre.at(1) = 0; // init y 30 | kalman.statePre.at(2) = 0; // init x' 31 | kalman.statePre.at(3) = 0; // init y' 32 | 33 | setIdentity(kalman.measurementMatrix); 34 | 35 | setIdentity(kalman.processNoiseCov, 36 | Scalar::all(0.000001)); 37 | 38 | string window = "Canvas"; 39 | namedWindow(window); 40 | setMouseCallback(window, onMouse); 41 | 42 | vector trackRoute; 43 | vector mouseRoute; 44 | 45 | while(waitKey(10) < 0) 46 | { 47 | // empty canvas 48 | Mat canvas(500, 1000, CV_8UC3, Scalar(255, 255, 255)); 49 | 50 | pos(0) = objectPos.x; 51 | pos(1) = objectPos.y; 52 | 53 | Mat estimation = kalman.correct(pos); 54 | 55 | Point estPt(estimation.at(0), 56 | estimation.at(1)); 57 | 58 | trackRoute.push_back(estPt); 59 | if(trackRoute.size() > 100) 60 | trackRoute.erase(trackRoute.begin()); 61 | 62 | polylines(canvas, 63 | trackRoute, 64 | false, 65 | Scalar(0,0,255), 66 | 5); 67 | 68 | mouseRoute.push_back(objectPos); 69 | if(mouseRoute.size() > 100) 70 | mouseRoute.erase(mouseRoute.begin()); 71 | polylines(canvas, 72 | mouseRoute, 73 | false, 74 | Scalar(0,0,0), 75 | 2); 76 | 77 | imshow(window, canvas); 78 | 79 | kalman.predict(); 80 | } 81 | return 0; 82 | 83 | } 84 | -------------------------------------------------------------------------------- /Chapter06/CvTrack/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter06/CvTrack/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvTrack) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter06/CvTrack/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | TermCriteria criteria(TermCriteria::MAX_ITER 8 | + TermCriteria::EPS, 9 | 20, 10 | 1.0); 11 | 12 | bool selecting = false; 13 | Rect selection; 14 | Point spo; // selection point origin 15 | 16 | void onMouse(int event, int x, int y, int, void*) 17 | { 18 | switch(event) 19 | { 20 | case EVENT_LBUTTONDOWN: 21 | { 22 | spo.x = x; 23 | spo.y = y; 24 | selection.x = spo.x; 25 | selection.y = spo.y; 26 | selection.width = 0; 27 | selection.height = 0; 28 | selecting = true; 29 | 30 | } break; 31 | case EVENT_LBUTTONUP: 32 | { 33 | selecting = false; 34 | } break; 35 | default: 36 | { 37 | selection.x = min(x, spo.x); 38 | selection.y = min(y, spo.y); 39 | selection.width = abs(x - spo.x); 40 | selection.height = abs(y - spo.y); 41 | } break; 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | VideoCapture cam(0); 48 | if(!cam.isOpened()) 49 | return -1; 50 | 51 | Rect srchWnd; 52 | 53 | string outputWindow = "Display"; 54 | namedWindow(outputWindow); 55 | cv::setMouseCallback(outputWindow, onMouse); 56 | 57 | vector route; 58 | Mat histogram, backProject, mask; 59 | 60 | int key = -1; 61 | while(key != ' ') 62 | { 63 | Mat frame; 64 | cam >> frame; 65 | if(frame.empty()) 66 | break; 67 | 68 | Mat frmHsv, hue; 69 | vector hsvChannels; 70 | cvtColor(frame, frmHsv, COLOR_BGR2HSV); 71 | split(frmHsv, hsvChannels); 72 | hue = hsvChannels[0]; 73 | 74 | int bins = 120; 75 | int nimages = 1; 76 | int channels[] = {0}; 77 | float rangeHue[] = {0, 180}; 78 | const float* ranges[] = {rangeHue}; 79 | int histSize[] = { bins }; 80 | bool uniform = true; 81 | 82 | if(selecting && selection.area() > 0) 83 | { 84 | Mat sel(frame, selection); 85 | 86 | int lbHue = 00 , hbHue = 180; 87 | int lbSat = 30 , hbSat = 256; 88 | int lbVal = 30 , hbVal = 230; 89 | 90 | inRange(frmHsv, 91 | Scalar(lbHue,lbSat,lbVal), 92 | Scalar(hbHue, hbSat, hbVal), 93 | mask); 94 | 95 | Mat roi(hue, selection); 96 | Mat maskroi(mask, selection); 97 | 98 | calcHist(&roi, 99 | nimages, 100 | channels, 101 | maskroi, 102 | histogram, 103 | 1, 104 | histSize, 105 | ranges, 106 | uniform); 107 | 108 | normalize(histogram, 109 | histogram, 0, 255, NORM_MINMAX); 110 | 111 | bitwise_not(sel, sel); 112 | 113 | srchWnd = selection; 114 | } 115 | else if(!histogram.empty()) 116 | { 117 | 118 | double scale = 1.0; 119 | calcBackProject(&hue, 120 | nimages, 121 | channels, 122 | histogram, 123 | backProject, 124 | ranges, 125 | scale, 126 | uniform); 127 | 128 | erode(backProject, 129 | backProject, 130 | Mat()); 131 | 132 | 133 | // meanShift(backProject, 134 | // srchWnd, 135 | // criteria); 136 | 137 | CamShift(backProject, 138 | srchWnd, 139 | criteria); 140 | 141 | Point p(srchWnd.x + srchWnd.width/2, 142 | srchWnd.y + srchWnd.height/2); 143 | 144 | route.push_back(p); 145 | if(route.size() > 60) 146 | route.erase(route.begin()); 147 | 148 | rectangle(frame, 149 | srchWnd, 150 | Scalar(0,0,255), 151 | 2); 152 | 153 | polylines(frame, 154 | route,false, 155 | Scalar(0,255,0), 156 | 2); 157 | 158 | cvtColor(backProject, backProject, COLOR_GRAY2BGR); 159 | rectangle(backProject, 160 | srchWnd, 161 | Scalar(0,0,255), 162 | 2); 163 | 164 | polylines(backProject, 165 | route,false, 166 | Scalar(0,255,0), 167 | 2); 168 | 169 | } 170 | 171 | switch(key) 172 | { 173 | case 'b': 174 | if(!backProject.empty()) imshow(outputWindow, backProject); 175 | else imshow(outputWindow, frame); 176 | break; 177 | case 'v': default: imshow(outputWindow, frame); 178 | break; 179 | 180 | } 181 | 182 | int k = waitKey(10); 183 | if(k > 0) 184 | key = k; 185 | } 186 | 187 | cam.release(); 188 | 189 | return 0; 190 | 191 | } 192 | -------------------------------------------------------------------------------- /Chapter06/CvTrackKalman/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter06/CvTrackKalman/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvTrackKalman) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter06/CvTrackKalman/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | #include 3 | 4 | using namespace std; 5 | using namespace cv; 6 | 7 | TermCriteria criteria(TermCriteria::MAX_ITER 8 | + TermCriteria::EPS, 9 | 20, 10 | 1.0); 11 | 12 | bool selecting = false; 13 | Rect selection; 14 | Point spo; // selection point origin 15 | 16 | void onMouse(int event, int x, int y, int, void*) 17 | { 18 | switch(event) 19 | { 20 | case EVENT_LBUTTONDOWN: 21 | { 22 | spo.x = x; 23 | spo.y = y; 24 | selection.x = spo.x; 25 | selection.y = spo.y; 26 | selection.width = 0; 27 | selection.height = 0; 28 | selecting = true; 29 | 30 | } break; 31 | case EVENT_LBUTTONUP: 32 | { 33 | selecting = false; 34 | } break; 35 | default: 36 | { 37 | selection.x = min(x, spo.x); 38 | selection.y = min(y, spo.y); 39 | selection.width = abs(x - spo.x); 40 | selection.height = abs(y - spo.y); 41 | } break; 42 | } 43 | } 44 | 45 | int main() 46 | { 47 | VideoCapture cam(0); 48 | if(!cam.isOpened()) 49 | return -1; 50 | 51 | KalmanFilter kalman(4,2); 52 | 53 | Mat_ tm(4, 4); // transition matrix 54 | tm << 1,0,1,0, // next x = 1x+0y+1x'+0y' 55 | 0,1,0,1, // next x'= 0x+1y+0x'+1y' 56 | 0,0,1,0, // next y = 0x+0y+1x'+0y 57 | 0,0,0,1; // next y'= 0x+0y+0x'+1y' 58 | kalman.transitionMatrix = tm; 59 | Mat_ pos(2,1); 60 | pos.at(0) = 0; 61 | pos.at(1) = 0; 62 | 63 | kalman.statePre.at(0) = 0; // init x 64 | kalman.statePre.at(1) = 0; // init y 65 | kalman.statePre.at(2) = 0; // init x' 66 | kalman.statePre.at(3) = 0; // init y' 67 | 68 | setIdentity(kalman.measurementMatrix); 69 | 70 | setIdentity(kalman.processNoiseCov, 71 | Scalar::all(0.00001)); 72 | 73 | Rect srchWnd; 74 | 75 | string outputWindow = "Display"; 76 | namedWindow(outputWindow); 77 | cv::setMouseCallback(outputWindow, onMouse); 78 | 79 | Mat histogram, backProject, mask; 80 | 81 | int key = -1; 82 | while(key != ' ') 83 | { 84 | Mat frame; 85 | cam >> frame; 86 | if(frame.empty()) 87 | break; 88 | 89 | Mat frmHsv, hue; 90 | vector hsvChannels; 91 | cvtColor(frame, frmHsv, COLOR_BGR2HSV); 92 | split(frmHsv, hsvChannels); 93 | hue = hsvChannels[0]; 94 | 95 | int bins = 120; 96 | int nimages = 1; 97 | int channels[] = {0}; 98 | float rangeHue[] = {0, 180}; 99 | const float* ranges[] = {rangeHue}; 100 | int histSize[] = { bins }; 101 | bool uniform = true; 102 | 103 | if(selecting && selection.area() > 0) 104 | { 105 | Mat sel(frame, selection); 106 | 107 | int lbHue = 00 , hbHue = 180; 108 | int lbSat = 30 , hbSat = 256; 109 | int lbVal = 30 , hbVal = 230; 110 | 111 | inRange(frmHsv, 112 | Scalar(lbHue,lbSat,lbVal), 113 | Scalar(hbHue, hbSat, hbVal), 114 | mask); 115 | 116 | Mat roi(hue, selection); 117 | Mat maskroi(mask, selection); 118 | 119 | calcHist(&roi, 120 | nimages, 121 | channels, 122 | maskroi, 123 | histogram, 124 | 1, 125 | histSize, 126 | ranges, 127 | uniform); 128 | 129 | normalize(histogram, 130 | histogram, 0, 255, NORM_MINMAX); 131 | 132 | bitwise_not(sel, sel); 133 | 134 | srchWnd = selection; 135 | } 136 | else if(!histogram.empty()) 137 | { 138 | 139 | double scale = 1.0; 140 | calcBackProject(&hue, 141 | nimages, 142 | channels, 143 | histogram, 144 | backProject, 145 | ranges, 146 | scale, 147 | uniform); 148 | 149 | erode(backProject, 150 | backProject, 151 | Mat()); 152 | 153 | 154 | // meanShift(backProject, 155 | // srchWnd, 156 | // criteria); 157 | 158 | CamShift(backProject, 159 | srchWnd, 160 | criteria); 161 | 162 | Point objectPos(srchWnd.x + srchWnd.width/2, 163 | srchWnd.y + srchWnd.height/2); 164 | 165 | pos(0) = objectPos.x; 166 | pos(1) = objectPos.y; 167 | 168 | Mat estimation = kalman.correct(pos); 169 | kalman.predict(); 170 | 171 | Point estPt(estimation.at(0), 172 | estimation.at(1)); 173 | 174 | drawMarker(frame, 175 | estPt, 176 | Scalar(0,255,0), 177 | MARKER_CROSS, 178 | 30, 179 | 2); 180 | 181 | cvtColor(backProject, backProject, COLOR_GRAY2BGR); 182 | drawMarker(backProject, 183 | estPt, 184 | Scalar(0,255,0), 185 | MARKER_CROSS, 186 | 30, 187 | 2); 188 | 189 | } 190 | 191 | switch(key) 192 | { 193 | case 'b': 194 | if(!backProject.empty()) imshow(outputWindow, backProject); 195 | else imshow(outputWindow, frame); 196 | break; 197 | case 'v': default: imshow(outputWindow, frame); 198 | break; 199 | 200 | } 201 | 202 | int k = waitKey(10); 203 | if(k > 0) 204 | key = k; 205 | } 206 | 207 | cam.release(); 208 | 209 | return 0; 210 | 211 | } 212 | -------------------------------------------------------------------------------- /Chapter07/CvCanny/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter07/CvCanny/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvCanny) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter07/CvCanny/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | int main() 7 | { 8 | Mat image = imread("Test.png"); 9 | Mat imgGray; 10 | cvtColor(image, imgGray, COLOR_BGR2GRAY); 11 | 12 | double threshold1 = 100.0; 13 | double threshold2 = 200.0; 14 | int apertureSize = 3; 15 | bool L2gradient = false; 16 | Mat edges; 17 | Canny(image, 18 | edges, 19 | threshold1, 20 | threshold2, 21 | apertureSize, 22 | L2gradient); 23 | 24 | imshow("image", image); 25 | imshow("edges", edges); 26 | 27 | waitKey(); 28 | 29 | return 0; 30 | } 31 | -------------------------------------------------------------------------------- /Chapter07/CvContours/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter07/CvContours/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvContours) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter07/CvContours/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | int main() 7 | { 8 | Mat image = imread("Test.png"); 9 | Mat imgGray; 10 | cvtColor(image, imgGray, COLOR_BGR2GRAY); 11 | 12 | double threshold1 = 100.0; 13 | double threshold2 = 200.0; 14 | int apertureSize = 3; 15 | bool L2gradient = false; 16 | Mat edges; 17 | Canny(image, 18 | edges, 19 | threshold1, 20 | threshold2, 21 | apertureSize, 22 | L2gradient); 23 | 24 | vector > contours; 25 | findContours(edges, 26 | contours, 27 | CV_RETR_TREE, 28 | CV_CHAIN_APPROX_SIMPLE, 29 | Point(0, 0)); 30 | 31 | RNG rng(12345); // any random number 32 | 33 | Mat result(edges.size(), CV_8UC3, Scalar(0)); 34 | int thickness = 2; 35 | for( int i = 0; i< contours.size(); i++ ) 36 | { 37 | Scalar color = Scalar(rng.uniform(0, 255), 38 | rng.uniform(0,255), 39 | rng.uniform(0,255) ); 40 | 41 | drawContours(result, 42 | contours, 43 | i, 44 | color, 45 | thickness); 46 | } 47 | 48 | imshow("image", image); 49 | imshow("result", result); 50 | imshow("edges", edges); 51 | 52 | waitKey(); 53 | 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /Chapter07/CvFeature/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter07/CvFeature/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvFeature) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter07/CvFeature/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | 7 | 8 | int main() 9 | { 10 | double matchThresh; 11 | 12 | // ***** Read images 13 | Mat object = imread("TestObject.png"); 14 | Mat scene = imread("TestScene.png"); 15 | 16 | // ***** Create the algorithm accordingly 17 | int algorithm = 1; 18 | Ptr detector; 19 | switch(algorithm) 20 | { 21 | 22 | case 1: 23 | detector = BRISK::create(); 24 | matchThresh = 350; 25 | break; 26 | 27 | case 2: 28 | detector = KAZE::create(); 29 | matchThresh = 0.1; 30 | break; 31 | 32 | case 3: 33 | detector = AKAZE::create(); 34 | matchThresh = 250; 35 | break; 36 | 37 | } 38 | 39 | // ***** Detect features 40 | vector objKPs, scnKPs; 41 | detector->detect(object, objKPs); 42 | detector->detect(scene, scnKPs); 43 | 44 | // ***** Extract descriptors 45 | Mat objDesc, scnDesc; 46 | detector->compute(object, objKPs, objDesc); 47 | detector->compute(scene, scnKPs, scnDesc); 48 | 49 | // ***** Match descriptors 50 | Ptr matcher = BFMatcher::create(); 51 | vector matches; 52 | matcher->match(objDesc, scnDesc, matches); 53 | 54 | // ***** Remove "bad" matches 55 | vector goodMatches; 56 | for(int i=0; i 0) 63 | { 64 | cout << "Found " << goodMatches.size() << " good matches."; 65 | } 66 | else 67 | { 68 | cout << "Didn't find a single good match. Quitting!"; 69 | return -1; 70 | } 71 | 72 | // ***** Draw matches 73 | Mat result; 74 | drawMatches(object, 75 | objKPs, 76 | scene, 77 | scnKPs, 78 | goodMatches, 79 | result, 80 | Scalar(0, 255, 0), // green for matched 81 | Scalar::all(-1), // unmatched color (default) 82 | vector(), // empty mask 83 | DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); 84 | 85 | // ***** Find the homography change between object and scene 86 | vector goodP1, goodP2; 87 | for(int i=0; i corners1(4), corners2(4); 96 | corners1[0] = Point2f(0,0); 97 | corners1[1] = Point2f(object.cols-1, 0); 98 | corners1[2] = Point2f(object.cols-1, object.rows-1); 99 | corners1[3] = Point2f(0, object.rows-1); 100 | perspectiveTransform(corners1, corners2, homoChange); 101 | 102 | // ***** Correct the coordinates to fir the drawMatches result image 103 | for(int i=0; i<4; i++) 104 | corners2[i].x += object.cols; 105 | 106 | // ***** Draw the bounding lines 107 | line(result, corners2[0], corners2[1], Scalar::all(255), 2); 108 | line(result, corners2[1], corners2[2], Scalar::all(255), 2); 109 | line(result, corners2[2], corners2[3], Scalar::all(255), 2); 110 | line(result, corners2[3], corners2[0], Scalar::all(255), 2); 111 | 112 | imshow("Result", result); 113 | waitKey(); 114 | 115 | return 0; 116 | } 117 | -------------------------------------------------------------------------------- /Chapter07/CvGFTT/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter07/CvGFTT/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvGFTT) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter07/CvGFTT/main.cpp: -------------------------------------------------------------------------------- 1 | #include "opencv2/opencv.hpp" 2 | 3 | using namespace std; 4 | using namespace cv; 5 | 6 | int main() 7 | { 8 | Mat image = imread("Test.png"); 9 | Mat imgGray; 10 | cvtColor(image, imgGray, COLOR_BGR2GRAY); 11 | 12 | vector corners; 13 | int maxCorners = 500; 14 | double qualityLevel = 0.01; 15 | double minDistance = 10; 16 | Mat mask; 17 | int blockSize = 3; 18 | int gradientSize = 3; 19 | bool useHarrisDetector = false; 20 | double k = 0.04; 21 | goodFeaturesToTrack(imgGray, 22 | corners, 23 | maxCorners, 24 | qualityLevel, 25 | minDistance, 26 | mask, 27 | blockSize, 28 | gradientSize, 29 | useHarrisDetector, 30 | k); 31 | 32 | Scalar color(0, 0, 255); 33 | MarkerTypes markerType = MARKER_TILTED_CROSS; 34 | int markerSize = 8; 35 | int thickness = 2; 36 | for(int i=0; i lines; 23 | double rho = 1.0; // 1 pixel, r resolution 24 | double theta = CV_PI / 180.0; // 1 degree, theta resolution 25 | int threshold = 100; // minimum number of intersections to "detect" a line 26 | HoughLines(edges, 27 | lines, 28 | rho, 29 | theta, 30 | threshold); 31 | 32 | Scalar color(0,0,255); 33 | int thickness = 2; 34 | for(int i=0; i detector = createLineSegmentDetector(); 13 | 14 | vector lines; 15 | detector->detect(imgGray, 16 | lines); 17 | 18 | Mat result(image.size(), 19 | CV_8UC3, 20 | Scalar(0, 0, 0)); 21 | 22 | // detector->drawSegments(result, 23 | // lines); 24 | 25 | // or the following 26 | 27 | Scalar color(0,0,255); 28 | int thickness = 2; 29 | for(int i=0; i> frame; 32 | if(frame.empty()) 33 | break; 34 | 35 | vector faces; 36 | 37 | double t = (double)getTickCount(); 38 | 39 | faceDetector.detectMultiScale(frame, 40 | faces); 41 | 42 | t = ((double)getTickCount() - t)/getTickFrequency(); 43 | t *= 1000; // convert to ms 44 | 45 | for(int i=0; i labels = { "person", "bicycle" , "car"}; 14 | 15 | int main() 16 | { 17 | Net network = readNetFromTensorflow("frozen_inference_graph.pb", 18 | "frozen_inference_graph.pbtxt"); 19 | if(network.empty()) 20 | { 21 | cout << "Can't read TensorFlow model." << endl; 22 | return -1; 23 | } 24 | 25 | VideoCapture cam; 26 | if(!cam.open(0)) 27 | { 28 | cout << "Can't open camera." << endl; 29 | return -1; 30 | } 31 | 32 | Scalar green = Scalar(0,255,0); 33 | Scalar white = Scalar(255,255,255); 34 | Scalar red = Scalar(0,0,255); 35 | int thickness = 2; 36 | double scale = 0.75; 37 | 38 | while(true) 39 | { 40 | Mat frame; 41 | cam >> frame; 42 | if(frame.empty()) 43 | break; 44 | 45 | const int inWidth = 300; 46 | const int inHeight = 300; 47 | const float meanVal = 127.5; // 255 divided by 2 48 | const float inScaleFactor = 1.0f / meanVal; 49 | bool swapRB = true; 50 | bool crop = false; 51 | Mat inputBlob = blobFromImage(frame, 52 | inScaleFactor, 53 | Size(inWidth, inHeight), 54 | Scalar(meanVal, meanVal, meanVal), 55 | swapRB, 56 | crop); 57 | 58 | network.setInput(inputBlob); 59 | 60 | double t = (double)getTickCount(); 61 | 62 | Mat result = network.forward(); 63 | 64 | t = ((double)getTickCount() - t)/getTickFrequency(); 65 | t *= 1000; // convert to ms 66 | 67 | Mat detections(result.size[2], result.size[3], CV_32F, result.ptr()); 68 | 69 | const float confidenceThreshold = 0.5f; 70 | for(int i=0; i(i, 2); 73 | if(confidence > confidenceThreshold) 74 | { 75 | int objectClass = (int)(detections.at(i, 1)) 76 | - 1; 77 | int left = static_cast( 78 | detections.at(i, 3) * frame.cols); 79 | int top = static_cast( 80 | detections.at(i, 4) * frame.rows); 81 | int right = static_cast( 82 | detections.at(i, 5) * frame.cols); 83 | int bottom = static_cast( 84 | detections.at(i, 6) * frame.rows); 85 | rectangle(frame, Point(left, top), 86 | Point(right, bottom), Scalar(0, 255, 0)); 87 | String label = "ID = " + to_string(objectClass); 88 | if(objectClass < labels.size()) 89 | label = labels[objectClass]; 90 | 91 | int baseLine = 0; 92 | Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 93 | 0.5, 2, &baseLine); 94 | top = max(top, labelSize.height); 95 | 96 | rectangle(frame, 97 | Point(left, top - labelSize.height), 98 | Point(left + labelSize.width, top + baseLine), 99 | white, 100 | CV_FILLED); 101 | 102 | putText(frame, label, Point(left, top), 103 | FONT_HERSHEY_SIMPLEX, 0.5, red); 104 | } 105 | } 106 | 107 | 108 | putText(frame, 109 | "Took " + to_string(int(t)) + "ms to detect", 110 | Point(0, frame.rows-1), 111 | FONT_HERSHEY_SIMPLEX, 112 | scale, 113 | green, 114 | thickness); 115 | 116 | imshow("Camera", frame); 117 | if(waitKey(10) == 27) // escape key 118 | break; 119 | 120 | } 121 | 122 | return 0; 123 | } 124 | -------------------------------------------------------------------------------- /Chapter08/CvSVM/.gitignore: -------------------------------------------------------------------------------- 1 | # This file is used to ignore files which are generated 2 | # ---------------------------------------------------------------------------- 3 | 4 | *~ 5 | *.autosave 6 | *.a 7 | *.core 8 | *.moc 9 | *.o 10 | *.obj 11 | *.orig 12 | *.rej 13 | *.so 14 | *.so.* 15 | *_pch.h.cpp 16 | *_resource.rc 17 | *.qm 18 | .#* 19 | *.*# 20 | core 21 | !core/ 22 | tags 23 | .DS_Store 24 | .directory 25 | *.debug 26 | Makefile* 27 | *.prl 28 | *.app 29 | moc_*.cpp 30 | ui_*.h 31 | qrc_*.cpp 32 | Thumbs.db 33 | *.res 34 | *.rc 35 | /.qmake.cache 36 | /.qmake.stash 37 | 38 | # qtcreator generated files 39 | *.pro.user* 40 | 41 | # xemacs temporary files 42 | *.flc 43 | 44 | # Vim temporary files 45 | .*.swp 46 | 47 | # Visual Studio generated files 48 | *.ib_pdb_index 49 | *.idb 50 | *.ilk 51 | *.pdb 52 | *.sln 53 | *.suo 54 | *.vcproj 55 | *vcproj.*.*.user 56 | *.ncb 57 | *.sdf 58 | *.opensdf 59 | *.vcxproj 60 | *vcxproj.* 61 | 62 | # MinGW generated files 63 | *.Debug 64 | *.Release 65 | 66 | # Python byte code 67 | *.pyc 68 | 69 | # Binaries 70 | # -------- 71 | *.dll 72 | *.exe 73 | 74 | -------------------------------------------------------------------------------- /Chapter08/CvSVM/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | 3 | project(CvSVM) 4 | 5 | set(OpenCV_DIR $ENV{OPENCV_PATH}) 6 | find_package(OpenCV REQUIRED) 7 | include_directories(${OpenCV_INCLUDE_DIRS}) 8 | 9 | add_executable(${PROJECT_NAME} "main.cpp") 10 | 11 | target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS}) 12 | -------------------------------------------------------------------------------- /Chapter08/CvSVM/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | using namespace cv; 5 | using namespace ml; 6 | 7 | int main() 8 | { 9 | const int SAMPLE_COUNT = 8; 10 | Ptr data; 11 | 12 | float samplesA[SAMPLE_COUNT][2] 13 | = { {250, 50}, 14 | {125, 100}, 15 | {50, 50}, 16 | {150, 150}, 17 | {100, 250}, 18 | {250, 250}, 19 | {150, 50}, 20 | {50, 250} }; 21 | Mat samples(SAMPLE_COUNT, 2, CV_32F, samplesA); 22 | 23 | int responsesA[SAMPLE_COUNT] 24 | = {2, 2, 2, 2, 1, 2, 2, 1}; 25 | Mat responses(SAMPLE_COUNT, 1, CV_32S, responsesA); 26 | 27 | SampleTypes layout = ROW_SAMPLE; 28 | data = TrainData::create(samples, 29 | layout, 30 | responses); 31 | 32 | Ptr svm = SVM::create(); 33 | svm->setType(SVM::C_SVC); 34 | svm->setKernel(SVM::LINEAR); 35 | svm->setTermCriteria( 36 | TermCriteria(TermCriteria::MAX_ITER + 37 | TermCriteria::EPS, 38 | 100, 39 | 1e-6)); 40 | 41 | if(!svm->train(data)) 42 | { 43 | cout << "training failed" << endl; 44 | return -1; 45 | } 46 | 47 | Mat image = Mat::zeros(300, 48 | 300, 49 | CV_8UC3); 50 | Vec3b blue(255,0,0), green(0,255,0); 51 | for (int i = 0; i < image.rows; ++i) 52 | { 53 | for (int j = 0; j < image.cols; ++j) 54 | { 55 | Mat_ sampleMat(1,2); 56 | sampleMat << j,i; 57 | float response = svm->predict(sampleMat); 58 | 59 | if (response == 1) 60 | image.at(i,j) = green; 61 | else if (response == 2) 62 | image.at(i,j) = blue; 63 | } 64 | } 65 | 66 | Vec3b black(0,0,0), white(255,255,255), color; 67 | for(int i=0; iHands-On Algorithms for Computer Vision 4 | 5 | This is the code repository for [Hands-On Algorithms for Computer Vision](https://www.packtpub.com/application-development/hands-algorithms-computer-vision?utm_source=github&utm_medium=repository&utm_campaign=9781789130942 ), published by Packt. 6 | 7 | **Learn how to use the best and most practical computer vision algorithms using OpenCV** 8 | 9 | ## What is this book about? 10 | An arena that has been positively impacted by the advancements in processing power and performance is the field of Computer Vision. It's only natural that over time, more and more algorithms are introduced to perform Computer Vision tasks more efficiently. Hands-On Algorithms for Computer Vision is a starting point for anyone who is interested in the field of Computer Vision and wants to explore the most practical algorithms used by professional Computer Vision developers. The book starts with the basics and builds up over the course of the chapters with hands-on examples for each algorithm. 11 | 12 | This book covers the following exciting features: 13 | Get to grips with machine learning and artificial intelligence algorithms 14 | Read, write, and process images and videos 15 | Perform mathematical, matrix, and other types of image data operations 16 | Create and use histograms from back-projection images 17 | Detect motion, extract foregrounds, and track objects. 18 | Extract key points with a collection of feature detector algorithms 19 | Develop cascade classifiers and use them, and train and test classifiers 20 | Employ TensorFlow Object Detection to detect multiple objects. 21 | 22 | If you feel this book is for you, get your [copy](https://www.amazon.com/dp/1789130948) today! 23 | 24 | https://www.packtpub.com/ 26 | 27 | ## Instructions and Navigations 28 | All of the code is organized into folders. For example, Chapter02. 29 | Chapter01 does not contain any code files 30 | 31 | The code will look like the following: 32 | ``` 33 | HistCompMethods method = HISTCMP_CORREL; 34 | double result = compareHist(histogram1, histogram2, method); 35 | ``` 36 | 37 | **Following is what you need for this book:** 38 | Hands-On Algorithms for Computer Vision helps those who want to learn algorithms in Computer Vision to create and customize their applications. This book will also help existing Computer Vision developers customize their applications. A basic understanding of computer vision and programming experience is needed. 39 | 40 | With the following software and hardware list you can run all code files present in the book (Chapter 1-8). 41 | ### Software and Hardware List 42 | | Chapter | Software required | OS required | 43 | | -------- | ------------------------------------ | ----------------------------------- | 44 | | 2-8 | Microsoft Visual Studio 2017 or 2015 | Windows, Mac OS X, and Linux (Any) | 45 | | 2-8 | Xcode | Mac OS X | 46 | | 2-8 | Visual Studio Code 1.25 | Windows, Mac OS X, and Linux (Any) | 47 | | 2-8 | CMake 3.11.4 | Windows, Mac OS X, and Linux (Any) | 48 | | 2-8 | OpenCV 3.4.2 | Windows, Mac OS X, and Linux (Any) | 49 | | 2-8 | Python 3.6.5 | Windows, Mac OS X, and Linux (Any) | 50 | 51 | We also provide a PDF file that has color images of the screenshots/diagrams used in this book. [Click here to download it](https://www.packtpub.com/sites/default/files/downloads/HandsOnAlgorithmsforComputerVision_ColorImages.pdf). 52 | 53 | ### Related products 54 | * Practical Computer Vision [[Packt]](https://www.packtpub.com/big-data-and-business-intelligence/practical-computer-vision?utm_source=github&utm_medium=repository&utm_campaign=9781788297684 ) [[Amazon]](https://www.amazon.com/dp/B079QXG3WR) 55 | 56 | * Computer Vision with OpenCV 3 and Qt5 [[Packt]](https://www.packtpub.com/application-development/computer-vision-opencv-3-and-qt5?utm_source=github&utm_medium=repository&utm_campaign=9781788472395 ) [[Amazon]](https://www.amazon.com/dp/178847239X) 57 | 58 | 59 | ## Get to Know the Author 60 | **Amin Ahmadi Tazehkandi** 61 | is an Iranian author, developer, and a computer vision expert. He completed his computer software engineering studies in Iran and has worked for numerous software and industrial companies around the world. 62 | 63 | 64 | ## Other books by the authors 65 | [Computer Vision with OpenCV 3 and Qt5](https://www.packtpub.com/application-development/computer-vision-opencv-3-and-qt5?utm_source=github&utm_medium=repository&utm_campaign=9781788472395 ) 66 | 67 | 68 | ### Suggestions and Feedback 69 | [Click here](https://docs.google.com/forms/d/e/1FAIpQLSdy7dATC6QmEL81FIUuymZ0Wy9vH1jHkvpY57OiMeKGqib_Ow/viewform) if you have any feedback or suggestions. 70 | 71 | -------------------------------------------------------------------------------- /Sign.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Algorithms-for-Computer-Vision/342d5fb7966ff8c473be4e3a560ebacc9205d20f/Sign.png -------------------------------------------------------------------------------- /Test.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Algorithms-for-Computer-Vision/342d5fb7966ff8c473be4e3a560ebacc9205d20f/Test.png -------------------------------------------------------------------------------- /Test2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/PacktPublishing/Hands-On-Algorithms-for-Computer-Vision/342d5fb7966ff8c473be4e3a560ebacc9205d20f/Test2.png --------------------------------------------------------------------------------