|
| 1 | +# bwskell |
| 2 | + |
| 3 | +```julia |
| 4 | +bwareaopen(I::Union{GMTimage{UInt8,2}, GMTimage{Bool,2}}; keepwhites::Bool=false, keepblacks::Bool=false, kwargs...)::GMTimage |
| 5 | +``` |
| 6 | + |
| 7 | +*keywords: GMT, Julia, image processing, area opening* |
| 8 | + |
| 9 | +Remove all connected components (groups of pixels) that have fewer than P pixels from the binary image ``Ibw``. |
| 10 | + |
| 11 | +Remove groups of pixels in the image that are smaller than a provided threshold size (in pixels) |
| 12 | +and replaces them with the pixel value of the largest neighbor polygon. This operation is known as an area opening. |
| 13 | + |
| 14 | +_Polygons_ are determined as regions of the image where the pixels all have the same value, and that are |
| 15 | +contiguous (connected). The work is done by the GDAL ``GDALSieveFilter`` function. |
| 16 | + |
| 17 | + |
| 18 | +### Args |
| 19 | +- `I::Union{GMTimage{<:UInt8, 2}, GMTimage{<:Bool, 2}}`: Input binary image. It can be a GMTimage object or a file name that once read by ``gmtread`` returns a binary image. |
| 20 | + |
| 21 | +### Kwargs |
| 22 | +- `keepwhites`: If set to `true` keeps all the white pixels (or true) in input image as white in the output image. |
| 23 | + That is, only let the black pixels be set to white (or true). |
| 24 | + |
| 25 | +- `keepblacks`: If set to `true` keeps the black pixels (or false) in the input image as black in the output image. |
| 26 | + That is, only let the white pixels be set to black (or false). |
| 27 | + |
| 28 | +- `threshold`: groups of pixels with sizes smaller than this will be merged into their largest neighbor. |
| 29 | + The default is 10. |
| 30 | + |
| 31 | +- `conn`: Connectivity. Either 4 indicating that diagonal pixels are not considered directly adjacent for polygon |
| 32 | + membership purposes or 8 indicating they are. The default is 4. |
| 33 | + |
| 34 | +### Returns |
| 35 | +A new binary \myreflink{GMTimage}. |
| 36 | + |
| 37 | +Example |
| 38 | +------- |
| 39 | + |
| 40 | +\begin{examplefig}{} |
| 41 | +```julia |
| 42 | +using GMT |
| 43 | + |
| 44 | +# Read the test image |
| 45 | +I = gmtread(TESTSDIR * "assets/face_bw.png"); |
| 46 | + |
| 47 | +# Remove the small blobs using the default threshold size (10 pixels) |
| 48 | +Ic = bwareaopen(I); |
| 49 | +grdimage(I, figsize=6) |
| 50 | +grdimage!(Ic, figsize=6, xshift=6, show=true) |
| 51 | +``` |
| 52 | +\end{examplefig} |
| 53 | + |
| 54 | +The result is almos good as all salt-&-pepper noise has gone, buy two outer white patches |
| 55 | +survived cleaning. We could have used a larger than 10 pixes threshold value in `bwareaopen` call |
| 56 | +but that would have removed the _mouth_ as well. To solve this we are going to take a more brute |
| 57 | +force approach. We will remove all patches smaller than 1000 pixels, which removes also the eyes, |
| 58 | +eyebrows and mouth but restore them in the same step by invoking the option `keepblacks`. |
| 59 | + |
| 60 | + |
| 61 | +\begin{examplefig}{} |
| 62 | +```julia |
| 63 | +using GMT |
| 64 | + |
| 65 | +I = gmtread(TESTSDIR * "assets/face_bw.png"); #Hide |
| 66 | + |
| 67 | +# Brute cleaning |
| 68 | +Ic1 = bwareaopen(I, thresh=1000); |
| 69 | + |
| 70 | +# Retain the "blacks" |
| 71 | +Ic2 = bwareaopen(Ic1, thresh=1000, keepblacks=true); |
| 72 | + |
| 73 | +# But this still leaves some noise over the eyes, so clean it again |
| 74 | +Ic3 = bwareaopen(Ic2) |
| 75 | + |
| 76 | +grdimage(Ic1, figsize=5) |
| 77 | +grdimage!(Ic2, figsize=5, xshift=5) |
| 78 | +grdimage!(Ic3, figsize=5, xshift=5, show=true) |
| 79 | +``` |
| 80 | +\end{examplefig} |
0 commit comments