diff --git a/README.md b/README.md new file mode 100644 index 0000000..c2f0b03 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# Cotton Color + +## 颜色检测 + +### 艳丽色彩检测 + +纯色彩检测就可以。饱和度检测。 + +思路:rgb -> HSV -> s -> threshold -> 杂质 + +### 黑色检测/滴灌带检测 + +L a* b* 色彩空间检测,纯黑色就是杂质,但是容易有噪声。 + +思路:rgb -> La*b* -> threshold -> 黑色 -> 模板匹配 -> 物体的大小 -> 阈值判断 -> 杂质 + +### 暗黄色检测/油棉 + +L a* b* 色彩空间检测,检测暗黄色。 + +思路:rgb -> La*b* -> threshold -> 暗黄色 -> 模板匹配 -> 物体的大小 -> 阈值判断 -> 杂质 + +### 带土地膜检测 + +L a* b* 色彩空间检测,检测明黄色、白色。 + +思路:rgb -> La*b* -> threshold -> 白色、明黄色 -> 模板匹配 -> 物体的大小 -> 阈值判断 -> 杂质 + + +## 深度学习检测 + +需求:模板匹配缺少对于纹理的判断,所以要加上深度学习对于各个杂质进行确认。 + +### 方案1:端到端式的方案 + +传统思路:rgb -> La*b* -> threshold -> 白色、明黄色 -> 模板匹配 -> 物体的大小 -> 阈值判断 -> 杂质 + +深度学习思路:rgb -> 可疑色彩图像增强 -> threshold -> 白色、明黄色 -> 模板匹配 -> 物体的大小 -> 阈值判断 -> 杂质 + | + -> YOLO -> 杂质 + + +### 方案2:验证形式的方案 + +传统思路:rgb -> La*b* -> threshold -> 白色、明黄色 -> 模板匹配 -> 物体的大小 -> 阈值判断 -> 杂质 + +融合深度学习思路:rgb -> La*b* -> threshold -> 白色、明黄色 -> 模板匹配 -> 物体的大小 -> 阈值判断 -> 杂质 + | | + 激进方案 -> 深度学习 - > 区块判别 -> 杂质 | + | + 保守方案 -> 深度学习确认 -> 杂质 + + \ No newline at end of file diff --git a/cotton_color.cpp b/cotton_color.cpp new file mode 100644 index 0000000..f3e5785 --- /dev/null +++ b/cotton_color.cpp @@ -0,0 +1,81 @@ +锘#include +#include +#include +#include + + +using namespace cv; +using namespace std; + + +/** + * @brief 椴滆壋鑹插僵妫娴嬪嚱鏁帮紝閫氳繃楗卞拰搴﹂槇鍊兼娴嬭緭鍏ュ浘鍍忎腑椴滆壋鐨勯鑹插尯鍩熴 + * + * 姝ゅ嚱鏁板皢杈撳叆鍥惧儚浠 BGR 鑹插僵绌洪棿杞崲鍒 HSV 鑹插僵绌洪棿锛屽苟鎻愬彇鍑洪ケ鍜屽害 (S) 閫氶亾銆傜劧鍚庯紝閫氳繃璁剧疆楗卞拰搴﹂槇鍊硷紝 + * 鏉ユ娴嬪浘鍍忎腑楗卞拰搴﹀ぇ浜庨槇鍊肩殑鍖哄煙锛屾爣璁颁负杈撳嚭鍥惧儚鐨勯矞鑹抽鑹插尯鍩熴 + * + * @param inputImage 杈撳叆鍥惧儚锛岀被鍨嬩负 cv::Mat锛岃姹備负 BGR 鑹插僵绌洪棿銆 + * @param outputImage 杈撳嚭鍥惧儚锛岀被鍨嬩负 cv::Mat锛岃緭鍑哄浘鍍忓皢鏍囪鍑洪矞鑹抽鑹插尯鍩燂紝鍘熷鍥惧儚灏哄銆 + * @param saturationThreshold 楗卞拰搴﹂槇鍊硷紝绫诲瀷涓 int锛岀敤浜庤繃婊や綆楗卞拰搴︾殑鍖哄煙锛岃寖鍥撮氬父涓 0 鍒 255銆 + * 楗卞拰搴﹂珮浜庢闃堝肩殑鍖哄煙灏嗚璁や负鏄矞鑹崇殑棰滆壊銆 + * + * @note 楗卞拰搴﹂槇鍊艰秺楂橈紝杈撳嚭鍥惧儚灏嗗彧淇濈暀鏇翠负椴滆壋鐨勫尯鍩熴 + * 鑻ラケ鍜屽害闃堝艰繃浣庯紝鍙兘浼氭娴嬪埌杩囧鐨勫尯鍩熴 + */ + /** + * @brief 椴滆壋鑹插僵妫娴嬪嚱鏁帮紝閫氳繃楗卞拰搴﹂槇鍊兼娴嬭緭鍏ュ浘鍍忎腑椴滆壋鐨勯鑹插尯鍩熴 + * + * @param inputImage 杈撳叆鍥惧儚锛岀被鍨嬩负 cv::Mat锛岃姹備负 BGR 鑹插僵绌洪棿銆 + * @param outputImage 杈撳嚭鍥惧儚锛岀被鍨嬩负 cv::Mat锛岃緭鍑哄浘鍍忓皢鏍囪鍑洪矞鑹抽鑹插尯鍩燂紝鍘熷鍥惧儚灏哄銆 + * @param params 鍙傛暟鏄犲皠锛岀敤浜庝紶閫掑悇绉嶅彲閰嶇疆鐨勫弬鏁帮紝濡傞ケ鍜屽害闃堝肩瓑銆 + */ +void vibrantColorDetection(const Mat& inputImage, Mat& outputImage, const map& params) { + // 浠庡弬鏁版槧灏勪腑鑾峰彇楗卞拰搴﹂槇鍊 + int saturationThreshold = params.at("saturationThreshold"); + + // 灏嗚緭鍏ュ浘鍍忎粠 BGR 杞崲涓 HSV + Mat hsvImage; + cvtColor(inputImage, hsvImage, COLOR_BGR2HSV); + + // 鍒嗙 HSV 鍥惧儚鐨勫悇涓氶亾 + Mat channels[3]; + split(hsvImage, channels); + + // 鑾峰彇楗卞拰搴﹂氶亾 (S) + Mat saturation = channels[1]; + + // 鍒涘缓杈撳嚭鍥惧儚锛屽皢楗卞拰搴﹀ぇ浜庨槇鍊肩殑鍖哄煙鏍囪涓烘潅璐 + outputImage = Mat::zeros(inputImage.size(), CV_8UC1); + + // 瀵归ケ鍜屽害鍥惧儚搴旂敤闃堝煎鐞 + threshold(saturation, outputImage, saturationThreshold, 255, THRESH_BINARY); +} + + +int main() { + // 璇诲彇杈撳叆鍥惧儚 + Mat inputImage = imread("C:\\Program Files\\Matrox Imaging\\Images\\test.bmp"); + + if (inputImage.empty()) { + cout << "Error: Could not load image!" << endl; + return -1; + } + + // 鍒涘缓杈撳嚭鍥惧儚 + Mat outputImage; + + // 浣跨敤 map 妯℃嫙 JSON 鍙傛暟浼犻 + map params; + params["saturationThreshold"] = 100; // 璁剧疆楗卞拰搴﹂槇鍊间负100 + + // 璋冪敤椴滆壋棰滆壊妫娴嬪嚱鏁 + vibrantColorDetection(inputImage, outputImage, params); + + // 鏄剧ず鍘熷浘鍜屾娴嬪埌鐨勯矞鑹冲尯鍩 + imshow("Original Image", inputImage); + imshow("Detected Vibrant Colors", outputImage); + + // 绛夊緟鐢ㄦ埛鎸夐敭 + waitKey(0); + return 0; +} diff --git a/cotton_color.sln b/cotton_color.sln new file mode 100644 index 0000000..6a23042 --- /dev/null +++ b/cotton_color.sln @@ -0,0 +1,31 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.11.35327.3 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "cotton_color", "cotton_color.vcxproj", "{6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}" +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 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Debug|x64.ActiveCfg = Debug|x64 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Debug|x64.Build.0 = Debug|x64 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Debug|x86.ActiveCfg = Debug|Win32 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Debug|x86.Build.0 = Debug|Win32 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Release|x64.ActiveCfg = Release|x64 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Release|x64.Build.0 = Release|x64 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Release|x86.ActiveCfg = Release|Win32 + {6205CBCD-F3B8-4365-B6BD-FFA76FF9D993}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {96810E2B-2AC9-4993-AC53-052B8AA18FA4} + EndGlobalSection +EndGlobal diff --git a/cotton_color.vcxproj b/cotton_color.vcxproj new file mode 100644 index 0000000..bf172fc --- /dev/null +++ b/cotton_color.vcxproj @@ -0,0 +1,138 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 17.0 + Win32Proj + {6205cbcd-f3b8-4365-b6bd-ffa76ff9d993} + cottoncolor + 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 + C:\Users\ZLSDKJ\source\opencv\build\include;%(AdditionalIncludeDirectories) + + + Console + true + true + true + C:\Users\ZLSDKJ\source\opencv\build\x64\vc16\lib;%(AdditionalLibraryDirectories) + opencv_world4100.lib;%(AdditionalDependencies) + + + + + + + + + \ No newline at end of file diff --git a/cotton_color.vcxproj.filters b/cotton_color.vcxproj.filters new file mode 100644 index 0000000..7ced59e --- /dev/null +++ b/cotton_color.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