C++ OpenCV實現圖像去陰影
C++ OpenCV實現圖像去陰影
資料來源: https://mp.weixin.qq.com/s/ejBBC_GFM27R-0-kQuHK0Q
GITHUB: https://github.com/Vaccae/OpenCVDemoCpp
# 實現思路
1 圖將轉為灰度圖
2 將灰度圖進行膨脹操作
3 膨脹後的圖再進行腐蝕操作
4 先膨脹後腐蝕後的圖減去原灰度圖再取反
5 將取反後的圖使用歸一化將白色背景修改貼近原圖
CODE:
#include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat src = imread("E:/DCIM/Test_image.jpg"); CvUtils::MatResize(src, 800, 520); //imshow("src", src); //1.将图像转为灰度图 Mat gray; cvtColor(src, gray, COLOR_BGR2GRAY); CvUtils::SetShowWindow(gray, "gray", 0, 30); imshow("gray", gray); //定义腐蚀和膨胀的结构化元素和迭代次数 Mat element = getStructuringElement(MORPH_RECT, Size(3, 3)); int iteration = 9; //2.将灰度图进行膨胀操作 Mat dilateMat; morphologyEx(gray, dilateMat, MORPH_DILATE, element, Point(-1, -1), iteration); //imshow("dilate", dilateMat); //3.将膨胀后的图再进行腐蚀 Mat erodeMat; morphologyEx(dilateMat, erodeMat, MORPH_ERODE, element, cv::Point(-1, -1), iteration); //imshow("erode", erodeMat); //4.膨胀再腐蚀后的图减去原灰度图再进行取反操作 Mat calcMat = ~(erodeMat - gray); CvUtils::SetShowWindow(calcMat, "calc", gray.cols*2, 30); imshow("calc", calcMat); //5.使用规一化将原来背景白色的改了和原来灰度图差不多的灰色 Mat removeShadowMat; normalize(calcMat, removeShadowMat, 0, 200, NORM_MINMAX); CvUtils::SetShowWindow(removeShadowMat, "dst", gray.cols, 30); imshow("dst", removeShadowMat); waitKey(0); return 0; }
成果圖:
One thought on “C++ OpenCV實現圖像去陰影”
https://github.com/Vaccae/OpenCVDemoCpp
2021-04-19 OpenCVDenoising項目為去乾燥函數fastNlMeansDenoising的使用
2021-04-12 OpenCVRemoveShadows項目為OpenCV圖像去陰影
2021-04-06 OpenCVResizeTest項目為OpenCV結果操作將圖片縮小後合併另一個圖像
2021-03-29 OpenCVImageToCartoon項目為OpenCV實現圖像卡通化演示
2021-03-10 opencvsrctosumiao項目為OpenCV實現圖像轉換為素描效果