所以,我正在使用OpenCV来捕获文档,扫描它并裁剪它。当房间没有照明时,它可以完美地工作。当房间里有一些光线,并且桌子上有眩光,文档靠近它时,它也会将眩光作为矩形的一部分捕获。
如何从照片中去除眩光?
这是我用来获取所需图像的代码:
Mat &image = *(Mat *) matAddrRgba;
Rect bounding_rect;
Mat thr(image.rows, image.cols, CV_8UC1);
cvtColor(image, thr, CV_BGR2GRAY); //Convert to gray
threshold(thr, thr, 150, 255, THRESH_BINARY + THRESH_OTSU); //Threshold the gray
vector<vector<Point> > contours; // Vector for storing contour
vector<Vec4i> hierarchy;
findContours(thr, contours, hierarchy, CV_RETR_CCOMP,
CV_CHAIN_APPROX_SIMPLE); // Find the contours in the image
sort(contours.begin(), contours.end(),
compareContourAreas); //Store the index of largest contour
bounding_rect = boundingRect(contours[0]);
rectangle(image, bounding_rect, Scalar(250, 250, 250), 5);
这里是我所说的反光照片:
我发现可以使用inRange函数,查找适当的颜色标量并使用inpaint函数消除光线。这是相关代码片段,但它总是崩溃,显示需要具有通道的8位图像。
Mat &image = *(Mat *) matAddrRgba;
Mat hsv, newImage, inpaintMask;
cv::Mat lower_red_hue_range;
inpaintMask = Mat::zeros(image.size(), CV_8U);
cvtColor(image, hsv, COLOR_BGR2HSV);
cv::inRange(hsv, cv::Scalar(0, 0, 215, 0), cv::Scalar(180, 255, 255, 0),
lower_red_hue_range);
image = lower_red_hue_range;
inpaint(image, lower_red_hue_range, newImage, 3, INPAINT_TELEA);