From afba01b708299659b8a4687b6425549c8fa593e8 Mon Sep 17 00:00:00 2001 From: ZhenyeLi <806957048@qq.com> Date: Tue, 5 Nov 2024 14:21:10 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=BB=91=E8=89=B2?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加黑色识别函数 --- cotton_color.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cotton_color.cpp b/cotton_color.cpp index 877fc36..04a16a4 100644 --- a/cotton_color.cpp +++ b/cotton_color.cpp @@ -54,6 +54,11 @@ void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map& params) +{ + outputImage = Mat::zeros(inputImage.size(), CV_8UC1); +} + string openFileDialog() { // 初始化文件选择对话框 OPENFILENAME ofn; // 文件对话框结构 From 4670045074ea867f4321d581a265986b3b3d0275 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Tue, 5 Nov 2024 15:19:06 +0800 Subject: [PATCH 2/5] add a test func --- cotton_color.cpp | 1 + cotton_color.vcxproj | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/cotton_color.cpp b/cotton_color.cpp index 877fc36..5d3df12 100644 --- a/cotton_color.cpp +++ b/cotton_color.cpp @@ -85,6 +85,7 @@ string openFileDialog() { } +void test() {} Mat readImage() { // 读取输入图像 string imagePath = openFileDialog(); diff --git a/cotton_color.vcxproj b/cotton_color.vcxproj index fd94705..8a13bfb 100644 --- a/cotton_color.vcxproj +++ b/cotton_color.vcxproj @@ -104,10 +104,13 @@ true _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true + E:\opencv4.10\opencv\build\include;%(AdditionalIncludeDirectories) Console true + E:\opencv4.10\opencv\build\x64\vc16\bin;E:\opencv4.10\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories) + Comdlg32.lib;opencv_world4100.lib;%(AdditionalDependencies) @@ -118,15 +121,15 @@ true NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true - C:\Users\ZLSDKJ\source\opencv\build\include;%(AdditionalIncludeDirectories) + E:\opencv4.10\opencv\build\include;%(AdditionalIncludeDirectories) Console true true true - C:\Users\ZLSDKJ\source\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories) - Comdlg32.lib;opencv_world4100.lib;%(AdditionalDependencies) + E:\opencv4.10\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories) + Comdlg32.lib;opencv_world4100.lib;opencv_world4100d.lib;%(AdditionalDependencies) From d1c0f2f9f89090ee49302d44d62899dc7e1814e7 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Thu, 7 Nov 2024 13:57:17 +0800 Subject: [PATCH 3/5] add GreenDetection add GreenDetection --- .idea/cotton_color.iml | 8 +++ .../inspectionProfiles/profiles_settings.xml | 6 ++ .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/vcs.xml | 6 ++ .idea/workspace.xml | 66 +++++++++++++++++++ cotton_color.cpp | 37 +++++++++-- cotton_color.vcxproj | 1 + 8 files changed, 131 insertions(+), 5 deletions(-) create mode 100644 .idea/cotton_color.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 .idea/workspace.xml diff --git a/.idea/cotton_color.iml b/.idea/cotton_color.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/cotton_color.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..060d2c5 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..876a814 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..7bc015d --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,66 @@ + + + + + + + + + + + + + + + + + + + + + + + + 1730791232605 + + + + + + + \ No newline at end of file diff --git a/cotton_color.cpp b/cotton_color.cpp index 5d3df12..1726e43 100644 --- a/cotton_color.cpp +++ b/cotton_color.cpp @@ -52,7 +52,33 @@ void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map& params) { + // 从参数映射中获取饱和度阈值 + int green = params.at("green"); + // 将输入图像从 BGR 转换为 HSV + Mat lab_image; + cvtColor(inputImage, lab_image, cv::COLOR_BGR2Lab); + + // 定义偏绿色的Lab范围(具体值可能需要调整) + Scalar lower_green_lab(101, 101, 95); + Scalar upper_green_lab(135, 120, green); + + // 创建掩膜 + Mat mask_lab; + inRange(lab_image, lower_green_lab, upper_green_lab, mask_lab); + + // 通过掩膜提取偏绿色部分 + Mat masked_image_lab; + bitwise_and(inputImage, inputImage, masked_image_lab, mask_lab); + + // 显示结果 + imshow("Original Image", inputImage); + imshow("Mask Lab", mask_lab); + //cv::imshow("Masked Image Lab", masked_image_lab); + waitKey(0); + destroyAllWindows(); +} string openFileDialog() { // 初始化文件选择对话框 @@ -85,7 +111,7 @@ string openFileDialog() { } -void test() {} + Mat readImage() { // 读取输入图像 string imagePath = openFileDialog(); @@ -109,7 +135,7 @@ Mat readImage() { int main() { // 读取输入图像 - Mat inputImage = readImage(); + Mat inputImage = readImage(); if (inputImage.empty()) { cout << "Error: Could not load image." << endl; @@ -121,10 +147,10 @@ int main() { // 使用 map 模拟 JSON 参数传递 map params; - params["saturationThreshold"] = 100; // 设置饱和度阈值为100 + params["green"] =134; // 设置饱和度阈值为100 // 调用鲜艳颜色检测函数 - vibrantColorDetection(inputImage, outputImage, params); + vibrantGreenDetection(inputImage, outputImage, params); // 显示原图和检测到的鲜艳区域 imshow("Original Image", inputImage); @@ -133,4 +159,5 @@ int main() { // 等待用户按键 waitKey(0); return 0; -} \ No newline at end of file +} + diff --git a/cotton_color.vcxproj b/cotton_color.vcxproj index 8a13bfb..2c944c3 100644 --- a/cotton_color.vcxproj +++ b/cotton_color.vcxproj @@ -122,6 +122,7 @@ NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true E:\opencv4.10\opencv\build\include;%(AdditionalIncludeDirectories) + ProgramDatabase Console From 2590c25e2c9d985605ade12b1e6415932352de53 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Thu, 7 Nov 2024 14:58:52 +0800 Subject: [PATCH 4/5] Revert "add GreenDetection" This reverts commit d1c0f2f9f89090ee49302d44d62899dc7e1814e7. --- .idea/cotton_color.iml | 8 --- .../inspectionProfiles/profiles_settings.xml | 6 -- .idea/misc.xml | 4 -- .idea/modules.xml | 8 --- .idea/vcs.xml | 6 -- .idea/workspace.xml | 66 ------------------- cotton_color.cpp | 37 ++--------- cotton_color.vcxproj | 1 - 8 files changed, 5 insertions(+), 131 deletions(-) delete mode 100644 .idea/cotton_color.iml delete mode 100644 .idea/inspectionProfiles/profiles_settings.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml diff --git a/.idea/cotton_color.iml b/.idea/cotton_color.iml deleted file mode 100644 index d0876a7..0000000 --- a/.idea/cotton_color.iml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml deleted file mode 100644 index 105ce2d..0000000 --- a/.idea/inspectionProfiles/profiles_settings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 060d2c5..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 876a814..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 7bc015d..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,66 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - 1730791232605 - - - - - - - \ No newline at end of file diff --git a/cotton_color.cpp b/cotton_color.cpp index 713328b..b7933ed 100644 --- a/cotton_color.cpp +++ b/cotton_color.cpp @@ -52,33 +52,7 @@ void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map& params) { - // 从参数映射中获取饱和度阈值 - int green = params.at("green"); - // 将输入图像从 BGR 转换为 HSV - Mat lab_image; - cvtColor(inputImage, lab_image, cv::COLOR_BGR2Lab); - - // 定义偏绿色的Lab范围(具体值可能需要调整) - Scalar lower_green_lab(101, 101, 95); - Scalar upper_green_lab(135, 120, green); - - // 创建掩膜 - Mat mask_lab; - inRange(lab_image, lower_green_lab, upper_green_lab, mask_lab); - - // 通过掩膜提取偏绿色部分 - Mat masked_image_lab; - bitwise_and(inputImage, inputImage, masked_image_lab, mask_lab); - - // 显示结果 - imshow("Original Image", inputImage); - imshow("Mask Lab", mask_lab); - //cv::imshow("Masked Image Lab", masked_image_lab); - waitKey(0); - destroyAllWindows(); -} void blackColorDetection(const Mat& inputImage, Mat& outputImage, const map& params) { @@ -116,7 +90,7 @@ string openFileDialog() { } - +void test() {} Mat readImage() { // 读取输入图像 string imagePath = openFileDialog(); @@ -140,7 +114,7 @@ Mat readImage() { int main() { // 读取输入图像 - Mat inputImage = readImage(); + Mat inputImage = readImage(); if (inputImage.empty()) { cout << "Error: Could not load image." << endl; @@ -152,10 +126,10 @@ int main() { // 使用 map 模拟 JSON 参数传递 map params; - params["green"] =134; // 设置饱和度阈值为100 + params["saturationThreshold"] = 100; // 设置饱和度阈值为100 // 调用鲜艳颜色检测函数 - vibrantGreenDetection(inputImage, outputImage, params); + vibrantColorDetection(inputImage, outputImage, params); // 显示原图和检测到的鲜艳区域 imshow("Original Image", inputImage); @@ -164,5 +138,4 @@ int main() { // 等待用户按键 waitKey(0); return 0; -} - +} \ No newline at end of file diff --git a/cotton_color.vcxproj b/cotton_color.vcxproj index 2c944c3..8a13bfb 100644 --- a/cotton_color.vcxproj +++ b/cotton_color.vcxproj @@ -122,7 +122,6 @@ NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true E:\opencv4.10\opencv\build\include;%(AdditionalIncludeDirectories) - ProgramDatabase Console From cd144620e369c1d2cc7c4f46024120dc594fa0c0 Mon Sep 17 00:00:00 2001 From: zjc-zjc-123 <1714105370@qq.com> Date: Fri, 8 Nov 2024 12:24:07 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=9B=BE=E5=83=8F?= =?UTF-8?q?=E7=BC=A9=E6=94=BE=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cotton_color.cpp | 81 +++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 36 deletions(-) diff --git a/cotton_color.cpp b/cotton_color.cpp index b7933ed..0fb1e26 100644 --- a/cotton_color.cpp +++ b/cotton_color.cpp @@ -5,32 +5,35 @@ #include #include // 包含文件对话框相关的函数 - using namespace cv; using namespace std; - /** - * @brief 鲜艳色彩检测函数,通过饱和度阈值检测输入图像中鲜艳的颜色区域。 - * - * 此函数将输入图像从 BGR 色彩空间转换到 HSV 色彩空间,并提取出饱和度 (S) 通道。然后,通过设置饱和度阈值, - * 来检测图像中饱和度大于阈值的区域,标记为输出图像的鲜艳颜色区域。 + * @brief 鲜艳绿色检测函数,通过指定的 Lab 色彩范围检测输入图像中的绿色区域。 * * @param inputImage 输入图像,类型为 cv::Mat,要求为 BGR 色彩空间。 - * @param outputImage 输出图像,类型为 cv::Mat,输出图像将标记出鲜艳颜色区域,原始图像尺寸。 - * @param saturationThreshold 饱和度阈值,类型为 int,用于过滤低饱和度的区域,范围通常为 0 到 255。 - * 饱和度高于此阈值的区域将被认为是鲜艳的颜色。 - * - * @note 饱和度阈值越高,输出图像将只保留更为鲜艳的区域。 - * 若饱和度阈值过低,可能会检测到过多的区域。 + * @param outputImage 输出图像,类型为 cv::Mat,输出图像将包含检测到的绿色区域。 + * @param params 参数映射,用于传递各种可配置的参数,如绿色阈值等。 */ - /** - * @brief 鲜艳色彩检测函数,通过饱和度阈值检测输入图像中鲜艳的颜色区域。 - * - * @param inputImage 输入图像,类型为 cv::Mat,要求为 BGR 色彩空间。 - * @param outputImage 输出图像,类型为 cv::Mat,输出图像将标记出鲜艳颜色区域,原始图像尺寸。 - * @param params 参数映射,用于传递各种可配置的参数,如饱和度阈值等。 - */ +void vibrantGreenDetection(const Mat& inputImage, Mat& outputImage, const map& params) { + // 从参数映射中获取绿色阈值 + int green = params.at("green"); + + // 将输入图像从 BGR 转换为 Lab + Mat lab_image; + cvtColor(inputImage, lab_image, cv::COLOR_BGR2Lab); + + // 定义偏绿色的 Lab 范围(具体值可能需要调整) + Scalar lower_green_lab(101, 101, 95); + Scalar upper_green_lab(135, 120, green); + + // 创建掩膜 + Mat mask_lab; + inRange(lab_image, lower_green_lab, upper_green_lab, mask_lab); + + // 通过掩膜提取偏绿色部分,将结果存储在 outputImage 中 + bitwise_and(inputImage, inputImage, outputImage, mask_lab); +} void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map& params) { // 从参数映射中获取饱和度阈值 int saturationThreshold = params.at("saturationThreshold"); @@ -52,13 +55,6 @@ void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map& params) -{ - outputImage = Mat::zeros(inputImage.size(), CV_8UC1); -} - string openFileDialog() { // 初始化文件选择对话框 OPENFILENAME ofn; // 文件对话框结构 @@ -89,8 +85,6 @@ string openFileDialog() { return ""; // 如果用户取消,返回空字符串 } - -void test() {} Mat readImage() { // 读取输入图像 string imagePath = openFileDialog(); @@ -111,10 +105,22 @@ Mat readImage() { return image; } +// 辅助函数,用于调整图像大小并显示,支持等比例放大 +void showImage(const string& windowName, const Mat& img, double scaleFactor = 1.0) { + Mat resizedImg; + int newWidth = static_cast(img.cols * scaleFactor); + int newHeight = static_cast(img.rows * scaleFactor); + + // 调整图像大小 + resize(img, resizedImg, Size(newWidth, newHeight)); + + // 显示图像 + imshow(windowName, resizedImg); +} int main() { // 读取输入图像 - Mat inputImage = readImage(); + Mat inputImage = readImage(); if (inputImage.empty()) { cout << "Error: Could not load image." << endl; @@ -126,16 +132,19 @@ int main() { // 使用 map 模拟 JSON 参数传递 map params; - params["saturationThreshold"] = 100; // 设置饱和度阈值为100 + params["green"] = 134; // 设置绿色阈值 - // 调用鲜艳颜色检测函数 - vibrantColorDetection(inputImage, outputImage, params); + // 调用鲜艳绿色检测函数 + vibrantGreenDetection(inputImage, outputImage, params); - // 显示原图和检测到的鲜艳区域 - imshow("Original Image", inputImage); - imshow("Detected Vibrant Colors", outputImage); + // 定义缩放因子,1.0 表示原始大小,>1.0 表示放大,<1.0 表示缩小 + double scaleFactor = 1.5; // 将图像放大1.5倍 + + // 显示原图和检测到的绿色区域,使用缩放因子 + showImage("Original Image", inputImage, scaleFactor); + showImage("Detected Vibrant Green", outputImage, scaleFactor); // 等待用户按键 waitKey(0); return 0; -} \ No newline at end of file +}