Using Morhpology to remove gaps that are small

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Using Morhpology to remove gaps that are small

Post by TedBaker » 2018-08-07T10:02:37-07:00

My code generates pretty accurate images that represent the dimensions of series of images.

However occasionally it makes a mistake...

for example the following image: where the 4th black line needs to be removed.

Image

Note: this image has been scaled vertically to make my question is easier to understand and view, the image can be scaled vertically down to a height of 1 pixel before actual processing, with a convert input.jpg -scale x1!

I would like to remove any black line that is not surrounded by 2451 or more white pixels on either side?

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Morhpology to remove gaps that are small

Post by fmw42 » 2018-08-07T10:14:47-07:00

Convert your 1 row image into txt: format and search for the locations of the black pixels. Check the distances between successive ones and then write white over those black ones that are not at the required distance. This will require scripting to process the txt: string output. You may want to thin the lines to one pixel wide first using -morphology.

It looks like you are looking for fairly regular separated intervals and want to remove those that are not. So you could find the lines that have a more or less regular interval to within your tolerances and mark those to save and then remove all the other. Then reconstruct the 1 row image from the locations of the black line you want to keep.

snibgo
Posts: 11910
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using Morhpology to remove gaps that are small

Post by snibgo » 2018-08-07T12:04:01-07:00

As Fred says. Connected components may also be used: from a Nx1 image of black and white only, a script can read the verbose output from connected components, looking for white areas less then the specified width. This needs a script.

Another method is to use morphology to change white lines that are wider than the specified width to, say, red. Any remaining white will be narrow. Then change any sequence of white-then-black to white-white. Then change red back to white.
snibgo's IM pages: im.snibgo.com

TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: Using Morhpology to remove gaps that are small

Post by TedBaker » 2018-08-07T14:35:18-07:00

snibgo wrote:
2018-08-07T12:04:01-07:00
Another method is to use morphology to change white lines that are wider than the specified width to, say, red. Any remaining white will be narrow. Then change any sequence of white-then-black to white-white. Then change red back to white.
Thanks

How do I this?, I am little familiar with using open and close to erode small areas, but not using morphology to preserve large areas.

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Morhpology to remove gaps that are small

Post by fmw42 » 2018-08-07T15:30:43-07:00

I am not suggesting you use it to preserve large areas. I was only suggesting that you use it to thin your lines to 1 pixel wide. Then use txt: format to study the spacing between the black regions. For those regions that do not fit, write over your input black pixels with white pixels.

TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: Using Morhpology to remove gaps that are small

Post by TedBaker » 2018-08-07T16:39:58-07:00

fmw42 wrote:
2018-08-07T15:30:43-07:00
I am not suggesting you use it to preserve large areas. I was only suggesting that you use it to thin your lines to 1 pixel wide. Then use txt: format to study the spacing between the black regions. For those regions that do not fit, write over your input black pixels with white pixels.
Thanks fred, I was going to code a solution using the approach you describe, as I think It will be the most robust. Mainly because I can understand it well enough to put all the "additional" checks that might come up. I was just interested in understanding a little bit about more about the solution that snibgo was referring to.

User avatar
fmw42
Posts: 25259
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using Morhpology to remove gaps that are small

Post by fmw42 » 2018-08-07T18:39:35-07:00

Sorry, I guess I missed that discussion. -connected components is also a solution for finding the black pixels from the white ones. That was also a good suggestion. The bounding box of the white areas will give you a measure of the spacing and if appropriate you can compare that to some minimum width of white that you would require.

I am not sure what he was trying to suggest regarding the change of long white regions to red. I assume you would use a morphology kernel of some length of white to change all regions of white of that length to red. Shorter regions would not get changed. Longer regions, would change to red, since there are lengths of the kernel that would fit into the longer regions for many pixels. But you will need to hear back from snibgo about that. He is likely more knowledgeable about -morphology than I.

snibgo
Posts: 11910
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using Morhpology to remove gaps that are small

Post by snibgo » 2018-08-07T21:23:31-07:00

I'm not sure of the conditions for removal. A black line is to be removed if and only if it has short white lines on both sides. Is that right?

In a symbolic language like C or bash we could keep track of long or short lines by arrays of coordinates. When doing the job with images, we do it with colours.

Windows BAT script, for IM v6. All the "+write *.png" lines are for debugging only, and can be removed. For v7, remove "-negate" and change "mask" to "write-mask", but it still goes wrong for m1.png and m2.png. I don't know why.

Code: Select all

set LEN=2451

%IM%convert ^
  exlines.jpg ^
  -scale "x1^!" ^
  -threshold 50%% ^
  +write mpr:INPT ^
+write x0.png ^
  ( +clone -negate +write mpr:ORG +delete ) ^
  -channel R ^
  -morphology HMT rectangle:%LEN%x1+0+0 ^
  +channel ^
+write x1.png ^
  -mask mpr:ORG -morphology Dilate rectangle:%LEN%x1+0+0 ^
  +mask ^
  -fill Black -opaque White ^
  -fill White -opaque Cyan ^
+write x.png ^
  -channel RGB ^
  ( -clone 0 ^
    -morphology Thicken:-1 2x1+0+0:0,1 ^
+write m1.png ^
  ) ^
  ( -clone 0 ^
    -morphology Thicken:-1 2x1+1+0:1,0 ^
+write m2.png ^
  ) ^
  +channel ^
  -delete 0 ^
  -compose Darken -composite ^
+write m.png ^
  mpr:INPT ^
  -compose Lighten -composite ^
  out.png
snibgo's IM pages: im.snibgo.com

TedBaker
Posts: 50
Joined: 2017-10-10T14:14:55-07:00
Authentication code: 1151

Re: Using Morhpology to remove gaps that are small

Post by TedBaker » 2018-08-08T06:33:41-07:00

fmw42 wrote:
2018-08-07T18:39:35-07:00
Sorry, I guess I missed that discussion. -connected components is also a solution for finding the black pixels from the white ones. That was also a good suggestion.
Indeed, I already use -connected components, from a script that I rewrote, using some ideas, that you previously posted. The solution works well as it performs a number of complex steps up to this point, but occasionally it makes a mistake at the end. I wrote a solution that walks the through the results of the -connected components data, and removes the mistakes. But I thought perhaps there was some more magic I could do with morphology, but I could not get it to work, as it was step to far, from what I have learnt.
snibgo wrote:
2018-08-07T21:23:31-07:00
I'm not sure of the conditions for removal. A black line is to be removed if and only if it has short white lines on both sides. Is that right?
Yes, I am working a open source library of tools to help with film scanning. Thanks I will study that! I have not used HMT, I tried using open and close, and different permutations of -negate at varies stages, but I just got lost.
I guess it takes a little while to get your head around thinking a little differently,

snibgo
Posts: 11910
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Using Morhpology to remove gaps that are small

Post by snibgo » 2018-08-08T07:21:15-07:00

I wrote the script late at night. It can probably be simplified and optimized.

I should have stated an assumption: that you care about black and white colours only, and any grayscale comes from JPEG compression, which can be removed by "-threshold 50%".

So, doing the job entirely within a single IM command is possible, but messy. A neater solution would be code (in C or bash or whatever) that reads the image just once to find the black/white boundaries, and hence the line segments, then processes the segments to determine which to turn white.

Ah, film scanning, yes that makes sense. The black lines are inter-frame gaps, but some are spurious.
snibgo's IM pages: im.snibgo.com

Post Reply