Cleaning a scanned image in opencv

2019-06-14 11:08发布

问题:

I'm trying to denoise the image then extract the skeleton of an image containing a handwritten line. I want the line to be continuous and solid but the method I use fails to do that and relatively slow. Here's the original image: Original Image

Morphed On the morphed image, you can see a small island at the lower right.

The thinned image from above shows the line is broken near the end.

Any other method to achieve desired result?

My code looks like this:

int morph_elem = 2;
int morph_size = 10;
int morph_operator = 0;

Mat origImage = imread(origImgPath, CV_LOAD_IMAGE_COLOR);
medianBlur(origImage, origImage, 3);
cvtColor(origImage, origImage, COLOR_RGB2GRAY);
threshold(origImage, origImage, 0, 255, THRESH_OTSU);

Mat element = getStructuringElement(morph_elem, Size(2 * morph_size + 1, 2 * morph_size + 1), cv::Point(morph_size, morph_size));

morphologyEx(origImage, origImage, MORPH_OPEN, element);
thin(origImage, true, true, true);

回答1:

To reduce the line break away try using adaptiveThreshold, play around with the methods and sizes and see what works best. To remove the little island simply do findContours and then mask out unwanted specs using drawContours with color=(255,255,255) and thickness=-1 after you've filtered it with something like wantedContours = [x for x in contours if contourArea(x) < 50].



回答2:

You want to perform a series of closing and opening operations on the image. It is best you understand how they work to make the appropriate choice. See this post: Remove spurious small islands of noise in an image - Python OpenCV