Files
Capstone_Design/ch12/findcts/main.cpp
2025-05-07 12:06:35 +09:00

133 lines
3.7 KiB
C++

#include <iostream>
#include "opencv2/opencv.hpp"
using namespace cv;
void contours_basic() {
Mat src = imread("../../resources/images/contours.bmp", IMREAD_GRAYSCALE);
std::vector<std::vector<Point>> contours;
findContours(src, contours, RETR_LIST, CHAIN_APPROX_NONE);
Mat dst;
cvtColor(src, dst, COLOR_GRAY2BGR);
for (int i = 0; i < contours.size(); i++) {
Scalar c(rand() & 255, rand() & 255, rand() & 255);
drawContours(dst, contours, i, c, 2);
}
imshow("src", src);
imshow("dst", dst);
waitKey();
destroyAllWindows();
}
void contours_hier() {
Mat src = imread("../../resources/images/contours.bmp", IMREAD_GRAYSCALE);
std::vector<std::vector<Point>> contours;
std::vector<Vec4i> hirarchy;
findContours(src, contours, hirarchy, RETR_CCOMP, CHAIN_APPROX_NONE);
Mat dst;
cvtColor(src, dst, COLOR_GRAY2BGR);
for (int i = 0; i >= 0; i = hirarchy[i][0]) {
Scalar c(rand() & 255, rand() & 255, rand() & 255);
drawContours(dst, contours, i, c, -1, LINE_8, hirarchy);
}
for (int i = 0; i < contours.size(); i++) {
std::cout << "Hirarchy of contour index: " << i << "-->" << hirarchy[i] << std::endl;
}
imshow("src", src);
imshow("dst", dst);
waitKey();
destroyAllWindows();
}
void contour_apps() {
Mat src = imread("../../resources/images/beta.png", IMREAD_GRAYSCALE);
Mat bin;
std::vector<std::vector<Point>> contours;
std::vector<Vec4i> hirarchy;
threshold(src, bin, 128, 255, THRESH_BINARY | THRESH_OTSU);
morphologyEx(bin, bin, MORPH_OPEN, Mat(), Point(-1, -1), 1);
findContours(bin, contours, hirarchy, RETR_TREE, CHAIN_APPROX_NONE);
Mat dst;
cvtColor(src, dst, COLOR_GRAY2BGR);
for (int idx = 0; idx >= 0; idx = hirarchy[idx][0]) {
Scalar c(rand() & 255, rand() & 255, rand() & 255);
drawContours(dst, contours, idx, c, 2, LINE_AA, hirarchy, 2, Point(0, 0));
Rect rect = boundingRect(contours[idx]);
rectangle(dst, rect, Scalar(0, 0, 255), 2, LINE_AA, 0);
RotatedRect rrect = minAreaRect(contours[idx]);
Point2f vertices[4];
rrect.points(vertices);
for (int i = 0; i < 4; i++)
line(dst, vertices[i], vertices[(i + 1 & 3)], Scalar(255, 0, 0), 2, LINE_AA, 0);
float angle = rrect.angle;
std::cout << "\nrotated angle: " << cvRound(angle) << std::endl;
Point2f center;
float radius;
minEnclosingCircle(contours[idx], center, radius);
circle(dst, center, radius, Scalar(0, 255, 255), 2, LINE_AA, 0);
double len = arcLength(contours[idx], true);
std::cout << "\nlength of contour: " << cvRound(len) << std::endl;
double area = contourArea(contours[idx], false);
std::cout << "\narea of contour: " << cvRound(area) << std::endl;
}
imshow("dst", dst);
waitKey();
destroyAllWindows();
}
void contour_approximation() {
Mat src = imread("../../resources/images/kmap_simple.jpg", IMREAD_GRAYSCALE);
Mat bin;
std::vector<std::vector<Point>> contours;
std::vector<Vec4i> hirarchy;
threshold(src, bin, 128, 255, THRESH_BINARY | THRESH_OTSU);
findContours(bin, contours, hirarchy, RETR_TREE, CHAIN_APPROX_NONE);
Mat dst;
cvtColor(src, dst, COLOR_GRAY2BGR);
for (int idx = 0; idx >= 0; idx = hirarchy[idx][0]) {
Scalar c(rand() & 255, rand() & 255, rand() & 255);
drawContours(dst, contours, idx, c, 2, LINE_AA, hirarchy, 2, Point(0, 0));
std::vector<Point> approxCurve;
approxPolyDP(contours[idx], approxCurve, arcLength(contours[idx], true) * 0.005, true);
int no_vertex = approxCurve.size();
std::cout << "The nimber of vertex: " << no_vertex << std::endl;
for (int v = 0; v < no_vertex; v++) {
circle(dst, approxCurve[v], 3, Scalar(0, 0, 255), 2);
line(dst, approxCurve[v], approxCurve[(v+1) % no_vertex], Scalar(255, 255, 0), 2);
}
}
imshow("src", src);
imshow("dst", dst);
waitKey();
destroyAllWindows();
}
int main() {
contour_approximation();
}