|
@@ -28,20 +28,262 @@ int main()
|
|
|
if (!AfxWinInit(hModule, nullptr, ::GetCommandLine(), 0))
|
|
|
{
|
|
|
// TODO: 在此处为应用程序的行为编写代码。
|
|
|
- wprintf(L"错误: MFC 初始化失败\n");
|
|
|
+ _tprintf(_T("错误: MFC 初始化失败\n"));
|
|
|
nRetCode = 1;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// TODO: 在此处为应用程序的行为编写代码。
|
|
|
+ printf("开始程序……");
|
|
|
+
|
|
|
+ cv::Mat thresholdImg;
|
|
|
+ if (GetBinaryImage("E:\\bin\\test.jpg", 100, 255, thresholdImg))
|
|
|
+ {
|
|
|
+ cv::imwrite("E:\\bin\\thresholdImg.jpg", thresholdImg);
|
|
|
+ thresholdImg.release();
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
// TODO: 更改错误代码以符合需要
|
|
|
- wprintf(L"错误: GetModuleHandle 失败\n");
|
|
|
+ _tprintf(_T("错误: GetModuleHandle 失败\n"));
|
|
|
nRetCode = 1;
|
|
|
}
|
|
|
|
|
|
+ //system("pause");
|
|
|
+
|
|
|
return nRetCode;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+CV_API bool ReadImage(std::string strImgPath, cv::Mat& img)
|
|
|
+{
|
|
|
+ // 参数有效性判断;
|
|
|
+ if (!PathFileExists(strImgPath.c_str())) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ OutputDebugString("参数无效!");
|
|
|
+#endif
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 以BGR三通道方式读取图片;
|
|
|
+ img = cv::imread(strImgPath.c_str(), cv::IMREAD_COLOR);
|
|
|
+ // 判断图片是否为空;
|
|
|
+ if (!img.empty()) {
|
|
|
+#ifdef _DEBUG
|
|
|
+ OutputDebugString("读取图片失败!");
|
|
|
+#endif
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+CV_API bool GetBinaryImage(std::string strImgPath, long nThresholdVal, long nMaxThresholdVal, cv::Mat& thresholdImg)
|
|
|
+{
|
|
|
+ cv::Mat img;
|
|
|
+ // 读取图片;
|
|
|
+ if (!ReadImage(strImgPath, img))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ cv::Mat grayImg;
|
|
|
+ // 转成灰阶图;
|
|
|
+ cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
|
|
|
+ // 释放对象;
|
|
|
+ img.release();
|
|
|
+
|
|
|
+ cv::Mat gaussImg;
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::GaussianBlur(grayImg, gaussImg, cv::Size(5, 5), 0, 0);
|
|
|
+ // 释放对象;
|
|
|
+ grayImg.release();
|
|
|
+
|
|
|
+ // 全局化指定的阀值与返回值相等;
|
|
|
+ cv::threshold(gaussImg, thresholdImg, nThresholdVal, nMaxThresholdVal, cv::THRESH_BINARY);
|
|
|
+ // 释放对象;
|
|
|
+ gaussImg.release();
|
|
|
+
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!thresholdImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool GetOTSUBinaryImage(std::string strImgPath, long nThresholdVal, long nMaxThresholdVal, cv::Mat& thresholdImg)
|
|
|
+{
|
|
|
+ cv::Mat img;
|
|
|
+ // 读取图片;
|
|
|
+ if (!ReadImage(strImgPath, img))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ cv::Mat grayImg;
|
|
|
+ // 转成灰阶图;
|
|
|
+ cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
|
|
|
+ // 释放对象;
|
|
|
+ img.release();
|
|
|
+
|
|
|
+ cv::Mat gaussImg;
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::GaussianBlur(grayImg, gaussImg, cv::Size(5, 5), 0, 0);
|
|
|
+ // 释放对象;
|
|
|
+ grayImg.release();
|
|
|
+
|
|
|
+ // OTSU,此时参数3可随意设置;
|
|
|
+ cv::threshold(gaussImg, thresholdImg, nThresholdVal, nMaxThresholdVal, cv::THRESH_BINARY | cv::THRESH_OTSU);
|
|
|
+ // 释放对象;
|
|
|
+ gaussImg.release();
|
|
|
+
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!thresholdImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+CV_API bool GetAdaptiveBinaryImage(std::string strImgPath, long nMaxThresholdVal, cv::Mat& thresholdImg)
|
|
|
+{
|
|
|
+ cv::Mat img;
|
|
|
+ // 读取图片;
|
|
|
+ if (!ReadImage(strImgPath, img))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ cv::Mat grayImg;
|
|
|
+ // 转成灰阶图;
|
|
|
+ cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
|
|
|
+ // 释放对象;
|
|
|
+ img.release();
|
|
|
+
|
|
|
+ cv::Mat gaussImg;
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::GaussianBlur(grayImg, gaussImg, cv::Size(5, 5), 0, 0);
|
|
|
+ // 释放对象;
|
|
|
+ grayImg.release();
|
|
|
+
|
|
|
+ cv::Mat blurImg;
|
|
|
+ // 自适应阀值;
|
|
|
+ cv::medianBlur(gaussImg, blurImg, 3);
|
|
|
+ // 释放对象;
|
|
|
+ gaussImg.release();
|
|
|
+
|
|
|
+ // 局部二值化;//blocksize一般取3、5、7
|
|
|
+ cv::adaptiveThreshold(blurImg, thresholdImg, nMaxThresholdVal, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 3, 4.5);
|
|
|
+ // 释放对象;
|
|
|
+ blurImg.release();
|
|
|
+
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!thresholdImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool GaussianBlur(cv::Mat& img, cv::Size cvSize, cv::Mat& gaussImg)
|
|
|
+{
|
|
|
+ if (!img.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::GaussianBlur(img, gaussImg, cvSize, 0, 0);
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!gaussImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool GaussianBlur(std::string strImgPath, cv::Size cvSize, cv::Mat& gaussImg)
|
|
|
+{
|
|
|
+ cv::Mat img;
|
|
|
+ // 读取图片;
|
|
|
+ if (!ReadImage(strImgPath, img))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::GaussianBlur(img, gaussImg, cvSize, 0, 0);
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!gaussImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool Color2Gray(cv::Mat& img, cv::Mat& grayImg)
|
|
|
+{
|
|
|
+ if (!img.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!grayImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool Color2Gray(std::string strImgPath, cv::Mat& grayImg)
|
|
|
+{
|
|
|
+ cv::Mat img;
|
|
|
+ // 读取图片;
|
|
|
+ if (!ReadImage(strImgPath, img))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!grayImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool Gray2Color(cv::Mat& img, cv::Mat& colorImg)
|
|
|
+{
|
|
|
+ if (!img.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::cvtColor(img, colorImg, cv::COLOR_GRAY2BGR);
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!colorImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool ErodeImg(cv::Mat& img, cv::MorphShapes kernelShape, cv::Size kernelSize, cv::Mat& erodeImg)
|
|
|
+{
|
|
|
+ if (!img.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // 获取自定义核(核形状:矩形MORPH_RECT、十字架CROSS、椭圆ELLIPSE )
|
|
|
+ cv::Mat element = cv::getStructuringElement(kernelShape, kernelSize);
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::erode(img, erodeImg, element);
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!erodeImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+CV_API bool DilateImg(cv::Mat& img, cv::MorphShapes kernelShape, cv::Size kernelSize, cv::Mat& dilateImg)
|
|
|
+{
|
|
|
+ if (!img.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // 获取自定义核(核形状:矩形MORPH_RECT、十字架CROSS、椭圆ELLIPSE )
|
|
|
+ cv::Mat element = cv::getStructuringElement(kernelShape, kernelSize);
|
|
|
+ // 再高斯模糊处理(滤波);
|
|
|
+ cv::dilate(img, dilateImg, element);
|
|
|
+ // 判断图片是否空;
|
|
|
+ if (!dilateImg.empty())
|
|
|
+ return false;
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|