Page 1 of 2

remove distorted vertical and horizontal line segments

Posted: 2014-08-17T06:05:39-07:00
by abcdef
I am working on an image with tabular data to extract text using an OCR tool. In the process, I am having problem with poorly scanned documents with broken lines noise created by partially removed lines of the table.
I am using .NET on Windows.

The original is in 8bpp grayscale. OCR tool itself is removing lines till now, but it is not good enough for poorly scanned documents as the lines are not fully removed. So, I had to resort to a custom line removal.
Please help. I tried to read through Convolve and Morphology Close option of image magick forum. I also tried basic matrix with Convolve and Close methods. Please help

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-17T09:14:22-07:00
by dlemstra
Can you post an example image?

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-17T23:04:43-07:00
by abcdef
I may not be able to share the images. Sorry for it. But it is an image of scanned tabular data or photo of document. It has broken lines, line segments which are not straight, blurred or shaded area close to the line, line touching the data. All these cause problem in OCRing. Used Bradley Local Thresholding that removed shaddow or lighter grey shades on one side of the lines. But still there are broken line segments.

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-20T06:15:01-07:00
by abcdef
Hi, Is Magick.NET having Anisotropic algorithm implementation? I saw it in GIMP. But GIMP uses scripting. I could not find it as .net API. Atleast this algorithm will remove lot of noise as I observed in GIMP.

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-20T09:23:24-07:00
by fmw42
Imagemagick does not have anisotropic diffusion to my knowledge.

In order to understand your issue we need an example. It does not have to be one of your private images, but something similar or some generic image that shows the same problem.

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-20T22:51:35-07:00
by abcdef
Ok. Where can I post the image samples? This forum does not allow it. Also, is it fine if I provide a portion of the image/s? Also, the key issue of image processing in my case is that it should be a generic solution and not specific to one instance. Thanks for responding.

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-20T23:32:00-07:00
by snibgo
You can upload to somewhere like dropbox.com and paste URLs here.

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-23T07:14:11-07:00
by abcdef
Hi below is the link to sample image. If we closely look at lines, they are distorted or a shadow line is created. I want to remove those lines to allow OCRing to work properly. But could not find line removal alg. Is it possible to remove these lines through imagemagick? Or is it possible to clear the noise around the lines and make them thinner?
https://www.dropbox.com/s/soisnw5di93c2 ... 1.tif?dl=0

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-23T08:49:06-07:00
by fmw42
Try these. They reduce the image to one row, threshold to get the darkest regions, expand, then composite with original to remove vertical lines. Then it repeats for the other dimension.

Unix syntax:

Code: Select all

convert image001.tif +repage \
\( -clone 0 -scale x1! -scale 321x522! -auto-level -threshold 55% -negate \) \
\( -clone 1 \) \
-compose over -composite \
\( -clone 0 -scale 1x! -scale 321x522! -auto-level -threshold 55% -negate \) \
\( -clone 1 \) \
-compose over -composite result1.tiff

Code: Select all

convert image001.tif +repage \
\( -clone 0 -scale x1! -scale 321x522! -auto-level -threshold 50% -morphology erode diamond:2 -negate \) \
\( -clone 1 \) \
-compose over -composite \
\( -clone 0 -scale 1x! -scale 321x522! -auto-level -threshold 50% -morphology erode diamond:2 -negate \) \
\( -clone 1 \) \
-compose over -composite result2:
For windows syntax, see http://www.imagemagick.org/Usage/windows/ regarding parenthesis and new line characters

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-23T11:44:26-07:00
by abcdef
Thank you. Is there a way to represent this in C# using Magick.NET?

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-23T12:30:00-07:00
by fmw42
Sorry I a do not know any APIs. Just the command line. But the MagicNet expert can probably give you the equivalents.

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-24T22:46:09-07:00
by abcdef
Is there any one from Magick.NET whoc an provide the fix in line with script option provided?

Re: remove distorted vertical and horizontal line segments

Posted: 2014-08-28T12:54:36-07:00
by dlemstra
I completely forgot about this topic.....

The translation of the command is a lot different from the command line because the -composite uses one of the images as a clipmask. I have translated the second example from Fred to C#.

Code: Select all

private static void RemoveLines(MagickImage original, MagickImage image, string geometryValue)
{
	// -scale x1! and -scale 1x!
	image.Scale(new MagickGeometry(geometryValue));

	// -scale 321x522! (resize to original width/height)
	MagickGeometry geometry = new MagickGeometry(original.Width, original.Height);
	geometry.IgnoreAspectRatio = true;
	image.Scale(geometry);

	image.AutoLevel(); // -auto-level
	image.Threshold(50); // -threshold 50%
	image.Morphology(MorphologyMethod.Erode, Kernel.Diamond, 2); // -morphology erode diamond:2

	// ( -clone 1 )
	using (MagickImage clone = image.Clone())
	{
		image.Negate(); // -negate (this needs to be done after the clone in Magick.NET)

		// -compose over -composite (one of the images is used as a clipmask because there are 3 images)
		image.ClipMask = clone.Clone();
		image.Composite(original, Gravity.Center);
		image.ClipMask = null;
	}
}

public static void RemoveLines()
{
	using (MagickImage image = new MagickImage("image001.tif")) // image001.tif
	{
		image.RePage(); // +repage

		using (MagickImage original = image.Clone())
		{
			using (MagickImage firstClone= original.Clone()) // -clone 0
			{
				RemoveLines(original, firstClone, "x1!"); // -scale x1!
				using (MagickImage secondClone = firstClone.Clone()) // -clone 0
				{
					RemoveLines(secondClone , firstClone, "1x!"); // -scale x1!
					firstClone.Write("result2.tiff");
				}
			}
		}
	}
}

Re: remove distorted vertical and horizontal line segments

Posted: 2014-09-04T01:00:21-07:00
by abcdef
Thank you.
But the implementation looks specific to the image that I shared. I tried the method on another image with little impact of LineRemoval method.

Re: remove distorted vertical and horizontal line segments

Posted: 2014-09-04T09:29:37-07:00
by fmw42
We have no idea how your images are going to vary. The code I created was specific to vertical and horizontal lines.