remove distorted vertical and horizontal line segments

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

remove distorted vertical and horizontal line segments

Post 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
Thanks
Vinod Bapatla
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: remove distorted vertical and horizontal line segments

Post by dlemstra »

Can you post an example image?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

Re: remove distorted vertical and horizontal line segments

Post 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.
Thanks
Vinod Bapatla
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

Re: remove distorted vertical and horizontal line segments

Post 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.
Thanks
Vinod Bapatla
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: remove distorted vertical and horizontal line segments

Post 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.
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

Re: remove distorted vertical and horizontal line segments

Post 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.
Thanks
Vinod Bapatla
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: remove distorted vertical and horizontal line segments

Post by snibgo »

You can upload to somewhere like dropbox.com and paste URLs here.
snibgo's IM pages: im.snibgo.com
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

Re: remove distorted vertical and horizontal line segments

Post 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
Thanks
Vinod Bapatla
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: remove distorted vertical and horizontal line segments

Post 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
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

Re: remove distorted vertical and horizontal line segments

Post by abcdef »

Thank you. Is there a way to represent this in C# using Magick.NET?
Thanks
Vinod Bapatla
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: remove distorted vertical and horizontal line segments

Post by fmw42 »

Sorry I a do not know any APIs. Just the command line. But the MagicNet expert can probably give you the equivalents.
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

Re: remove distorted vertical and horizontal line segments

Post by abcdef »

Is there any one from Magick.NET whoc an provide the fix in line with script option provided?
Thanks
Vinod Bapatla
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: remove distorted vertical and horizontal line segments

Post 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");
				}
			}
		}
	}
}
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
abcdef
Posts: 9
Joined: 2014-08-16T23:28:43-07:00
Authentication code: 6789

Re: remove distorted vertical and horizontal line segments

Post 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.
Thanks
Vinod Bapatla
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: remove distorted vertical and horizontal line segments

Post by fmw42 »

We have no idea how your images are going to vary. The code I created was specific to vertical and horizontal lines.
Post Reply