๐ ๊ณต๋ถํ๋ ์ง์ง์ํ์นด๋ ์ฒ์์ด์ง?
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (4) Drawing shapes and text ๋ณธ๋ฌธ
[C++ ๋ก OpenCV ๊ตฌํํ๊ธฐ] (4) Drawing shapes and text
์ง์ง์ํ์นด 2023. 6. 12. 10:58<๋ณธ ๋ธ๋ก๊ทธ๋ Murtaza's Workshop ์ ์ ํ๋ธ๋ฅผ ์ฐธ๊ณ ํด์ ๊ณต๋ถํ๋ฉฐ ์์ฑํ์์ต๋๋ค :-)>
=> LEARN OPENCV C++ in 4 HOURS | Including 3x Projects | Computer Vision
๐ Drawing shapes
๐ง [Default] Drawing shapes
#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;
// #4. Drawing shapes and text
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
Mat img(512, 512, CV_8UC3, Scalar(255, 0, 255)); // Blank image
imshow("Image", img);
waitKey(0);
}
๐ง[Circle] Drawing shapes
circle( img, Point(x, y), radius, Scalar(b,g,r), thickness, lineType, shift )
img : ์์ ๊ทธ๋ ค๋ฃ์ ์ด๋ฏธ์ง ์ ๋ ฅ
Point(x, y) : ์์ ์ค์ฌ ์์น
radius : ์์ ๋ฐ์ง๋ฆ, ์ซ์๋ก๋ง ์ ๋ ฅ
scalar(b, g, r) : ์ ์ ์์์ b, g, r ์์ผ๋ก ์ ๋ ฅ
thickness : ์ ์ ๊ตต๊ธฐ๋ฅผ ์ค์ ํ ์ ์์
lineType : ์ ์ ํ์ ์ ์ค์ ๊ฐ๋ฅ,
โบ FILLED : ์์ ์ฑ์ ๋ฃ์ , -1
โบ LINE_4 : 4 connected line , 4
โบ LINE_8 : 8 connected line , 8
โบ LINE_AA : ์ํฐ์จ๋ฆฌ์ด์ฑ , 16
shift : ๊ทธ๋ฅ 0
#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;
// #4. Drawing shapes and text
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
Mat img(512, 512, CV_8UC3, Scalar(255, 0, 100)); // Blank image
// circle( img, Point(x, y), radius, Scalar(b,g,r), thickness, lineType, shift )
circle(img, Point(256, 256), 155, Scalar(0, 69, 255), 10);
imshow("Image", img);
waitKey(0);
}
๐ง[Rectangle] Drawing shapes
rectangle( img , Rect , Scalar(b, g, r), thickness, lineType, shift )
img : ์ฌ๊ฐํ์ ๊ทธ๋ ค๋ฃ์ ์ด๋ฏธ์ง ์ ๋ ฅ
Rect : ์ฌ๊ฐํ์ ๋ฒ์
โบ Rect( Point(x1, y1), Point(x2, y2) ) : ๋ ์ขํ๋ฅผ ์ด์ฉํ ์ฌ๊ฐํ ๋ฒ์
โบ Rect( x, y, width, height ) : x, y ์ขํ์์๋ถํฐ width ์ height ๊น์ง์ ์ฌ๊ฐํ ๋ฒ์
๋ ๋ฐฉ๋ฒ ์ค ํ 1
scalar(b, g, r) : ์ ์ ์์์ b, g, r ์์ผ๋ก ์ ๋ ฅ
thickness : ์ ์ ๊ตต๊ธฐ๋ฅผ ์ค์ ํ ์ ์์
lineType : ์ ์ ํ์ ์ ์ค์ ๊ฐ๋ฅ,
โบ FILLED : ์์ ์ฑ์ ๋ฃ์ , -1
โบ LINE_4 : 4 connected line , 4
โบ LINE_8 : 8 connected line , 8
โบ LINE_AA : ์ํฐ์จ๋ฆฌ์ด์ฑ , 16
shift : ๊ทธ๋ฅ 0
#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;
// #4. Drawing shapes and text
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
Mat img(512, 512, CV_8UC3, Scalar(255, 0, 100)); // Blank image
// circle( img, Point(x, y), radius, Scalar(b,g,r), thickness, lineType, shift )
circle(img, Point(256, 256), 155, Scalar(0, 69, 255), 10);
// rectangle( img , Rect , Scalar(b, g, r), thickness, lineType, shift )
rectangle(img, Point(130, 226), Point(382, 286), Scalar(255, 255, 0), FILLED);
imshow("Image", img);
waitKey(0);
}
๐ง[Line] Drawing shapes
line( Input Output Array, pt1, pt2, scalar(b,g,r), thickness, lineType, shift )
Input Output Array : ์ง์ ์ ๊ทธ๋ ค๋ฃ์ ์ด๋ฏธ์ง ์ ๋ ฅ
pt1, pt2 : ์ง์ ์ ์ ๋ ์ขํ์ ์ ๋ ฅ, Point(x, y)
scalar(b, g, r) : ์ง์ ์ ์์์ b, g, r ์์ผ๋ก ์ ๋ ฅ
thickness : ์ง์ ์ ๊ตต๊ธฐ๋ฅผ ์ค์ ํ ์ ์์
lineType : ์ง์ ์ ํ์ ์ ์ค์ ๊ฐ๋ฅ,
โบ FILLED : ์์ ์ฑ์ ๋ฃ์ , -1
โบ LINE_4 : 4 connected line , 4
โบ LINE_8 : 8 connected line , 8
โบ LINE_AA : ์ํฐ์จ๋ฆฌ์ด์ฑ , 16
shift : ๊ทธ๋ฅ 0
#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;
// #4. Drawing shapes and text
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
Mat img(512, 512, CV_8UC3, Scalar(255, 0, 100)); // Blank image
// circle( img, Point(x, y), radius, Scalar(b,g,r), thickness, lineType, shift )
circle(img, Point(256, 256), 155, Scalar(0, 69, 255), 10);
// rectangle( img , Rect , Scalar(b, g, r), thickness, lineType, shift )
rectangle(img, Point(130, 226), Point(382, 286), Scalar(255, 255, 0), FILLED);
// line( Input Output Array, pt1, pt2, scalar(b,g,r), thickness, lineType, shift )
line(img, Point(130, 296), Point(382, 454), Scalar(255, 12, 4), 3);
imshow("Image", img);
waitKey(0);
}
๐ Drawing text
๐งText
putText( img, string, Point(x,y), font face, font scale, color, thickness, line type, bottom left origin )
img: ์ ๋ ฅํ ์ด๋ฏธ์ง, ์ ๋ ฅํ ์ด๋ฏธ์ง์ ํ ์คํธ๋ฅผ ์ฝ์ ํ๊ฒ ๋๋ค.
string : ์ ๋ ฅํ๊ณ ์ถ์ ํ ์คํธ, char ์๋ฃํ์ด๋ฉด ๋๋ค.
Point(x, y) : ํ ์คํธ๋ฅผ ์ฝ์ ํ ์์น, xy์ขํ๊ณ๋ก ์ ๋ ฅํด์ผ ํ๋ค.
font face : ๊ธ๊ผด
โบ ์ธ๋ฆฌํ(Serif)๋ ํ ๋์ด ๋์๋ฐ๋์ฒ๋ผ ๋ ์นด๋กญ๊ฒ ํ์ด๋์จ ํฐํธ (๋ช ์กฐ์ฒด)
โบ ์ฐ ์ธ๋ฆฌํ(San-Serif)๋ ์ธ๋ฆฌํ์ ๋ฐ๋๋ก, ํ์ด๋์จ ๋ถ๋ถ์ด ์์ (๊ณ ๋์ฒด)
โบ Option / Value / Explain
โบ FONT_HERSHEY_SIMPLEX / 0 / ์ค๊ฐ ํฌ๊ธฐ ์ฐ์ธ๋ฆฌํ ํฐํธ
โบ FONT_HERSHEY_PLAIN / 1 / ์์ ํฌ๊ธฐ ์ฐ์ธ๋ฆฌํ ํฐํธ
โบ FONT_HERSHEY_DUPLEX / 2 / 2์ค ์ฐ์ธ๋ฆฌํ ํฐํธ
โบ FONT_HERSHEY_COMPLEX / 3 / ์ค๊ฐ ํฌ๊ธฐ ์ธ๋ฆฌํ ํฐํธ
โบ FONT_HERSHEY_TRIPLEX / 4 / 3์ค ์ธ๋ฆฌํ ํฐํธ
โบ FONT_HERSHEY_COMPLEX_SMALL / 5 / COMPLEX ๋ณด๋ค ์์ ํฌ๊ธฐ
โบ FONT_HERSHEY_SCRIPT_SIMPLEX / 6 / ํ๊ธฐ์ฒด ์คํ์ผ ํฐํธ
โบ FONT_HERSHEY_SCRIPT_COMPLEX / 7 / ๋ณต์กํ ํ๊ธฐ์ฒด ์คํ์ผ
โบ FONT_ITALIC / 16 / ์ดํ๋ฆญ์ฒด๋ฅผ ์ํ ํ๋๊ทธ
font scale : ํฐํธ ํฌ๊ธฐ
color : scalar ๊ฐ์ ์์ ์ ๋ณด ์ ๋ ฅ
โบ scalar( Blue, Green, Red)
thickness : ํฐํธ ๊ตต๊ธฐ
line type : ๊ธ์ ์ ์ ํํ
bottom left origin : ์์์ ์์ ์ขํ๋ฅผ ์ข์ธก ํ๋จ์ผ๋ก ์ค์
#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;
// #4. Drawing shapes and text
void main() {
// Mat์ ์ด๋ฏธ์ง๋ฅผ ๋ด์ ๊ฐ์ฒด์ด๋ค. ํ๋ ฌ ๊ตฌ์ฑ
Mat img(512, 512, CV_8UC3, Scalar(255, 0, 100)); // Blank image
// circle( img, Point(x, y), radius, Scalar(b,g,r), thickness, lineType, shift )
circle(img, Point(256, 256), 155, Scalar(0, 69, 255), 10);
// rectangle( img , Rect , Scalar(b, g, r), thickness, lineType, shift )
rectangle(img, Point(130, 226), Point(382, 286), Scalar(255, 255, 0), FILLED);
// line( Input Output Array, pt1, pt2, scalar(b,g,r), thickness, lineType, shift )
line(img, Point(130, 296), Point(382, 454), Scalar(255, 12, 4), 3);
// putText( img, string, Point(x,y), font face, font scale, color, thickness, line type, bottom left origin )
putText(img, "gani hello", Point(147, 242), FONT_HERSHEY_DUPLEX, 2, Scalar(0, 34, 255), 2);
imshow("Image", img);
waitKey(0);
}