借助opencv库,实现将图片中的阴影去掉,记录下实现代码,如下
+(UIImage*)removeShadow:(UIImage*)processedImage{
cv::Mat src = [MMOpenCVHelper cvMatFromAdjustedUIImage:processedImage];
//1.将图像转为灰度图
cv::Mat gray;
cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);
//定义腐蚀和膨胀的结构化元素和迭代次数
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3));
int iteration = 9;
//2.将灰度图进行膨胀操作
cv::Mat dilateMat;
cv::morphologyEx(gray, dilateMat, cv::MORPH_DILATE, element, cv::Point(-1, -1), iteration);
//3.将膨胀后的图再进行腐蚀
cv::Mat erodeMat;
cv::morphologyEx(dilateMat, erodeMat, cv::MORPH_ERODE, element, cv::Point(-1, -1), iteration);
//4.膨胀再腐蚀后的图减去原灰度图再进行取反操作
cv::Mat calcMat = ~(erodeMat - gray);
//5.使用规一化将原来背景白色的改了和原来灰度图差不多的灰色
cv::Mat removeShadowMat;
cv::normalize(calcMat, removeShadowMat, 0, 200, cv::NORM_MINMAX);
//转换成8bit图像显示
convertScaleAbs(removeShadowMat, removeShadowMat);
UIImage *resultImage=[MMOpenCVHelper UIImageFromCVMat:removeShadowMat];
return resultImage;
}