๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (2) Basic Functions (cvtColor, GaussianBlur, Edge detection, dilate, erode) ๋ณธ๋ฌธ
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (2) Basic Functions (cvtColor, GaussianBlur, Edge detection, dilate, erode)
์ง์ง์ํ์นด 2023. 6. 12. 00:58<๋ณธ ๋ธ๋ก๊ทธ๋ Murtaza's Workshop ์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision
๐ RGB(Red, Green, Blue)
์์์ ํํํ๋ ๋ฐฉ๋ฒ์ผ๋ก๋ RGB(Red, Green, Blue)
: ๋นจ๊ฐ, ์ด๋ก, ํ๋ ์ธ ๊ฐ์ง ์์ ๋น์ ์์ด์ ์ํ๋ ์์ ๋ง๋ฆ
: ๊ฐ ์์์ 0~255 ์ฌ์ด์ ๊ฐ์ผ๋ก ํ์ํ๊ณ ๊ฐ์ด ์ปค์ง์๋ก ํด๋น ์์์ ๋น์ด ๋ฐ์์ง๋ ์๋ฆฌ
: RGB = (255, 255, 255) ์ผ ๋๋ ํฐ์์ด๊ณ , RGB = (0, 0, 0) ์ผ ๋๋ ๊ฒ์์
OpenCV๋ ๊ทธ ๋ฐ๋์ ์์์ธ BGR
RGBA๋ RGB์ A(์ํ, alpha)๊ฐ ์ถ๊ฐ๋ ์์ ํ๊ธฐ๋ฒ
: A๋ ๋ฐฐ๊ฒฝ์ ํฌ๋ช ๋
: A๊ฐ์ด 255๋ฉด ํฐ์, 0์ด๋ฉด ๊ฒ์์
๐ Basic Functions
๐ง cvtColor
cvtColor( input Array, output Array, flag)
input Array๋ฅผ ์ ๋ ฅ๋ฐ์ flag ์ ๋ํ ์ต์ ์ผ๋ก ์ด๋ฏธ์ง ์์ฑ๋์ ๋ณ๊ฒฝ
COLOR_BGR2GRAY: BGR ์์ ์ด๋ฏธ์ง๋ฅผ ํ์์กฐ ์ด๋ฏธ์ง๋ก ๋ณํ
COLOR_GRAY2BGR: ํ์์กฐ ์ด๋ฏธ์ง๋ฅผ BGR ์์ ์ด๋ฏธ์ง๋ก ๋ณํ
COLOR_BGR2RGB: BGR ์์ ์ด๋ฏธ์ง๋ฅผ RGB ์์ ์ด๋ฏธ์ง๋ก ๋ณํ
COLOR_BGR2HSV: BGR ์์ ์ด๋ฏธ์ง๋ฅผ HSV ์์ ์ด๋ฏธ์ง๋ก ๋ณํ
COLOR_HSV2BGR: HSV ์์ ์ด๋ฏธ์ง๋ฅผ BGR ์์ ์ด๋ฏธ์ง๋ก ๋ณํ
COLOR_BGR2YUV: BGR ์์ ์ด๋ฏธ์ง๋ฅผ YUV ์์ ์ด๋ฏธ์ง๋ก ๋ณํ
COLOR_YUV2BGR: YUB ์์ ์ด๋ฏธ์ง๋ฅผ BGR ์์ ์ด๋ฏธ์ง๋ก ๋ณํ
#include <opencv2/opencv.hpp> // OpenCV์์ ์ง์ํ๋ ๋ชจ๋ ๊ธฐ๋ฅ
#include <opencv2/videoio.hpp> // ๋น๋์ค ์ถ์ ๋ฐ ๋ฐฐ๊ฒฝ segmentation๊ณผ ๊ด๋ จ๋ ๋ฃจํด
#include <opencv2/imgcodecs.hpp> // ๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์
์ด ์ ์ธ (Mat ์ด๋ Point๊ฐ ์ ์ธ, ํ๋ ฌ ์ฐ์ฐ ํน์ ๋ฒกํฐ ์ฐ์ฐ)
#include <opencv2/highgui.hpp> // ์๋์ฐ ํ๋ฉด, UI์ฒ๋ฆฌ(์ฌ๋ผ์ด๋, ๋ฒํผ ๋ฑ) ๋ฐ ๋ง์ฐ์ค ์ ์ด ๊ฐ๋ฅ
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
// #1. Read Images Videos and Webcams
// 1) Importing Images
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
string path = "Resources/alpaca.jpg";
Mat img = imread(path);
Mat imgGray;
Mat imgBlur;
// cvtColor( input Array, output Array, flag)
// : input Array๋ฅผ ์
๋ ฅ๋ฐ์ flag ์ ๋ํ ์ต์
์ผ๋ก ์ด๋ฏธ์ง ์์ฑ๋์ ๋ณ๊ฒฝ
cvtColor(img, imgGray, COLOR_BGR2GRAY);
imshow("img", img);
imshow("Gray img", imgGray);
waitKey(0);
}
๐ง GaussianBlur
GaussianBlur( src, dst, kernel_size, sigma_x, sigma_y, borderType)
๊ฐ์ฐ์์ ๋ธ๋ฌ๋ ์ค์๊ฐ์ ๊ฐ์ค์น๋ฅผ ๋ ์ฃผ๊ณ ์ฃผ๋ณ์ ๋ ํ๋ฆฌ๊ฒ ๋ง๋ ๋ค
src : ์ ๋ ฅํ ์ด๋ฏธ์ง ๋ณ์
dst : ํํฐ๊ฐ ์ ์ฉ๋์ด ์ ์ฅ๋ ์ด๋ฏธ์ง ๋ณ์
kernel_size : ๊ฐ์ฐ์์ ํจ์๊ฐ ์ ์ฉ๋ ์ปค๋ ๋ง์คํฌ์ ํฌ๊ธฐ
โ sigma_x : X ๋ฐฉํฅ ํ์คํธ์ฐจ
โ sigma_y : Y ๋ฐฉํฅ ํ์คํธ์ฐจ, sigma_x ๋ง ์ค์ ํ๋ฉด ์๋์ผ๋ก ๊ฐ์ ๊ฐ์ผ๋ก ์ ์ฉ๋จ
โป simga_x ์ sigma_y ๊ฐ์ 0์ผ๋ก ์ฌ์ฉํ๋ฉด ์ปค๋ ํฌ๊ธฐ์์ ์๋์ผ๋ก ๊ณ์ฐ ๋จ
borderType : ์ด๋ฏธ์ง์ ํ ๋๋ฆฌ ๋ฐ๊นฅ์ชฝ์ ๊ฐ์์ ํฝ์ ๋ค์ ์ด๋ป๊ฒ ์ฒ๋ฆฌํ ๊ฒ์ธ์ง ์ ํ
#include <opencv2/opencv.hpp> // OpenCV์์ ์ง์ํ๋ ๋ชจ๋ ๊ธฐ๋ฅ
#include <opencv2/videoio.hpp> // ๋น๋์ค ์ถ์ ๋ฐ ๋ฐฐ๊ฒฝ segmentation๊ณผ ๊ด๋ จ๋ ๋ฃจํด
#include <opencv2/imgcodecs.hpp> // ๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์
์ด ์ ์ธ (Mat ์ด๋ Point๊ฐ ์ ์ธ, ํ๋ ฌ ์ฐ์ฐ ํน์ ๋ฒกํฐ ์ฐ์ฐ)
#include <opencv2/highgui.hpp> // ์๋์ฐ ํ๋ฉด, UI์ฒ๋ฆฌ(์ฌ๋ผ์ด๋, ๋ฒํผ ๋ฑ) ๋ฐ ๋ง์ฐ์ค ์ ์ด ๊ฐ๋ฅ
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
// #1. Read Images Videos and Webcams
// 1) Importing Images
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
string path = "Resources/alpaca.jpg";
Mat img = imread(path);
Mat imgGray;
Mat imgBlur;
// cvtColor( input Array, output Array, flag)
// : input Array๋ฅผ ์
๋ ฅ๋ฐ์ flag ์ ๋ํ ์ต์
์ผ๋ก ์ด๋ฏธ์ง ์์ฑ๋์ ๋ณ๊ฒฝ
cvtColor(img, imgGray, COLOR_BGR2GRAY);
// GaussianBlur( src, dst, kernel_size, sigma_x, sigma_y, borderType)
// ์ค์๊ฐ์ ๊ฐ์ค์น๋ฅผ ๋ ์ฃผ๊ณ ์ฃผ๋ณ์ ๋ ํ๋ฆฌ๊ฒ
GaussianBlur(img, imgBlur, Size(3, 3), 3, 0);
imshow("img", img);
imshow("Gray img", imgGray);
imshow("Gray Blur", imgBlur);
waitKey(0);
}
๐ง Canny
์์์ธ์์์ ๊ฒฝ๊ณ์ ๊ฒ์ถ์ด ๋์ด์ผ ์ปดํจํฐ๊ฐ ๋ฌผ์ฒด๋ฅผ ์ธ์ํ๊ณ ์ฒ๋ฆฌ
๊ฒฝ๊ณ์ ๊ฒ์ถ, ์บ๋ ์๊ณ ๋ฆฌ์ฆ ( Edge_detection, Canny algorithm )
Canny( src, dst, threshold1, threshold2)
src : ์ ๋ ฅํ ์ด๋ฏธ์ง ๋ณ์ (grayscale ์ด๋ฏธ์ง๋ฅผ ์ ๋ ฅํด์ผํจ)
dst : ํํฐ๊ฐ ์ ์ฉ๋์ด ์ ์ฅ๋ ์ด๋ฏธ์ง ๋ณ์
threshold1 : ๋ฎ์ ๊ฒฝ๊ณ ๊ฐ (0~255)
threshold2 : ๋์ ๊ฒฝ๊ณ ๊ฐ (0~255)
#include <opencv2/opencv.hpp> // OpenCV์์ ์ง์ํ๋ ๋ชจ๋ ๊ธฐ๋ฅ
#include <opencv2/videoio.hpp> // ๋น๋์ค ์ถ์ ๋ฐ ๋ฐฐ๊ฒฝ segmentation๊ณผ ๊ด๋ จ๋ ๋ฃจํด
#include <opencv2/imgcodecs.hpp> // ๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์
์ด ์ ์ธ (Mat ์ด๋ Point๊ฐ ์ ์ธ, ํ๋ ฌ ์ฐ์ฐ ํน์ ๋ฒกํฐ ์ฐ์ฐ)
#include <opencv2/highgui.hpp> // ์๋์ฐ ํ๋ฉด, UI์ฒ๋ฆฌ(์ฌ๋ผ์ด๋, ๋ฒํผ ๋ฑ) ๋ฐ ๋ง์ฐ์ค ์ ์ด ๊ฐ๋ฅ
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
// #1. Read Images Videos and Webcams
// 1) Importing Images
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
string path = "Resources/alpaca.jpg";
Mat img = imread(path);
Mat imgGray;
Mat imgBlur;
Mat imgCanny;
// cvtColor( input Array, output Array, flag)
// : input Array๋ฅผ ์
๋ ฅ๋ฐ์ flag ์ ๋ํ ์ต์
์ผ๋ก ์ด๋ฏธ์ง ์์ฑ๋์ ๋ณ๊ฒฝ
cvtColor(img, imgGray, COLOR_BGR2GRAY);
// GaussianBlur( src, dst, kernel_size, sigma_x, sigma_y, borderType)
// ์ค์๊ฐ์ ๊ฐ์ค์น๋ฅผ ๋ ์ฃผ๊ณ ์ฃผ๋ณ์ ๋ ํ๋ฆฌ๊ฒ
GaussianBlur(img, imgBlur, Size(3, 3), 3, 0);
Canny(imgBlur, imgCanny, 50, 150);
imshow("img", img);
imshow("Gray img", imgGray);
imshow("Gray Blur", imgBlur);
imshow("Canny", imgCanny);
waitKey(0);
}
๐ง Morphology
1) ์นจ์ (erode)
erode( src, dst, kernel, anchor, iteration, borderType, borderValue)
src : ์ ๋ ฅํ ์ด๋ฏธ์ง ๋ณ์ (grayscale ์ด๋ฏธ์ง๋ฅผ ์ ๋ ฅํด์ผํจ)
dst : ํํฐ๊ฐ ์ ์ฉ๋์ด ์ ์ฅ๋ ์ด๋ฏธ์ง ๋ณ์
kernel : ์นจ์์ฐ์ฐ์ ํ mask ํฌ๊ธฐ๋ฅผ ํฌํจํ ํํฐ์ ๋ํ ์ ๋ณด(์ปค๋) ํ๋ ฌ
anchor : ์ปค๋์ด ์ ์ฉ๋ ๋, ๋ณ๊ฒฝ๋ ํฝ์ ์ ์์น, ๊ธฐ๋ณธ์ผ๋ก Point(-1,-1), Point(-1,-1)์ ์ค์
iteration : ๋ฐ๋ณตํ ํ ์, ์นจ์์ฐ์ฐ์ ๋ฐ๋ณตํด์ ์ ์ฉ. ๋๋ฌด ๋ง์ด ๋ฐ๋ณตํ๋ฉด object๊ฐ ๋ค ์์ด์ ธ๋ฒ๋ฆด ์ ์์
borderType : ์ด๋ฏธ์ง ํ ๋๋ฆฌ ๋ถ๋ถ์์ ๊ฒฝ๊ณ ๋ฐ์ ๊ฐ์์ ํฝ์ ๋ค์ ์ฒ๋ฆฌํ ๋ฐฉ๋ฒ ์ ํ (๊ธฐ๋ณธ๊ฐ์ mirror padding )
2) ํฝ์ฐฝ (dilate)
dilate( src, dst, kernel, anchor, iteration, borderType, borderValue)
src : ์ ๋ ฅํ ์ด๋ฏธ์ง ๋ณ์ (grayscale ์ด๋ฏธ์ง๋ฅผ ์ ๋ ฅํด์ผํจ)
dst : ํํฐ๊ฐ ์ ์ฉ๋์ด ์ ์ฅ๋ ์ด๋ฏธ์ง ๋ณ์
kernel : ํฝ์ฐฝ์ฐ์ฐ์ ํ mask ํฌ๊ธฐ๋ฅผ ํฌํจํ ํํฐ์ ๋ํ ์ ๋ณด(์ปค๋) ํ๋ ฌ
anchor : ์ปค๋์ด ์ ์ฉ๋ ๋, ๋ณ๊ฒฝ๋ ํฝ์ ์ ์์น, ๊ธฐ๋ณธ์ผ๋ก Point(-1,-1), Point(-1,-1)์ ์ค์
iteration : ๋ฐ๋ณตํ ํ ์, ์นจ์์ฐ์ฐ์ ๋ฐ๋ณตํด์ ์ ์ฉ. ๋๋ฌด ๋ง์ด ๋ฐ๋ณตํ๋ฉด object๊ฐ ๋ค ์์ด์ ธ๋ฒ๋ฆด ์ ์์
borderType : ์ด๋ฏธ์ง ํ ๋๋ฆฌ ๋ถ๋ถ์์ ๊ฒฝ๊ณ ๋ฐ์ ๊ฐ์์ ํฝ์ ๋ค์ ์ฒ๋ฆฌํ ๋ฐฉ๋ฒ ์ ํ (๊ธฐ๋ณธ๊ฐ์ mirror padding )
#include <opencv2/opencv.hpp> // OpenCV์์ ์ง์ํ๋ ๋ชจ๋ ๊ธฐ๋ฅ
#include <opencv2/videoio.hpp> // ๋น๋์ค ์ถ์ ๋ฐ ๋ฐฐ๊ฒฝ segmentation๊ณผ ๊ด๋ จ๋ ๋ฃจํด
#include <opencv2/imgcodecs.hpp> // ๊ธฐ๋ณธ ๋ฐ์ดํฐ ํ์
์ด ์ ์ธ (Mat ์ด๋ Point๊ฐ ์ ์ธ, ํ๋ ฌ ์ฐ์ฐ ํน์ ๋ฒกํฐ ์ฐ์ฐ)
#include <opencv2/highgui.hpp> // ์๋์ฐ ํ๋ฉด, UI์ฒ๋ฆฌ(์ฌ๋ผ์ด๋, ๋ฒํผ ๋ฑ) ๋ฐ ๋ง์ฐ์ค ์ ์ด ๊ฐ๋ฅ
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
// #1. Read Images Videos and Webcams
// 1) Importing Images
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
string path = "Resources/alpaca.jpg";
Mat img = imread(path);
Mat imgGray;
Mat imgBlur;
Mat imgCanny;
Mat imgDil;
Mat imgErode;
// cvtColor( input Array, output Array, flag)
// : input Array๋ฅผ ์
๋ ฅ๋ฐ์ flag ์ ๋ํ ์ต์
์ผ๋ก ์ด๋ฏธ์ง ์์ฑ๋์ ๋ณ๊ฒฝ
cvtColor(img, imgGray, COLOR_BGR2GRAY);
// GaussianBlur( src, dst, kernel_size, sigma_x, sigma_y, borderType)
// : ์ค์๊ฐ์ ๊ฐ์ค์น๋ฅผ ๋ ์ฃผ๊ณ ์ฃผ๋ณ์ ๋ ํ๋ฆฌ๊ฒ
GaussianBlur(img, imgBlur, Size(3, 3), 3, 0);
// Canny( src, dst, threshold1, threshold2)
// : ๊ฒฝ๊ณ์ ๊ฒ์ถ
Canny(imgBlur, imgCanny, 50, 150);
Mat kernel = getStructuringElement(MORPH_RECT, Size(5, 5));
// Morphology
// For Erosion : erode( src, dst, kernel, anchor, iteration, borderType, borderValue)
// : ํฐ์ ํฝ์
์ ๋๋ฆฌ๋ ์ญํ
erode(imgCanny, imgErode, kernel, Point(-1, -1), 1);
// For Dilation : dilate( src, dst, kernel, anchor, iteration, borderType, borderValue)
// : ํฐ์ ํฝ์
์ ์ค์ด๋ ์ญํ
dilate(imgCanny, imgDil, kernel, Point(-1, -1), 1);
imshow("img", img);
imshow("Gray img", imgGray);
imshow("Gray Blur", imgBlur);
imshow("Canny", imgCanny);
imshow("Dilation", imgDil);
imshow("Erode", imgErode);
waitKey(0);
}

'๐ฉโ๐ป IoT (Embedded) > Image Processing' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (4) Drawing shapes and text (0) | 2023.06.12 |
---|---|
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (3) Resize and Crop (0) | 2023.06.12 |
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (1) Read Images, Videos and Webcam (0) | 2023.06.11 |
EX3 05) [OpenCV๋ก ๋ฐฐ์ฐ๋ C++์ ์์์ฒ๋ฆฌ] Rect_ ํด๋์ค (0) | 2023.01.11 |
EX3 04) [OpenCV๋ก ๋ฐฐ์ฐ๋ C++์ ์์์ฒ๋ฆฌ] Size_ ํด๋์ค (1) | 2023.01.05 |