Skip to content

Commit 6baf189

Browse files
committed
2 parents 7b0229d + 429faf7 commit 6baf189

File tree

20 files changed

+2545
-0
lines changed

20 files changed

+2545
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This folder contains code for Classification With Localization: Convert any keras Classifier to a Detector. You can find the Complete blogpost [here](https://www.learnopencv.com/classification-with-localization/).
2+
3+
Although you can download the code from this section, but you can also use this Ready to run [Colab Notebook](https://colab.research.google.com/drive/1naVE8yU_ryVGvLRxHE_QPR5rjmaVb4ne#scrollTo=wNgVViF5Tz19).
4+
5+
# AI Courses by OpenCV
6+
7+
Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/courses/) is a great place to start.
8+
9+
<a href="https://opencv.org/courses/">
10+
<p align="center">
11+
<img src="https://www.learnopencv.com/wp-content/uploads/2020/04/AI-Courses-By-OpenCV-Github.png">
12+
</p>
13+
</a>

Classification-with-localization-convert-any-keras-classifier-into-a-detector/[LearnOpenCV]_Classification_With_Localization_Notebook.ipynb

+1,947
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tensorflow==2.3.0
2+
imgaug==0.2.9
3+
numpy==1.18.5
4+
pandas==1.1.3
5+
opencv-contrib-python==4.1.2.30
6+
opencv-python==4.1.2.30
7+
sklearn==0.0
8+
sklearn-pandas==1.8.0
9+
scikit-image==0.16.2
10+
scikit-learn==0.22.2.post1
11+
scipy==1.4.1
12+
matplotlib==3.2.2
13+
urllib3==1.24.3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required(VERSION 3.1)
2+
3+
set(CMAKE_CXX_STANDARD 11)
4+
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
5+
6+
PROJECT(stereo)
7+
8+
find_package( OpenCV REQUIRED )
9+
10+
11+
MACRO(add_example name)
12+
ADD_EXECUTABLE(${name} ${name}.cpp)
13+
TARGET_LINK_LIBRARIES(${name} ${OpenCV_LIBS})
14+
ENDMACRO()
15+
16+
add_example(stereo)
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Introduction to Epipolar Geometry and Stereo Vision
2+
This folder contains the code for the blog post on [Introduction to Epipolar Geometry and Stereo Vision](https://www.learnopencv.com/introduction-to-epipolar-geometry-and-stereo-vision/).
3+
## Compilation
4+
To compile the `stereo.cpp` code file, use the following:
5+
```shell
6+
mkdir build
7+
cd build
8+
cmake ..
9+
cmake --build . --config Release
10+
```
11+
12+
## Usage
13+
14+
### Using the C++ code
15+
16+
Refer to the following to use the compiled files:
17+
18+
```shell
19+
./build/stereo
20+
```
21+
22+
### Using the python code
23+
24+
Refer to the following to use the `stereo.py` file:
25+
26+
```shell
27+
python3 stereo.py
28+
```
29+
30+
# AI Courses by OpenCV
31+
32+
Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/courses/) is a great place to start.
33+
34+
<a href="https://opencv.org/courses/">
35+
<p align="center">
36+
<img src="https://www.learnopencv.com/wp-content/uploads/2020/04/AI-Courses-By-OpenCV-Github.png">
37+
</p>
38+
</a>
6.74 MB
Loading
6.7 MB
Loading
+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <opencv2/opencv.hpp>
2+
#include "opencv2/core/core.hpp"
3+
#include "opencv2/calib3d/calib3d.hpp"
4+
#include <opencv2/highgui/highgui.hpp>
5+
#include <opencv2/imgproc/imgproc.hpp>
6+
#include <stdio.h>
7+
#include <string.h>
8+
9+
int main(int argc, char** argv)
10+
{
11+
12+
cv::Mat imgL,imgR;
13+
14+
imgL = cv::imread("images/im0.png",0);
15+
cv::resize(imgL,imgL,cv::Size(600,600));
16+
imgR = cv::imread("images/im1.png",0);
17+
cv::resize(imgR,imgR,cv::Size(600,600));
18+
19+
int minDisparity = 0;
20+
int numDisparities = 64;
21+
int blockSize = 8;
22+
int disp12MaxDiff = 1;
23+
int uniquenessRatio = 10;
24+
int speckleWindowSize = 10;
25+
int speckleRange = 8;
26+
27+
cv::Ptr<cv::StereoSGBM> stereo = cv::StereoSGBM::create(minDisparity,numDisparities,blockSize,disp12MaxDiff,uniquenessRatio,speckleWindowSize,speckleRange);
28+
29+
cv::Mat disp;
30+
stereo->compute(imgL,imgR,disp);
31+
32+
cv::normalize(disp, disp, 0, 255, cv::NORM_MINMAX, CV_8UC1);
33+
34+
cv::imshow("Left image",imgL);
35+
cv::imshow("Right image",imgR);
36+
cv::imshow("disparity",disp);
37+
cv::waitKey(0);
38+
39+
return 0;
40+
}
41+
+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from __future__ import print_function
2+
3+
import numpy as np
4+
import cv2
5+
6+
def main():
7+
imgL = cv2.imread("images/im0.png",0)
8+
imgL = cv2.resize(imgL,(600,600))
9+
10+
imgR = cv2.imread("images/im1.png",0)
11+
imgR = cv2.resize(imgR,(600,600))
12+
13+
# Setting parameters for StereoSGBM algorithm
14+
minDisparity = 0
15+
numDisparities = 64
16+
blockSize = 8
17+
disp12MaxDiff = 1
18+
uniquenessRatio = 10
19+
speckleWindowSize = 10
20+
speckleRange = 8
21+
22+
# Creating an object of StereoSGBM algorithm
23+
stereo = cv2.StereoSGBM_create(minDisparity = minDisparity,
24+
numDisparities = numDisparities,
25+
blockSize = blockSize,
26+
disp12MaxDiff = disp12MaxDiff,
27+
uniquenessRatio = uniquenessRatio,
28+
speckleWindowSize = speckleWindowSize,
29+
speckleRange = speckleRange
30+
)
31+
32+
# Calculating disparith using the StereoSGBM algorithm
33+
disp = stereo.compute(imgL, imgR).astype(np.float32)
34+
35+
# Calculating disparith using the StereoSGBM algorithm
36+
disp = cv2.normalize(disp,0,255,cv2.NORM_MINMAX)
37+
38+
# Displaying the disparity map
39+
cv2.imshow("disparity",disp)
40+
cv2.imshow("left image",imgL)
41+
cv2.imshow("right image",imgR)
42+
cv2.waitKey(0)
43+
44+
45+
if __name__ == '__main__':
46+
main()
47+
cv2.destroyAllWindows()

Optical-Flow-in-OpenCV/README.md

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
# Optical Flow demo
2+
3+
This contains the code for **Optical Flow in OpenCV (C++/Python)** post
4+
For more information - visit
5+
[Optical Flow in OpenCV (C++/Python)](https://www.learnopencv.com/optical-flow-in-opencv)
6+
7+
## Python
8+
9+
### Installation
10+
11+
Before you start the demo of Optical Flow calculation, you need to create a virtual environment in your working
12+
directory and install the required libraries:
13+
14+
```Shell
15+
virtualenv -p python3.7 venv
16+
source venv/bin/activate
17+
pip install -r reqirements.txt
18+
```
19+
20+
### Sparse Optical Flow
21+
22+
There is a demo `lucas_kanade.py` script of **Lucas-Kanade** algorithm which can be run with this command:
23+
24+
```
25+
python3 demo.py --algorithm lucaskanade --video_path videos/car.mp4
26+
```
27+
28+
### Dense Optical Flow
29+
30+
The wrapper of Dense Optical Flow algorithms `dense_optical_flow.py` can run a couple of OpenCV's algorithm
31+
implementations:
32+
33+
- To start the **Dense Lucas-Kanade** algorithm:
34+
```
35+
python3 demo.py --algorithm lucaskanade_dense --video_path videos/people.mp4
36+
```
37+
- To start the **Farneback** algorithm:
38+
```
39+
python3 demo.py --algorithm farneback --video_path videos/people.mp4
40+
```
41+
- To start the **RLOF** algorithm:
42+
```
43+
python3 demo.py --algorithm rlof --video_path videos/people.mp4
44+
```
45+
46+
## C++
47+
48+
### Installation
49+
50+
Before you start the demo of Optical Flow calculation, you need to build the project:
51+
52+
```Shell
53+
cd algorithms
54+
cmake .
55+
make
56+
```
57+
58+
### Sparse Optical Flow
59+
60+
There is a demo `lucas_kanade.cpp` script of **Lucas-Kanade** algorithm which can be run with this command:
61+
62+
```
63+
./OpticalFlow ../videos/car.mp4 lucaskanade
64+
```
65+
66+
### Dense Optical Flow
67+
68+
The wrapper of Dense Optical Flow algorithms `dense_optical_flow.py` can run a couple of OpenCV's algorithm
69+
implementations:
70+
71+
- To start the **Dense Lucas-Kanade** algorithm:
72+
```
73+
./OpticalFlow ../videos/car.mp4 lucaskanade_dense
74+
```
75+
- To start the **Farneback** algorithm:
76+
```
77+
./OpticalFlow ../videos/car.mp4 farneback
78+
```
79+
- To start the **RLOF** algorithm:
80+
```
81+
./OpticalFlow ../videos/car.mp4 rlof
82+
```
83+
84+
85+
# AI Courses by OpenCV
86+
87+
Want to become an expert in AI? [AI Courses by OpenCV](https://opencv.org/courses/) is a great place to start.
88+
89+
<a href="https://opencv.org/courses/">
90+
<p align="center">
91+
<img src="https://www.learnopencv.com/wp-content/uploads/2020/04/AI-Courses-By-OpenCV-Github.png">
92+
</p>
93+
</a>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cmake_minimum_required(VERSION 3.15)
2+
project(OpticalFlow)
3+
find_package( OpenCV REQUIRED )
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(OpticalFlow main.cpp)
7+
target_link_libraries( OpticalFlow ${OpenCV_LIBS} )
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
#include <opencv2/core.hpp>
3+
#include <opencv2/highgui.hpp>
4+
#include <opencv2/imgproc.hpp>
5+
#include <opencv2/videoio.hpp>
6+
7+
8+
using namespace cv;
9+
using namespace std;
10+
11+
template <typename Method, typename... Args>
12+
void dense_optical_flow(string filename, bool save, Method method, bool to_gray, Args&&... args)
13+
{
14+
VideoCapture capture(samples::findFile(filename));
15+
if (!capture.isOpened()) {
16+
//error in opening the video input
17+
cerr << "Unable to open file!" << endl;
18+
}
19+
Mat frame1, prvs;
20+
capture >> frame1;
21+
if (to_gray)
22+
cvtColor(frame1, prvs, COLOR_BGR2GRAY);
23+
else
24+
prvs = frame1;
25+
int counter = 0;
26+
while (true) {
27+
Mat frame2, next;
28+
capture >> frame2;
29+
if (frame2.empty())
30+
break;
31+
if (to_gray)
32+
cvtColor(frame2, next, COLOR_BGR2GRAY);
33+
else
34+
next = frame2;
35+
Mat flow(prvs.size(), CV_32FC2);
36+
method(prvs, next, flow, std::forward<Args>(args)...);
37+
// visualization
38+
Mat flow_parts[2];
39+
split(flow, flow_parts);
40+
Mat magnitude, angle, magn_norm;
41+
cartToPolar(flow_parts[0], flow_parts[1], magnitude, angle, true);
42+
normalize(magnitude, magn_norm, 0.0f, 1.0f, NORM_MINMAX);
43+
angle *= ((1.f / 360.f) * (180.f / 255.f));
44+
//build hsv image
45+
Mat _hsv[3], hsv, hsv8, bgr;
46+
_hsv[0] = angle;
47+
_hsv[1] = Mat::ones(angle.size(), CV_32F);
48+
_hsv[2] = magn_norm;
49+
merge(_hsv, 3, hsv);
50+
hsv.convertTo(hsv8, CV_8U, 255.0);
51+
cvtColor(hsv8, bgr, COLOR_HSV2BGR);
52+
if (save) {
53+
string save_path = "./optical_flow_frames/frame_" + to_string(counter) + ".jpg";
54+
imwrite(save_path, bgr);
55+
}
56+
imshow("frame", frame2);
57+
imshow("flow", bgr);
58+
int keyboard = waitKey(30);
59+
if (keyboard == 'q' || keyboard == 27)
60+
break;
61+
prvs = next;
62+
counter++;
63+
}
64+
}
65+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import cv2
2+
import numpy as np
3+
4+
5+
def dense_optical_flow(method, video_path, params=[], to_gray=False):
6+
# read the video
7+
cap = cv2.VideoCapture(video_path)
8+
# Read the first frame
9+
ret, old_frame = cap.read()
10+
11+
# crate HSV & make Value a constant
12+
hsv = np.zeros_like(old_frame)
13+
hsv[..., 1] = 255
14+
15+
# Preprocessing for exact method
16+
if to_gray:
17+
old_frame = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY)
18+
19+
while True:
20+
# Read the next frame
21+
ret, new_frame = cap.read()
22+
frame_copy = new_frame
23+
if not ret:
24+
break
25+
# Preprocessing for exact method
26+
if to_gray:
27+
new_frame = cv2.cvtColor(new_frame, cv2.COLOR_BGR2GRAY)
28+
# Calculate Optical Flow
29+
flow = method(old_frame, new_frame, None, *params)
30+
31+
# Encoding: convert the algorithm's output into Polar coordinates
32+
mag, ang = cv2.cartToPolar(flow[..., 0], flow[..., 1])
33+
# Use Hue and Saturation to encode the Optical Flow
34+
hsv[..., 0] = ang * 180 / np.pi / 2
35+
hsv[..., 2] = cv2.normalize(mag, None, 0, 255, cv2.NORM_MINMAX)
36+
# Convert HSV image into BGR for demo
37+
bgr = cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
38+
cv2.imshow("frame", frame_copy)
39+
cv2.imshow("optical flow", bgr)
40+
k = cv2.waitKey(25) & 0xFF
41+
if k == 27:
42+
break
43+
old_frame = new_frame

0 commit comments

Comments
 (0)