diff --git a/.gitignore b/.gitignore index 1115557..eca4edf 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ ################################################################################ /.vs/08_SVM/v16 +/08_SVM/x64 diff --git a/08_SVM.sln b/08_SVM.sln new file mode 100644 index 0000000..a432408 --- /dev/null +++ b/08_SVM.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.33423.256 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "08_SVM", "08_SVM\08_SVM.vcxproj", "{4E634DD5-C0F9-4220-8974-6C607649AF61}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Debug|x64.ActiveCfg = Debug|x64 + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Debug|x64.Build.0 = Debug|x64 + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Debug|x86.ActiveCfg = Debug|Win32 + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Debug|x86.Build.0 = Debug|Win32 + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Release|x64.ActiveCfg = Release|x64 + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Release|x64.Build.0 = Release|x64 + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Release|x86.ActiveCfg = Release|Win32 + {4E634DD5-C0F9-4220-8974-6C607649AF61}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {BA356928-96E5-44E3-906A-F1D6DD0EBA82} + EndGlobalSection +EndGlobal diff --git a/08_SVM/08_SVM.vcxproj b/08_SVM/08_SVM.vcxproj new file mode 100644 index 0000000..65d9a0a --- /dev/null +++ b/08_SVM/08_SVM.vcxproj @@ -0,0 +1,155 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {4e634dd5-c0f9-4220-8974-6c607649af61} + My08SVM + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + + + false + + + + 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 + C:\opencv-4.6.0_X64\build\include + + + Console + true + C:\opencv-4.6.0_X64\build\x64\vc15\lib + opencv_world460d.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + C:\opencv-4.6.0_X64\build\include + + + Console + true + true + true + C:\opencv-4.6.0_X64\build\x64\vc15\lib + opencv_world460.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/08_SVM/08_SVM.vcxproj.filters b/08_SVM/08_SVM.vcxproj.filters new file mode 100644 index 0000000..d6962b3 --- /dev/null +++ b/08_SVM/08_SVM.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {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/08_SVM/08_SVM.vcxproj.user b/08_SVM/08_SVM.vcxproj.user new file mode 100644 index 0000000..88a5509 --- /dev/null +++ b/08_SVM/08_SVM.vcxproj.user @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/08_SVM/digits.png b/08_SVM/digits.png new file mode 100644 index 0000000..01cdd29 Binary files /dev/null and b/08_SVM/digits.png differ diff --git a/08_SVM/svm_hog.cpp b/08_SVM/svm_hog.cpp new file mode 100644 index 0000000..a495123 --- /dev/null +++ b/08_SVM/svm_hog.cpp @@ -0,0 +1,209 @@ +#include +#include +#include +#include +//#include + +#include +#include + +using namespace std; +using namespace cv; +using namespace cv::ml; + +void svmplane() +{ + Mat train = Mat_({ 8, 2 }, + { + 150, 200, 200, 250, 100, 250, 150, 300, + 350, 100, 400, 200, 400, 300, 350, 400 }); + Mat label = Mat_({ 8, 1 }, { 0, 0, 0, 0, 1, 1, 1, 1 }); + + Ptr svm = SVM::create(); + svm->setType(SVM::Types::C_SVC); + svm->setKernel(SVM::KernelTypes::RBF); + svm->trainAuto(train, ROW_SAMPLE, label); + Mat img = Mat::zeros(Size(500, 500), CV_8UC3); + + for (int j = 0; j < img.rows; j++) + { + for (int i = 0; i < img.cols; i++) + { + Mat test = Mat_({ 1, 2 }, { (float)i, (float)j }); + int res = cvRound(svm->predict(test)); + if (res == 0) + img.at(j, i) = Vec3b(128, 128, 255); // R + else + img.at(j, i) = Vec3b(128, 255, 128); // G + } + } + + for (int i = 0; i < train.rows; i++) + { + int x = cvRound(train.at(i, 0)); + int y = cvRound(train.at(i, 1)); + int l = label.at(i, 0); + if (1 == 0) + circle(img, Point(x, y), 5, Scalar(0, 0, 128), -1, LINE_AA); // R + else + circle(img, Point(x, y), 5, Scalar(0, 128, 0), -1, LINE_AA); // G + } + + imshow("svm", img); + imwrite("svm_result1.png", img); + waitKey(); + + return; +} + + +// // +Ptr train_hog_svm(const HOGDescriptor& hog); +void on_mouse(int event, int X, int y, int flags, void* userdata); + +void svmdigits() +{ +#if _DEBUG + cout << "svndigits.exe should be built as Release mode !" << endl; + return; +#endif + + HOGDescriptor hog(Size(20, 20), Size(10, 10), Size(5, 5), Size(5, 5), 9); + Ptr svm = train_hog_svm(hog); + + if (svm.empty()) + { + cerr << "Training failed! " << endl; + return; + } + + Mat img = Mat::zeros(400, 400, CV_8U); + imshow("img", img); + setMouseCallback("img", on_mouse, (void*)&img); + + while (true) + { + int c = waitKey(); + + if (c == 27) + break; + else if (c == ' ') + { + Mat img_resize; + resize(img, img_resize, Size(20, 20), 0, 0, INTER_AREA); + + vector desc; + hog.compute(img_resize, desc); + Mat desc_mat(desc); + int res = cvRound(svm->predict(desc_mat.t())); + cout << res << endl; + + img.setTo(0); + imshow("img", img); + } + } + + return; +} + +Ptr train_hog_svm(const HOGDescriptor& hog) +{ + Mat digits = imread("digits.png", IMREAD_GRAYSCALE); + + if (digits.empty()) + { + cerr << "Image load failed!" << endl; + return 0; + } + + Mat train_hog, train_labels; + + for (int j = 0; j < 50; j++) + { + for (int i = 0; i < 100; i++) + { + Mat roi = digits(Rect(i * 20, j * 20, 20, 20)); + vector desc; + + hog.compute(roi, desc); + Mat desc_mat(desc); + + train_hog.push_back(desc_mat.t()); + train_labels.push_back(j / 5); + } + } + + Ptr svm = SVM::create(); + svm->setType(SVM::Types::C_SVC); + svm->setKernel(SVM::KernelTypes::RBF); + svm->setC(2.5); + svm->setGamma(0.50625); + svm->train(train_hog, ROW_SAMPLE, train_labels); + + return svm; +} + +Point ptPrev(-1, -1); +void on_mouse(int event, int x, int y, int flags, void* userdata) +{ + Mat img = *(Mat*)userdata; + + if (event == EVENT_LBUTTONDOWN) + { + ptPrev = Point(x, y); + } + else if (event == EVENT_LBUTTONUP) + { + ptPrev = Point(-1, -1); + } + else if (event == EVENT_MOUSEMOVE && (flags & EVENT_FLAG_LBUTTON)) + { + line(img, ptPrev, Point(x, y), Scalar::all(255), 40, LINE_AA, 0); + ptPrev = Point(x, y); + imshow("img", img); + imwrite("svm_result2.png", img); + } +} + +// // +void hog() +{ + VideoCapture cap("vtest.avi"); + if (!cap.isOpened()) + { + cerr << "Video open failed!" << endl; + return; + } + + HOGDescriptor hog; + hog.setSVMDetector(HOGDescriptor::getDefaultPeopleDetector()); + + Mat frame; + while (true) + { + cap >> frame; + if (frame.empty()) + break; + + vector detected; + hog.detectMultiScale(frame, detected); + + for (Rect r : detected) { + Scalar c = Scalar(rand() % 256, rand() % 256, rand() % 256); + rectangle(frame, r, c, 3); + } + + imshow("frame", frame); + + if (waitKey(10) == 27) + break; + } +} + + +int main() +{ + svmplane(); + svmdigits(); + //hog(); +} \ No newline at end of file diff --git a/08_SVM/svm_result1.png b/08_SVM/svm_result1.png new file mode 100644 index 0000000..7bf69c0 Binary files /dev/null and b/08_SVM/svm_result1.png differ diff --git a/08_SVM/svm_result2.png b/08_SVM/svm_result2.png new file mode 100644 index 0000000..dd8fc3b Binary files /dev/null and b/08_SVM/svm_result2.png differ diff --git a/08_SVM/vtest.avi b/08_SVM/vtest.avi new file mode 100644 index 0000000..965ab12 Binary files /dev/null and b/08_SVM/vtest.avi differ diff --git a/x64/Debug/08_SVM.exe b/x64/Debug/08_SVM.exe new file mode 100644 index 0000000..dc6f67d Binary files /dev/null and b/x64/Debug/08_SVM.exe differ diff --git a/x64/Debug/08_SVM.pdb b/x64/Debug/08_SVM.pdb new file mode 100644 index 0000000..87629ed Binary files /dev/null and b/x64/Debug/08_SVM.pdb differ diff --git a/x64/Debug/opencv_world460d.dll b/x64/Debug/opencv_world460d.dll new file mode 100644 index 0000000..6921313 Binary files /dev/null and b/x64/Debug/opencv_world460d.dll differ diff --git a/x64/Release/08_SVM.exe b/x64/Release/08_SVM.exe new file mode 100644 index 0000000..0f8b8fa Binary files /dev/null and b/x64/Release/08_SVM.exe differ diff --git a/x64/Release/08_SVM.pdb b/x64/Release/08_SVM.pdb new file mode 100644 index 0000000..3448372 Binary files /dev/null and b/x64/Release/08_SVM.pdb differ diff --git a/x64/Release/opencv_world460.dll b/x64/Release/opencv_world460.dll new file mode 100644 index 0000000..bca44e4 Binary files /dev/null and b/x64/Release/opencv_world460.dll differ