Skip to content

Commit

Permalink
Two new IP funs
Browse files Browse the repository at this point in the history
  • Loading branch information
joa-quim committed Jan 19, 2025
1 parent f7e5bf0 commit 6bc0db9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
Franklin = "713c75ef-9fc9-4b05-94a9-213340da978e"
GMT = "5752ebe1-31b9-557e-87aa-f909b540aa54"
Gumbo = "708ec375-b3d6-5a57-a7ce-8257bf98657a"
#Gumbo = "708ec375-b3d6-5a57-a7ce-8257bf98657a"
ImageCore = "a09fc81d-aa75-5fe9-8630-4744c3626534"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
Expand Down
3 changes: 2 additions & 1 deletion documentation/all_docs_ref/all_refs.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ its use requires resorting to the \myreflink{Monolithic} mode.
|:-----|:----|:----|:----|:----|:----|:----|:----|
| \myreflink{binarize} | \myreflink{bwhitmiss} | \myreflink{bwperim} | \myreflink{bwskell} | \myreflink{fillsinks} | \myreflink{imbothat} | \myreflink{imclose} | \myreflink{imcomplement} |
| \myreflink{imdilate} | \myreflink{imerode} | \myreflink{imfill} | \myreflink{imhdome} | \myreflink{imhmin} | \myreflink{imhmax} | \myreflink{immorphgrad} | \myreflink{imopen} |
| \myreflink{imreconstruct} | \myreflink{imtophat} | \myreflink{isodata} | \myreflink{padarray} | \myreflink{strel} | \myreflink{rgb2gray} | \myreflink{rgb2lab} | \myreflink{rgb2ycbcr} |
| \myreflink{imreconstruct} | \myreflink{imrankfilter} | \myreflink{imsegment} | \myreflink{imtophat} | \myreflink{isodata} | \myreflink{padarray} | \myreflink{strel} | \myreflink{rgb2gray} |
| \myreflink{rgb2lab} | \myreflink{rgb2ycbcr} | | | | | | |


## GDAL utility functions
Expand Down
47 changes: 47 additions & 0 deletions documentation/utilities/imrankfilter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# imrankfilter

```julia
J = imrankfilter(I::GMTimage; width::Int=3, height::Int=0, rank=0.5)::GMTimage
```

*keywords: GMT, Julia, image rank filter*

Rank order filter.

This defines, for each pixel, a neighborhood of pixels given by a rectangle "centered" on the pixel.
This set of ``width x height`` pixels has a distribution of values, and if they are sorted in increasing
order, we choose the pixel such that rank*(wf*hf-1) pixels have a lower or equal value and (1-rank)*(wf*hf-1)
pixels have an equal or greater value. In other words, `rank=0` returns the minimun, `rank=1` returns
the maximum in box and `rank=0.5` returns the median. Other values return the quantile.

### Args
- `I::GMTimage`: Input image. This can be a RGB, grayscale or a binary image.

### Kwargs
- `width::Int=3`: Width of the filter.

- `height::Int`: Height of the filter (defaults to `width`).

- `rank=0.5`: Rank.

### Returns
A new \myreflink{GMTimage} of the same type as `I` with the filtered image.

Example
-------

\begin{examplefig}{}
```julia
using GMT

I = gmtread(TESTSDIR * "assets/small_squares.png");
J = imrankfilter(I, width=10);
grdimage(I, figsize=6)
grdimage!(J, figsize=6, xshift=6, show=true)
```
\end{examplefig}

See Also
--------

\myreflink{imdilate}, \myreflink{imerode}, \myreflink{imopen}, \myreflink{imclose}
58 changes: 58 additions & 0 deletions documentation/utilities/imsegment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# imsegment

```julia
J = function imsegment(I::GMTimage{<:UInt8, 3}; maxdist::Int=0, maxcolors::Int=0, selsize::Int=3, colors::Int=5)::GMTimage
```

*keywords: GMT, Julia, image segmentation*

Unsupervised RGB color segmentation.

For more details see the docs in Leptonica's function ``pixColorSegment`` (in src/colorseg.c).

### Args
- `I::GMTimage{<:UInt8, 3}`: Input RGB image.

### Kwargs
- `maxdist::Int=0`: Maximum euclidean dist to existing cluster.

- `maxcolors::Int=0`: Maximum number of colors allowed in first pass.

- `selsize::Int=3`: Size of the structuring element for closing to remove noise.

- `colors::Int=5`: Number of final colors allowed after 4th pass.

As a very rough guideline (Leptonica docs), given a target value of `colors`, here are
approximate values of `maxdist` and `maxcolors`:

| `colors` | `maxcolors` | `maxdist` |
|----------|-------------|-----------|
| 2 | 4 | 150 |
| 3 | 6 | 100 |
| 4 | 8 | 90 |
| 5 | 10 | 75 |
| 6 | 12 | 60 |
| 7 | 14 | 45 |
| 8 | 16 | 30 |

### Returns
A new, indexed, \myreflink{GMTimage} with the segmentation.

Examples
--------

This was thought as a simple example but turned out to show a bit tricky result. The image
"bunny_cenora.jpg" is simple and we can clearly see that it has only 6 colors, so we would
expect that `colors=6` would do the job. But in fact we need to set `colors=7` because
the outline (black) is in fact picked as two different (dark) colors.

\begin{examplefig}{}
```julia
using GMT

I = gmtread(TESTSDIR * "assets/bunny_cenora.jpg");
J = imsegment(I, colors=7);
grdimage(I, figsize=6)
grdimage!(J, figsize=6, xshift=6, show=true)
```
\end{examplefig}

0 comments on commit 6bc0db9

Please sign in to comment.