From 0d9c3ed4701750bfc2c19055829b74373438fd6a Mon Sep 17 00:00:00 2001 From: USW Date: Wed, 7 May 2025 12:06:35 +0900 Subject: [PATCH] =?UTF-8?q?12=ED=9A=8C=EC=B0=A8=20=EC=88=98=EC=97=85=20?= =?UTF-8?q?=EB=81=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Capstone_Design.sln | 24 +++++ ch12/findcts/findcts.vcxproj | 142 +++++++++++++++++++++++++ ch12/findcts/findcts.vcxproj.filters | 27 +++++ ch12/findcts/main.cpp | 133 +++++++++++++++++++++++ ch12/labeling/labeling.vcxproj | 142 +++++++++++++++++++++++++ ch12/labeling/labeling.vcxproj.filters | 27 +++++ ch12/labeling/main.cpp | 61 +++++++++++ resources/images/beta.png | 3 + resources/images/kmap_simple.jpg | 3 + 9 files changed, 562 insertions(+) create mode 100644 ch12/findcts/findcts.vcxproj create mode 100644 ch12/findcts/findcts.vcxproj.filters create mode 100644 ch12/findcts/main.cpp create mode 100644 ch12/labeling/labeling.vcxproj create mode 100644 ch12/labeling/labeling.vcxproj.filters create mode 100644 ch12/labeling/main.cpp create mode 100644 resources/images/beta.png create mode 100644 resources/images/kmap_simple.jpg diff --git a/Capstone_Design.sln b/Capstone_Design.sln index c88a62c..d3db993 100644 --- a/Capstone_Design.sln +++ b/Capstone_Design.sln @@ -69,6 +69,12 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "adaptive", "ch11\adaptive\a EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "morphology", "ch11\morphology\morphology.vcxproj", "{9BD9A147-29ED-4F6C-8BFD-71F03E56332C}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "ch12", "ch12", "{792AFFC9-6147-4A66-B676-D64FB3E62F2E}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "labeling", "ch12\labeling\labeling.vcxproj", "{AC2741E6-3BF6-417D-8CE5-82696760AC35}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "findcts", "ch12\findcts\findcts.vcxproj", "{6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|x64 = Debug|x64 @@ -253,6 +259,22 @@ Global {9BD9A147-29ED-4F6C-8BFD-71F03E56332C}.Release|x64.Build.0 = Release|x64 {9BD9A147-29ED-4F6C-8BFD-71F03E56332C}.Release|x86.ActiveCfg = Release|Win32 {9BD9A147-29ED-4F6C-8BFD-71F03E56332C}.Release|x86.Build.0 = Release|Win32 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Debug|x64.ActiveCfg = Debug|x64 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Debug|x64.Build.0 = Debug|x64 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Debug|x86.ActiveCfg = Debug|Win32 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Debug|x86.Build.0 = Debug|Win32 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Release|x64.ActiveCfg = Release|x64 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Release|x64.Build.0 = Release|x64 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Release|x86.ActiveCfg = Release|Win32 + {AC2741E6-3BF6-417D-8CE5-82696760AC35}.Release|x86.Build.0 = Release|Win32 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Debug|x64.ActiveCfg = Debug|x64 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Debug|x64.Build.0 = Debug|x64 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Debug|x86.ActiveCfg = Debug|Win32 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Debug|x86.Build.0 = Debug|Win32 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Release|x64.ActiveCfg = Release|x64 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Release|x64.Build.0 = Release|x64 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Release|x86.ActiveCfg = Release|Win32 + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -280,6 +302,8 @@ Global {12A25565-0007-412B-9490-9AA28305272D} = {94FE4669-77AC-4D41-8E24-F5DEDEAC930F} {8F873187-A5F8-4D02-ABE9-BF652FE53BC3} = {94FE4669-77AC-4D41-8E24-F5DEDEAC930F} {9BD9A147-29ED-4F6C-8BFD-71F03E56332C} = {94FE4669-77AC-4D41-8E24-F5DEDEAC930F} + {AC2741E6-3BF6-417D-8CE5-82696760AC35} = {792AFFC9-6147-4A66-B676-D64FB3E62F2E} + {6F3AAF9A-D050-4BE9-A2F7-3E0CAB16E69E} = {792AFFC9-6147-4A66-B676-D64FB3E62F2E} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {E1A0C3F4-F16D-4626-86CE-D8484C0C776D} diff --git a/ch12/findcts/findcts.vcxproj b/ch12/findcts/findcts.vcxproj new file mode 100644 index 0000000..970af53 --- /dev/null +++ b/ch12/findcts/findcts.vcxproj @@ -0,0 +1,142 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {6f3aaf9a-d050-4be9-a2f7-3e0cab16e69e} + findcts + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/ch12/findcts/findcts.vcxproj.filters b/ch12/findcts/findcts.vcxproj.filters new file mode 100644 index 0000000..05f0d88 --- /dev/null +++ b/ch12/findcts/findcts.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 소스 파일 + + + + + 리소스 파일 + + + \ No newline at end of file diff --git a/ch12/findcts/main.cpp b/ch12/findcts/main.cpp new file mode 100644 index 0000000..9fc2e91 --- /dev/null +++ b/ch12/findcts/main.cpp @@ -0,0 +1,133 @@ +#include +#include "opencv2/opencv.hpp" +using namespace cv; + +void contours_basic() { + Mat src = imread("../../resources/images/contours.bmp", IMREAD_GRAYSCALE); + + std::vector> 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> contours; + std::vector 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> contours; + std::vector 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> contours; + std::vector 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 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(); +} \ No newline at end of file diff --git a/ch12/labeling/labeling.vcxproj b/ch12/labeling/labeling.vcxproj new file mode 100644 index 0000000..3890137 --- /dev/null +++ b/ch12/labeling/labeling.vcxproj @@ -0,0 +1,142 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {ac2741e6-3bf6-417d-8ce5-82696760ac35} + labeling + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + + + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + \ No newline at end of file diff --git a/ch12/labeling/labeling.vcxproj.filters b/ch12/labeling/labeling.vcxproj.filters new file mode 100644 index 0000000..2def071 --- /dev/null +++ b/ch12/labeling/labeling.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 소스 파일 + + + + + 리소스 파일 + + + \ No newline at end of file diff --git a/ch12/labeling/main.cpp b/ch12/labeling/main.cpp new file mode 100644 index 0000000..b9b82f1 --- /dev/null +++ b/ch12/labeling/main.cpp @@ -0,0 +1,61 @@ +#include +#include "opencv2/opencv.hpp" +using namespace cv; + +void labeling_basic() { + uchar data[] = { + 0, 0, 1, 1, 0, 0, 0, 0, + 1, 1, 1, 1, 0, 0, 1, 0, + 1, 1, 1, 1, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 1, 1, 0, + 0, 0, 0, 1, 1, 1, 1, 0, + 0, 0, 0, 1, 0, 0, 1, 0, + 0, 0, 1, 1, 1, 1, 1, 0, + 0, 0, 0, 0, 0, 0, 0, 0 + }; + + Mat src = Mat(8, 8, CV_8UC1, data) * 255; + + Mat labels; + int cnt = connectedComponents(src, labels); + + std::cout << "src:\n" << src << std::endl; + std::cout << "lables:\n" << labels << std::endl; + std::cout << "number of lables:\n" << cnt << std::endl; +} + +void labling_stats() { + Mat src = imread("../../resources/images/keyboard.bmp", IMREAD_GRAYSCALE); + + Mat bin; + threshold(src, bin, 0, 255, THRESH_BINARY | THRESH_OTSU); + + Mat labels, stats, centroids; + int cnt = connectedComponentsWithStats(bin, labels, stats, centroids); + + Mat dst; + cvtColor(src, dst, COLOR_GRAY2BGR); + + for (int i = 1; i < cnt; i++) { + int* p = stats.ptr(i); + double* c = centroids.ptr(i); + + //if (p[4] < 20) + // continue; + + rectangle(dst, Rect(p[0], p[1], p[2], p[3]), Scalar(0, 255, 255), 2); + char num[30] = { '0' + i, '\0'}; + sprintf_s(num, "%d", i); + putText(dst, num, Point(c[0], c[1]), FONT_HERSHEY_PLAIN, 1.2, Scalar(0, 0, 255)); + } + + imshow("src", src); + imshow("dst", dst); + + waitKey(); + destroyAllWindows(); +} + +int main() { + labling_stats(); +} \ No newline at end of file diff --git a/resources/images/beta.png b/resources/images/beta.png new file mode 100644 index 0000000..6e38f9e --- /dev/null +++ b/resources/images/beta.png @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:f0faf65eec69428ff1683be48488c13a62f0f4579cbe25d37c0091854fb1912f +size 15725 diff --git a/resources/images/kmap_simple.jpg b/resources/images/kmap_simple.jpg new file mode 100644 index 0000000..d31b045 --- /dev/null +++ b/resources/images/kmap_simple.jpg @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:78eab056f304fb021da9c265319e9279871eeae71dbe2e28b09bff0ed0ee4071 +size 83848