Antialiasing for labels
See original GitHub issueIs your feature request related to a problem? Please describe. Low resolution labels are often too jagged.
Describe the solution you’d like
Originally I hoped that the Spacingd
transform would just have some antialiasing.
But a separate transform for this might be a better solution anyway.
I implemented a simple transform using a gaussian filter combined with a threshold on every label.
class Antialiasingd(MapTransform):
def __init__(
self,
keys: KeysCollection,
sigma: Union[Sequence[float], float] = 1.0,
approx: str = "erf",
threshold: float = 0.5,
allow_missing_keys: bool = False,
) -> None:
super().__init__(keys, allow_missing_keys)
self.sigma = sigma
self.approx = approx
self.threshold = threshold
def __call__(self, data: Mapping[Hashable, NdarrayTensor]) -> Dict[Hashable, NdarrayTensor]:
d = dict(data)
for key in self.key_iterator(d):
img = d[key]
gaussian_filter = GaussianFilter(img.ndim - 1, self.sigma, approx=self.approx)
labels = torch.unique(img)[1:]
new_img = torch.zeros_like(img)
for label in labels:
label_mask = (img == label).to(torch.float)
blurred = gaussian_filter(label_mask.unsqueeze(0)).squeeze(0)
new_img[blurred > self.threshold] = label
d[key] = new_img
return d
What do you think?
Here some results
Parameters | Result |
---|---|
Original | |
Sigma=1.0, Threshold=0.5 | |
Sigma=4.0, Threshold=0.5 | |
Sigma=4.0, Threshold=0.4 |
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (12 by maintainers)
Top Results From Across the Web
Anti-Alias Text in Label - MSDN - Microsoft
Hi guys. Can anyone help me work out how to anti-alias the text in a label? I'm not quite sure of the best...
Read more >Anti aliasing label and transparent background (c# visual studio)
how can I add Anti aliasing to the labels and remove/ make the form background transparent.
Read more >antialias [PyMOL Documentation]
Anti-aliasing is a graphical technique for smoothing pixel-based images to reduce discrete edges. No Anti-aliasing, Anti-aliased. No ...
Read more >Image antialiasing — Matplotlib 3.6.2 documentation
The default image interpolation in Matplotlib is 'antialiased', and it is applied to the data. This uses a hanning interpolation on the data...
Read more >Some more advanced formatting - JpGraph
If you want to use background images or improve performance anti-aliasing can ... Since scale labels are normal text element all normal text...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Another option could be median smoothing. We achieved very good and fast results with the below transform.
cc @kbressem
Is there interest in adding a median filter based on @ebrahimebrahim’s medianblur_with_gpu? If so, I would be interested in contributing it with a PR.