因此,我使用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);
Run Code Online (Sandbox Code Playgroud)
这是我正在谈论的眩光照片:
我发现的东西是使用inRange,找到适当的颜色标量,我们可以去除光线.这是一个代码片段,但它始终崩溃说它需要8位图像与chanels.
Mat &image = *(Mat *) matAddrRgba;
Mat …Run Code Online (Sandbox Code Playgroud) 我的图像有一个"光反射",图像上的两个第一个零点与图像的其余部分有一些不同.当我将其转换为二进制图像时,这部分变为白色,我需要获得数字的确切轮廓并且这会阻碍.我可以通过使用OpenCV来解决这个问题吗?
原始图片https://docs.google.com/file/d/0BzUNc6BOkYrNNlE3U04wWEVvVE0/edit?usp=sharing
二进制版本https://docs.google.com/file/d/0BzUNc6BOkYrNeEE0U3NvOElqa1E/edit?usp=sharing
如果我增加阈值,我会丢失图像右侧的数字.我的代码:
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main ( int argc, char **argv )
{
Mat im_gray = imread("img2.jpg",CV_LOAD_IMAGE_GRAYSCALE);
Mat im_rgb = imread("img2.jpg");
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);
Mat img_bw = im_gray > 90;
imwrite("image_bw2.jpg", img_bw);
return 0;
}
Run Code Online (Sandbox Code Playgroud)