Ask Your Question

How to change the background of scanned document in opencv c++

asked 2015-06-09 04:28:20 -0500

ashok gravatar image

updated 2015-06-22 04:15:29 -0500

C:\fakepath\Screen Shot 2015-06-19 at 7.57.23 pm.png

I can removed the background from outside of my image (O).how to remove the background gray color inside circle of image.

Here is my code.

//src have my actual mat object image.

cv::Mat source(src.rows, src.cols,CV_8UC1);
cv::addWeighted(src, 2.5, src, -1, 2.0, source);
cv::GaussianBlur(source, imgH, cv::Size(5, 5), cv::BORDER_WRAP);

imgH = imgH + cvScalar(-50, -50, -50);

//Now replace the black background with white color
cv::floodFill(imgH, cv::Point(10,10), cv::Scalar(255,255,255), (cv::Rect*)0, cv::Scalar(), cv::Scalar(255,255,255));
cv::floodFill(imgH, cv::Point(imgH.cols-10 ,10), cv::Scalar(255,255,255), (cv::Rect*)0, cv::Scalar(), cv::Scalar(255,255,255));

cv::floodFill(imgH, cv::Point(10,imgH.rows-10), cv::Scalar(255,255,255), (cv::Rect*)0, cv::Scalar(), cv::Scalar(255,255,255));
cv::floodFill(imgH, cv::Point(imgH.cols-10,imgH.rows-10), cv::Scalar(255,255,255), (cv::Rect*)0, cv::Scalar(), cv::Scalar(255,255,255));
edit retag flag offensive close merge delete



example image? Own ideas?

FooBar gravatar imageFooBar ( 2015-06-19 04:11:11 -0500 )edit

If that's your actual image, a simple thresholding will work.

LorenaGdL gravatar imageLorenaGdL ( 2015-06-22 04:29:49 -0500 )edit

Threshold is not working. I am tried with this code cv::threshold(imgH, imgH, 245, 255, CV_THRESH_TRUNC); And also tried with CV_THRESH_BINARY. both are not working. Actually this not my full image, I cropped the one letter from my output text document.

ashok gravatar imageashok ( 2015-06-22 05:13:02 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-06-22 06:18:56 -0500

Flart gravatar image

updated 2015-06-23 04:47:25 -0500

Gray is not so bright, as you suggested at comment. Try CV_THRESH_OTSU or cv::threshold(imgH, imgH, 150, 255, CV_THRESH_TRUNC), it's enough for that kind of images.

edit flag offensive delete link more


thak you for the code.

ashok gravatar imageashok ( 2015-06-23 06:10:40 -0500 )edit
Login/Signup to Answer

Question Tools



Asked: 2015-06-09 04:28:20 -0500

Seen: 440 times

Last updated: Jun 23 '15