forked from SRA-VJTI/Pixels_Seminar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Port to C++] OpenCV Overview (SRA-VJTI#94)
* opencv overview * Fixed issue with Makefile * Made changes as instructed * Made Makefile for the current folder * Added Morphological operations * Adding function descriptions and expected output for morphology Signed-off-by: vrnimje <vedantnimjed@gmail.com> * Restructure the Readme * Added comments * Removed blob detection notebook * Made changes * Renamed folders * Removed Makefile * Updated readme * Removed redundant images * Removed redundant images --------- Signed-off-by: vrnimje <vedantnimjed@gmail.com> Co-authored-by: vrnimje <vedantnimjed@gmail.com>
- Loading branch information
1 parent
b86dd1f
commit 75d2dd0
Showing
82 changed files
with
1,284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
output: read_display_image.cpp | ||
@echo "Building.." | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@PROJECT_SOURCE_DIR@../../..@g' {} \; | ||
@g++ read_display_image.cpp -o read_display_image `pkg-config opencv4 --cflags --libs` -lsqlite3 | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@../../..@PROJECT_SOURCE_DIR@g' {} \; | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@../../..@PROJECT_SOURCE_DIR@g' {} \; | ||
|
||
clean: | ||
rm output |
12 changes: 12 additions & 0 deletions
12
4_cv_basics/7_opencv_overview/1_read_display_image/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Reading and Displaying Images | ||
|
||
- <b>Mat</b>: Mat is not a function. It is a data structure, a type of variable. Like int, char, string variable types in C++, Mat is a variable of OpenCV, which creates a matrix data structure to load images inside it. In this program, we wrote 'Mat myImage;'. That means we are declaring a matrix variable named 'myImage'. | ||
|
||
- <b>namedWindow()</b>: It allocates some memory and creates a window to show the image. It works like a photo frame. In OpenCV, we have to make the function as 'namedWindow("name of the window",flag)'. | ||
- <b>imread()</b>: This function reads an image from a defined location. This program reads the image from 'C:' drive. To use this function, you have to write it as 'imread("location of the image/name of the image with the extension", flag)'. | ||
- <b>imshow()</b>: This function shows the image in the defined window. To use this function you have to write as 'imshow(name of the window", name of the matrix)'. | ||
- <b>waitKey()</b>: This is a vital function of OpenCV. To process images and executes operations, we must allow the system some time. If we don't do it, we will not | ||
|
||
This function waits for a certain period before closing the program. If you use waitKey(10000), it will close the program after 10 seconds. If you write waitKey(0), it will get the desired output. This function will enable us to give the system the required time to operate. wait for the keystroke from the user. When the user clicks any key from the keyboard, the program will stop. This function has to be written as 'waitKey(milliseconds)'. | ||
|
||
- <b>destroyWindows()</b>: This function closes all windows. When we create windows, we allocate some memory. <b>destroyWindow()</b> function releases that memory to the system. |
27 changes: 27 additions & 0 deletions
27
4_cv_basics/7_opencv_overview/1_read_display_image/read_display_image.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <iostream> | ||
#include <opencv2/opencv.hpp> | ||
using namespace cv; | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
Mat image; | ||
image = imread("PROJECT_SOURCE_DIR/assets/images/purple_night.jpg"); | ||
if (!image.data) | ||
{ | ||
cout << "Image not found" << endl; | ||
return -1; | ||
} | ||
|
||
auto x = image.size; | ||
|
||
// x[0] - height of the image (number of rows) | ||
// x[1] - width of the image (number of columns) | ||
cout << x[0] << ',' << x[1] << endl; | ||
cout << image.channels() << endl; | ||
namedWindow("Display Image"); | ||
imshow("Display Image", image); | ||
waitKey(0); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
output: grayscale.cpp | ||
@echo "Building.." | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@PROJECT_SOURCE_DIR@../../..@g' {} \; | ||
@g++ grayscale.cpp -o grayscale `pkg-config opencv4 --cflags --libs` -lsqlite3 | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@../../..@PROJECT_SOURCE_DIR@g' {} \; | ||
|
||
clean: | ||
rm output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## Grayscale | ||
|
||
A grayscale image is a type of image that only contains shades of gray, ranging from black (zero intensity) to white (maximum intensity), with no color information. It is a commonly used image representation in computer vision, image processing, and other related fields. Converting a color image to grayscale is a process of removing the color information and keeping only the brightness information of the image. | ||
|
||
In OpenCV, the `cvtColor` function is used to convert an image from one color space to another. To convert an image to grayscale, we need to use the `COLOR_BGR2GRAY` flag as the second argument of cvtColor function. This flag specifies that we want to convert the image from the `BGR` color space (used by default in OpenCV) to grayscale. | ||
|
||
The `cvtColor` function takes the input image and creates a new image with the same size and depth, but with only one channel (grayscale). The algorithm for converting color to grayscale varies depending on the application, but a common approach is to calculate the luminance (brightness) of each pixel using the following formula: | ||
|
||
$Luminance = 0.299 * R + 0.587 * G + 0.114 * B$ | ||
|
||
Where $R$, $G$ and $B$ are the red, green, and blue color channels of the pixel. The coefficients $0.299$, $0.587$ and $0.114$ are chosen to reflect the sensitivity of the human eye to different colors. This formula is applied to each pixel of the input image to produce the corresponding pixel value in the output grayscale image. | ||
|
||
#### Illustration | ||
|
||
<img src="../assets/images/grayscale.png"/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#include <iostream> | ||
#include <opencv2/opencv.hpp> | ||
using namespace cv; | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
Mat image; | ||
|
||
// Reading image | ||
image = imread("PROJECT_SOURCE_DIR/assets/images/purple_night.jpg"); | ||
if (!image.data) | ||
{ | ||
cout << "Image not found" << endl; | ||
return -1; | ||
} | ||
|
||
Mat im2; | ||
|
||
// Converts colorspace from BGR to Grayscale (BGR2GRAY) | ||
cvtColor(image, im2, COLOR_BGR2GRAY); | ||
|
||
auto x = im2.size; | ||
|
||
// x[0] - height of the image (number of rows) | ||
// x[1] - width of the image (number of columns) | ||
cout << x[0] << ',' << x[1] << endl; | ||
cout << im2.channels() << endl; | ||
|
||
// Display resultant image | ||
namedWindow("Display Image"); | ||
imshow("Display Image", im2); | ||
waitKey(0); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
output: resizing.cpp | ||
@echo "Building.." | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@PROJECT_SOURCE_DIR@../../..@g' {} \; | ||
@g++ resizing.cpp -o resizing `pkg-config opencv4 --cflags --libs` -lsqlite3 | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@../../..@PROJECT_SOURCE_DIR@g' {} \; | ||
|
||
clean: | ||
rm output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
## Resizing | ||
|
||
Image resizing refers to the scaling of images. Scaling comes in handy in many image processing as well as machine learning applications. It helps in reducing the number of pixels from an image and that has several advantages e.g. It can reduce the time of training of a neural network as the more the number of pixels in an image more is the number of input nodes that in turn increases the complexity of the model. | ||
|
||
It also helps in zooming in on images. Many times we need to resize the image i.e. either shrink it or scale it up to meet the size requirements. OpenCV provides us several interpolation methods for resizing an image. | ||
|
||
Choice of Interpolation Method for Resizing: | ||
|
||
1. <b>INTER_AREA</b>: This is used when we need to shrink an image. | ||
2. <b>INTER_CUBIC</b>: This is slow but more efficient. | ||
3. <b>INTER_LINEAR</b>: This is primarily used when zooming is required. This is the default interpolation technique in OpenCV. | ||
|
||
### Illustration | ||
|
||
<img src = "../assets/images/resize.png" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#include <iostream> | ||
#include <opencv2/opencv.hpp> | ||
using namespace cv; | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
Mat image; | ||
image = imread("PROJECT_SOURCE_DIR/assets/images/gray.png"); | ||
if (!image.data) | ||
{ | ||
cout << "Image not found" << endl; | ||
return -1; | ||
} | ||
|
||
int height = image.size().height; | ||
int width = image.size().width; | ||
|
||
cout << "Original Image Size: " << width << "x" << height << std::endl; | ||
|
||
Mat im2; | ||
|
||
Size new_size; | ||
|
||
new_size.height = 200; | ||
new_size.width = 700; | ||
|
||
resize(image, im2, new_size); | ||
|
||
namedWindow("Display Image"); | ||
imshow("Display Image", im2); | ||
waitKey(0); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
output: blending.cpp | ||
@echo "Building.." | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@PROJECT_SOURCE_DIR@../../..@g' {} \; | ||
@g++ blending.cpp -o blending `pkg-config opencv4 --cflags --libs` -lsqlite3 | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@../../..@PROJECT_SOURCE_DIR@g' {} \; | ||
|
||
clean: | ||
rm output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
## Blending | ||
|
||
This is also image addition, but different weights are given to images in order to give a feeling of blending or transparency. Images are added as per the equation below: | ||
|
||
$g(x)=(1−α)(f_0(x)) +αf1(x)$ | ||
|
||
By varying $α$ from $0→1$, you can perform a cool transition between one image to another. | ||
|
||
Here two images are taken to blend together. The first image is given a weight of 0.7 and the second image is given 0.3. `cv.addWeighted()` applies the following equation to the image: | ||
|
||
$dst=α⋅img1+β⋅img2+γ$ | ||
|
||
Here $γ$ is taken as zero. | ||
|
||
The $\gamma$ parameter in blending of images refers to the gamma correction applied to the images before blending. Gamma correction is a nonlinear operation that adjusts the brightness levels of an image. It is used to compensate for the nonlinear response of the human eye to changes in brightness. | ||
|
||
The $\gamma$ parameter in the addWeighted function controls the gamma correction applied to the source images before blending. It is specified as a floating-point value, where a value of 1.0 means no gamma correction is applied, and values <b>less</b> than 1.0 <b>darken</b> the image, while values <b>greater</b> than 1.0 <b>brighten</b> the image. | ||
|
||
**Note** : Image combination is a subset of Image Blending. In image blending we can specify the amount/percentage of effect that we want form either of the input images. | ||
|
||
### Illustrations | ||
|
||
<img src="../assets/images/blending1.png" aligh = "left"/> | ||
|
||
<img src="../assets/images/blending2.png"/> | ||
|
||
### Expected Results | ||
|
||
<!-- Masking --> | ||
<table> | ||
<tr> | ||
<th colspan="2" style="text-align:center"> BLENDING </th> | ||
</tr> | ||
<tr> | ||
<th style="text-align:center"> Image1</th> | ||
<th style="text-align:center"> Image2 </th> | ||
</tr> | ||
<tr> | ||
<td><image src="../assets/images/blending_image_1.png" alt = "Image1" width = 350 height="200"></td> | ||
<td><image src="../assets/images/blending_image_2.png" alt = "Image1" width = 350 height="200"></td> | ||
</tr> | ||
|
||
<tr> | ||
<th style="text-align:center" colspan="2"> Display Image</th> | ||
</tr> | ||
<tr> | ||
<td style="text-align: center" colspan="2"><image src="../assets/images/blending_res.png" alt = "Image1" width = 350 height="200"></td> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <iostream> | ||
#include <opencv2/opencv.hpp> | ||
using namespace cv; | ||
using namespace std; | ||
|
||
int main() | ||
{ | ||
// Reading input images (A and B) | ||
Mat image1 = imread("PROJECT_SOURCE_DIR/assets/images/dummy1.jpg"); | ||
Mat image2 = imread("PROJECT_SOURCE_DIR/assets/images/purple_night.jpg"); | ||
|
||
resize(image1, image1, Size(), 0.75, 0.75); | ||
resize(image2, image2, Size(), 0.75, 0.75); | ||
imshow("Image1", image1); | ||
imshow("Image2", image2); | ||
|
||
Mat res; | ||
|
||
// Defining weights for the two images | ||
float alpha = 0.4, beta = (1 - alpha); | ||
|
||
// Blending image1 and image2 | ||
// res = alpha * image1 + beta * image2 + gamma(0) | ||
addWeighted(image1, alpha, image2, beta, 0.0, res); | ||
|
||
namedWindow("Display Image"); | ||
resize(res, res, Size(), 0.75, 0.75); | ||
imshow("Display Image", res); | ||
waitKey(0); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
output: masking.cpp | ||
@echo "Building.." | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@PROJECT_SOURCE_DIR@../../..@g' {} \; | ||
@g++ masking.cpp -o masking `pkg-config opencv4 --cflags --libs` -lsqlite3 | ||
@find $(shell pwd) -name "*.cpp" -type f -exec sed -i 's@../../..@PROJECT_SOURCE_DIR@g' {} \; | ||
clean: | ||
rm output |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
## Masking | ||
|
||
1. Masking is an image processing method in which we define a small 'image piece' and use it to modify a larger image. | ||
2. When talking about editing and processing images the term 'masking' refers to the practice of using a mask to protect a specific area of an image, just as you would use masking tape when painting your house. | ||
3. Masking an area of an image protects that area from being altered by changes made to the rest of the image. | ||
4. Masking is the process that is underneath many types of image processing, including edge detection, motion detection, and noise reduction | ||
5. Usually binary masks are used (BLACK and WHITE) | ||
6. Generally, BLACK portions are those which are undesired and want to be removed/ masked off | ||
7. Generally, WHITE portions are those which are desired and want to be retained in the original image | ||
|
||
#### Illustrations | ||
|
||
#### Input Images | ||
|
||
<img src="../assets/images/bitwise_ip1.png" align="left" width="300" height="300"/> | ||
|
||
<img src="../assets/images/bitwise_ip2.png" align = "centre" width="300" height="300"/> | ||
|
||
#### Bitwise AND | ||
|
||
<img src="../assets/images/bitwise_and1.png" width="300" height="300"/> | ||
|
||
#### Bitwise OR | ||
|
||
<img src="../assets/images/bitwise_or1.png" width="300" height="300"/> | ||
|
||
#### Bitwise XOR | ||
|
||
<img src="../assets/images/bitwise_xor.png" width="300" height="300"/> | ||
|
||
#### Bitwise NOT | ||
|
||
1. Image1 - Bitwise NOT/inversion of Input Image 1 | ||
2. Image2 - Bitwise NOT/inversion of Input Image 2 | ||
|
||
<img src="../assets/images/bitwise_not1.png" align="left" width="300" height="300"/> | ||
|
||
<br> | ||
|
||
<img src="../assets/images/bitwise_not2.png" align="justified" width="300" height="300"/> | ||
|
||
### Expected Results | ||
|
||
<table> | ||
<tr> | ||
<th colspan="2" style="text-align:center"> MASKING </th> | ||
</tr> | ||
<tr> | ||
<th style="text-align:center"> Image1</th> | ||
<th style="text-align:center"> Image2 </th> | ||
</tr> | ||
<tr> | ||
<td><image src="../assets/images/Image1.png" alt = "Image1" width = 350 height="200"></td> | ||
<td><image src="../assets/images/Image2.png" alt = "Image1" width = 350 height="200"></td> | ||
</tr> | ||
|
||
<tr> | ||
<th style="text-align:center"> ROI</th> | ||
<th style="text-align:center"> Gray </th> | ||
</tr> | ||
<tr> | ||
<td><image src="../assets/images/roi.png" alt = "Image1" width = 350 height="200"></td> | ||
<td><image src="../assets/images/gray.png" alt = "Image1" width = 350 height="200"></td> | ||
|
||
<tr> | ||
<th style="text-align:center"> Mask</th> | ||
<th style="text-align:center"> Mask_Inv </th> | ||
</tr> | ||
<tr> | ||
<td><image src="../assets/images/mask.png" alt = "Image1" width = 350 height="200"></td> | ||
<td><image src="../assets/images/mask_inv.png" alt = "Image1" width = 350 height="200"></td> | ||
</tr> | ||
|
||
<tr> | ||
<th style="text-align:center"> BG</th> | ||
<th style="text-align:center"> FG </th> | ||
</tr> | ||
<tr> | ||
<td><image src="../assets/images/bg.png" alt = "Image1" width = 350 height="200"></td> | ||
<td><image src="../assets/images/fg.png" alt = "Image1" width = 350 height="200"></td> | ||
</tr> | ||
|
||
<tr> | ||
<th colspan="2" style="text-align:center"> dst</th> | ||
</tr> | ||
<tr> | ||
<td colspan="2" style="text-align: center"><image src="../assets/images/dst.png" alt = "Image1" width = 350 height="200"></td> | ||
</tr> | ||
</table> |
Oops, something went wrong.