Skip to content

Commit e774551

Browse files
committed
Add bwareaopen
1 parent b7265d6 commit e774551

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

documentation/all_docs_ref/all_refs.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ its use requires resorting to the \myreflink{Monolithic} mode.
6868

6969
| | | | | | | | |
7070
|:-----|:----|:----|:----|:----|:----|:----|:----|
71-
| \myreflink{binarize} | \myreflink{bwhitmiss} | \myreflink{bwperim} | \myreflink{bwskell} | \myreflink{fillsinks} | \myreflink{imbothat} | \myreflink{imclose} | \myreflink{imcomplement} |
72-
| \myreflink{imdilate} | \myreflink{imerode} | \myreflink{imfill} | \myreflink{imfilter} | \myreflink{imhdome} | \myreflink{imhmin} | \myreflink{imhmax} | \myreflink{immorphgrad} |
73-
| \myreflink{imopen} | \myreflink{imrankfilter} | \myreflink{imreconstruct} | \myreflink{imsegment} | \myreflink{imsobel} | \myreflink{imtophat} | \myreflink{isodata} | \myreflink{padarray} |
74-
| \myreflink{strel} | \myreflink{rgb2gray} | \myreflink{rgb2lab} | \myreflink{rgb2ycbcr} | | | | |
71+
| \myreflink{binarize} | \myreflink{bwareaopen} | \myreflink{bwhitmiss} | \myreflink{bwperim} | \myreflink{bwskell} | \myreflink{fillsinks} | \myreflink{imbothat} | \myreflink{imclose} |
72+
| \myreflink{imcomplement} | \myreflink{imdilate} | \myreflink{imerode} | \myreflink{imfill} | \myreflink{imfilter} | \myreflink{imhdome} | \myreflink{imhmin} | \myreflink{imhmax} |
73+
| \myreflink{immorphgrad} | \myreflink{imopen} | \myreflink{imrankfilter} | \myreflink{imreconstruct} | \myreflink{imsegment} | \myreflink{imsobel} | \myreflink{imtophat} | \myreflink{isodata} |
74+
| \myreflink{padarray} | \myreflink{strel} | \myreflink{rgb2gray} | \myreflink{rgb2lab} | \myreflink{rgb2ycbcr} | | | |
7575

7676
## GDAL utility functions
7777

documentation/modules/grdgravmag3d.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Required Arguments
2020
If two grids are provided then the gravity/magnetic effect of the volume between them is computed.
2121

2222
- **C** or **density** : -- *density=??*\
23-
Sets body density in _SI_. Append either a constant, the name of a grid file or a GMTgrid
23+
Sets body density in _SI_. Append either a constant, the name of a grid file or a \myreflink{GMTgrid}
2424
grid with variable densities. This option is mutually exclusive with **mag_params**
2525

2626
- **F** or **track** : -- *track=xy_loc*\

documentation/utilities/bwareaopen.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)