Cleaning a scanned image in opencv

2019-06-14 11:19发布

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);

2条回答
一纸荒年 Trace。
2楼-- · 2019-06-14 11:49

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].

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-06-14 11:49

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

查看更多
登录 后发表回答