Basic pixel removal in black and white picture
Basic pixel removal in black and white picture
Hello everyone,
it's my first topic on this website.
I play with imagemagick from times to times but now I'm stuck with a very simple task.
I have an image in black and white. (gif converted with -monochrome)
I want to remove isolated black pixels, so : remove black pixels surrounded by 8 white pixels.
I tried a lot of things with -median, -morphology...
If you have any idea, let me know. :p
Thanks !!
it's my first topic on this website.
I play with imagemagick from times to times but now I'm stuck with a very simple task.
I have an image in black and white. (gif converted with -monochrome)
I want to remove isolated black pixels, so : remove black pixels surrounded by 8 white pixels.
I tried a lot of things with -median, -morphology...
If you have any idea, let me know. :p
Thanks !!
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Basic pixel removal in black and white picture
Your image has only black and white pixels. Where a black pixel is surrounded by 8 white pixels, you want to turn it black, leaving all other pixels alone. Correct?
I think this (Windows script) does the trick:
I think this (Windows script) does the trick:
Code: Select all
convert ^
in.png ^
-negate ^
-verbose ^
-morphology Thinning 3:0,0,0,0,1,0,0,0,0 ^
-negate ^
out.png
snibgo's IM pages: im.snibgo.com
Re: Basic pixel removal in black and white picture
hello,
thank you that's awesome !!
...
now that I have you, do you know if it's possible to do the same thing but with several pixels ?
Detect for instance 2 isolated pixels ?
this pattern for example :
000
010
010
000
or
0000
0110
0000
thank you that's awesome !!
...
now that I have you, do you know if it's possible to do the same thing but with several pixels ?
Detect for instance 2 isolated pixels ?
this pattern for example :
000
010
010
000
or
0000
0110
0000
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Basic pixel removal in black and white picture
EDIT to the above: I said, "Where a black pixel is surrounded by 8 white pixels, you want to turn it black," but of course I meant "... you want to turn it white". And you don't need "-verbose", which just tells us how many it found.
For the first example:
To make it easier to read, I have spaced out each row of the kernel, so now it needs quotes ("). If using Unix, these should probably be single quotes (').
You can probably see how to adapt this for your final example. You could do both (or all three) changes in a single command. See http://www.imagemagick.org/Usage/morphology/#thinning , and the rest of that page.
For the first example:
Code: Select all
convert ^
test.png ^
-negate ^
-morphology Thinning ^
"3x4:0,0,0, 0,1,0, 0,1,0, 0,0,0" ^
-negate ^
test4.png
You can probably see how to adapt this for your final example. You could do both (or all three) changes in a single command. See http://www.imagemagick.org/Usage/morphology/#thinning , and the rest of that page.
snibgo's IM pages: im.snibgo.com
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Basic pixel removal in black and white picture
try
convert image -morphology close square:1 result
for two pixels closed just iterate another time or use square:2
or a rectangular shape once for each direction if you just two horizontal or two vertical black pixels surrounded by white.
see
http://www.imagemagick.org/Usage/morphology/#close
It would help to provide a link to an example image that you want to process.
convert image -morphology close square:1 result
for two pixels closed just iterate another time or use square:2
or a rectangular shape once for each direction if you just two horizontal or two vertical black pixels surrounded by white.
see
http://www.imagemagick.org/Usage/morphology/#close
It would help to provide a link to an example image that you want to process.
Last edited by fmw42 on 2013-06-09T10:45:44-07:00, edited 1 time in total.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Basic pixel removal in black and white picture
I'll also add that I've been a bit thick. I used "thinning", which reduces white pixels, so I had to negate everything. If I had used "thicken", I wouldn't have needed to negate. Thus:
Loads of ways to skin a cat.
Code: Select all
convert ^
test.png ^
-verbose ^
-morphology Thicken ^
"3:1,1,1, 1,0,1, 1,1,1" ^
testThk.png
snibgo's IM pages: im.snibgo.com
Re: Basic pixel removal in black and white picture
Ok, thx for your answers.
I had some time to check this out but it's still unclear for me. :p
I got a lot of things work with Thicken, but is there a way to be more clear about it.
For instance :
If I have :
000
010
010
010
000
and I want to remove if this pattern is matched.
I did :
after that I have :
000
010
000
010
000
I can make a :
to remove the rest, but how can I say something like : delete the whole pattern.
I had some time to check this out but it's still unclear for me. :p
I got a lot of things work with Thicken, but is there a way to be more clear about it.
For instance :
If I have :
000
010
010
010
000
and I want to remove if this pattern is matched.
I did :
Code: Select all
-morphology Thicken "3x5: \
1,1,1, \
1,0,1, \
1,0,1, \
1,0,1, \
1,1,1" \
000
010
000
010
000
I can make a :
Code: Select all
-morphology Thicken "3x3: \
1,1,1, \
1,0,1, \
1,1,1" \
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Basic pixel removal in black and white picture
My "thicken" (or "thinning") code finds the location of a pattern, and turns the pixel at that location white.
If that's not what you want, you need to define what you do want. For example:
- Where there are two vertical black pixels surrounded by white, turn them both white. Do nothing else.
- Where there are one or two vertical black pixels surrounded by white, turn them it or them both white. Do nothing else.
- Where there are any number of vertical black pixels surrounded by white, turn them them all white. Do nothing else.
See http://www.imagemagick.org/Usage/morphology/
If that's not what you want, you need to define what you do want. For example:
- Where there are two vertical black pixels surrounded by white, turn them both white. Do nothing else.
- Where there are one or two vertical black pixels surrounded by white, turn them it or them both white. Do nothing else.
- Where there are any number of vertical black pixels surrounded by white, turn them them all white. Do nothing else.
See http://www.imagemagick.org/Usage/morphology/
snibgo's IM pages: im.snibgo.com
Re: Basic pixel removal in black and white picture
I understand the one pixel surrounded well.
But my problem is if there is more than one pixel I can't target the whole pattern.
In your exemple in the second message :
But in fact this code just remove for me only one pixel.
Transforming :
000
010
010
000
into :
000
000
010
000
but not the expected :
000
000
000
000
This pattern is even more annoying to remove :
0000
0010
0100
0000
But my problem is if there is more than one pixel I can't target the whole pattern.
In your exemple in the second message :
Code: Select all
convert ^
test.png ^
-negate ^
-morphology Thinning ^
"3x4:0,0,0, 0,1,0, 0,1,0, 0,0,0" ^
-negate ^
test4.png
Transforming :
000
010
010
000
into :
000
000
010
000
but not the expected :
000
000
000
000
This pattern is even more annoying to remove :
0000
0010
0100
0000
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: Basic pixel removal in black and white picture
Are you trying to make white go to black or black go to white?
Seems to me that if you want to fill black spots in a white background, you can create any such pattern and use -morphology close
see
http://www.imagemagick.org/Usage/morphology/#close
http://www.imagemagick.org/Usage/morphology/#user
http://www.imagemagick.org/Usage/morphology/#showkernel
I may be wrong, but just using a 3x4 white rectangle should handle, the following
000
010
010
000
or a 4x3 rectangle for the following
0000
0110
0000
But I have not tested this. So I may be wrong. It may do too much.
So snibgo's thinning approach may be better since it should be able to target unique patterns better. As he mentioned, you would need to negate the image, thin it, then negate it again, since it is looking to remove/thin white from a mostly black background. You might be able to use thicken without doing the two negates. Not sure how complementary it is.
Seems to me that if you want to fill black spots in a white background, you can create any such pattern and use -morphology close
see
http://www.imagemagick.org/Usage/morphology/#close
http://www.imagemagick.org/Usage/morphology/#user
http://www.imagemagick.org/Usage/morphology/#showkernel
I may be wrong, but just using a 3x4 white rectangle should handle, the following
000
010
010
000
or a 4x3 rectangle for the following
0000
0110
0000
But I have not tested this. So I may be wrong. It may do too much.
So snibgo's thinning approach may be better since it should be able to target unique patterns better. As he mentioned, you would need to negate the image, thin it, then negate it again, since it is looking to remove/thin white from a mostly black background. You might be able to use thicken without doing the two negates. Not sure how complementary it is.
Re: Basic pixel removal in black and white picture
I want to remove the black pixels part of a group of pixel with less than 5 pixels, whatever the pattern is.
Here is a file, I want to remove the black isolated pixels.
http://farm4.staticflickr.com/3772/9015 ... 74_o_d.gif
Of course I want to keep the three pattern on the bottom left, but remove everything else.
Here is a file, I want to remove the black isolated pixels.
http://farm4.staticflickr.com/3772/9015 ... 74_o_d.gif
Of course I want to keep the three pattern on the bottom left, but remove everything else.
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Basic pixel removal in black and white picture
Code: Select all
convert ^
in.gif ^
-verbose ^
-negate ^
-morphology Thinning "4x4:0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0" ^
-morphology Thinning:5 LineEnds ^
-morphology Thinning ^
"3:0,0,0, 0,1,0, 0,0,0" ^
-negate ^
out.gif
snibgo's IM pages: im.snibgo.com
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Basic pixel removal in black and white picture
You may like to thin using a 'Ring Kernel'
http://www.imagemagick.org/Usage/morphology/#ring
However some form of Line Ends (or sub-variation) as Fred suggested may be useful.
http://www.imagemagick.org/Usage/morphology/#lineends
For example this variation may work well (and is slightly more restrictive than the default)
'LineEnds:2@'
This variant may also work well and will remove single isolated pixels too (as it uses "I don't care" elements)
'LineEnds:4@'
NOTE the '@' rotate it by 45 degree increments up to 8 times (or until it repeats).
The '>' means rotate 90 degree increments.
NOTE the 4x4 kernel shown above should have 4 rotations or you will not match every pixel in a 2x2 square!
EG: '4x4>: 0,0,0,0 0,1,1,0 0,1,1,0 0,0,0,0'
A variation to match things smaller than 2x2 is to use some I dont care elements
'4x4+1+1>: 0,0,0,0 0,1,-,0 0,-,-,0 0,0,0,0'
That is any single pixel in a 2x2 area surrounded completely by black.
OR even remove corners from the set to make black 'ring' rather than a black square...
'4x4+1+1>: -,0,0,- 0,1,-,0 0,-,-,0 -,0,0,-'
Just remember these are defining 4 kernels, and will capture any isolated single, double, or triple pixels, but not 3 pixels in a line! or 4 pixels in any non-square formation.
http://www.imagemagick.org/Usage/morphology/#ring
However some form of Line Ends (or sub-variation) as Fred suggested may be useful.
http://www.imagemagick.org/Usage/morphology/#lineends
For example this variation may work well (and is slightly more restrictive than the default)
'LineEnds:2@'
This variant may also work well and will remove single isolated pixels too (as it uses "I don't care" elements)
'LineEnds:4@'
NOTE the '@' rotate it by 45 degree increments up to 8 times (or until it repeats).
The '>' means rotate 90 degree increments.
NOTE the 4x4 kernel shown above should have 4 rotations or you will not match every pixel in a 2x2 square!
EG: '4x4>: 0,0,0,0 0,1,1,0 0,1,1,0 0,0,0,0'
A variation to match things smaller than 2x2 is to use some I dont care elements
'4x4+1+1>: 0,0,0,0 0,1,-,0 0,-,-,0 0,0,0,0'
That is any single pixel in a 2x2 area surrounded completely by black.
OR even remove corners from the set to make black 'ring' rather than a black square...
'4x4+1+1>: -,0,0,- 0,1,-,0 0,-,-,0 -,0,0,-'
Just remember these are defining 4 kernels, and will capture any isolated single, double, or triple pixels, but not 3 pixels in a line! or 4 pixels in any non-square formation.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/
-
- Posts: 12159
- Joined: 2010-01-23T23:01:33-07:00
- Authentication code: 1151
- Location: England, UK
Re: Basic pixel removal in black and white picture
True, but in this case, it doesn't need to match every pixel; it just removes one pixel so the rest can be removed by LineEnds.Anthony wrote:NOTE the 4x4 kernel shown above should have 4 rotations or you will not match every pixel in a 2x2 square!
EG: '4x4>: 0,0,0,0 0,1,1,0 0,1,1,0 0,0,0,0'
Thanks for the info about LineEnds:2@ and LineEnds:4@.
snibgo's IM pages: im.snibgo.com
- anthony
- Posts: 8883
- Joined: 2004-05-31T19:27:03-07:00
- Authentication code: 8675308
- Location: Brisbane, Australia
Re: Basic pixel removal in black and white picture
Also note you normally do not need to repeat the morphology operator, just given it a list of kernels to apply to the image one kernel at a time.
By default the morphology:compose is set to 'none' meaning just do it with one kernel, then apply the next kernel to that result. This is actually why 'line-ends' could actually remove multiple pixels in the one iteration.
See IM Examples, Thinning Style - Sequential or Simultaneous
http://www.imagemagick.org/Usage/morpho ... ning_style
By default the morphology:compose is set to 'none' meaning just do it with one kernel, then apply the next kernel to that result. This is actually why 'line-ends' could actually remove multiple pixels in the one iteration.
See IM Examples, Thinning Style - Sequential or Simultaneous
http://www.imagemagick.org/Usage/morpho ... ning_style
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
https://imagemagick.org/Usage/