本文介绍了ImageMagick:自定义排名过滤器?像侵蚀,扩张或中位数,但等级不同,例如“百分位数"过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用ImageMagick的等级过滤器(例如腐蚀或缓蚀或中值)时,会采用最小值( erode )或最大值( dilate )或中间(每个源像素周围一定半径或自定义形状内的所有像素的中间值).

When applying a rank filter with ImageMagick such as erode or delate or median, it takes the minimum (erode) or maximum (dilate) or middle (median) value of all pixels within a certain radius or custom shape around each source pixel.

是否还可以对周围像素值进行自定义排名?例如,当使用5x5正方形时,分别使用25个中间值中的最低值,最高值和 erode dilate median 过滤器拍摄每个像素周围的像素.例如,我正在寻找一种采用第8或23rd值的方法.

Is it also possible to take a custom rank of the surrounding pixel values? For example when using a 5x5 square, with the erode or dilate or median filters, respectively the lowest, highest, of middle value of the 25 pixels surrounding each pixel is taken. What I'm looking for is a way to take the 8th or 23rd value, for example.

也许这可以表示为百分位数,例如取每个像素周围任意区域的20%百分位值.在7x7正方形的情况下,该值将是0.2 * 7 * 7 =顺序中总共49个值中的第9个值.

Perhaps this could be expressed as a percentile, e.g. take the 20% percentile value of an arbitrary area surrounding each pixel. Which in case of a 7x7 square would be the 0.2*7*7 = the 9th value out of the total 49 in order.

erode dilate median 过滤器分别对应于0%,100%和50%百分位.

The erode, dilate and median filters would correspond to the 0%, 100%, and 50% percentiles, respectively.

推荐答案

除了在您自己的进程中进行编译之外,我想不出一种在 ImageMagick 中轻松实现此目的的方法模块" .最好的介绍是 snibgo ,实际上是Windows的-请参见 sortpixels.c 示例此处

I can't think of a way to do that easily in ImageMagick - other than compiling in your own "process module". The best introduction to that is by snibgo and actually for Windows - see the sortpixels.c example here

一个可以从命令行简单执行此操作的工具是 libvips 及其 im_rank()函数.

One tool that can do this simply from the command-line is libvips with its im_rank() function.

因此,如果您希望从5x5邻居的排序列表中获得索引8,则可以执行以下操作:

So, if you want index 8 from a sorted list of 5x5 neighbours, you could do:

vips im_rank input.png result.png 5 5 8

我通过使用 ImageMagick 生成随机图像并为 index 依次选择更大的值进行了快速测试,并且输出图像逐渐变得更亮-但这就是总和-我的测试总数.我没有理由相信它行不通-这是一个出色的图书馆,而且非常节俭.

I did a quick test by generating a random image with ImageMagick and choosing successively greater values for index and the output images got successively brighter - but that was the sum-total of my testing. I have no reason to believe it will not work - it is an excellent library and very fast and frugal.

如果您可以使用Python,则可以执行以下操作:

If you can live with Python, you can do this:

#!/usr/bin/env python3

import numpy as np
from PIL import Image
from scipy.ndimage import generic_filter
from scipy import stats

# Modal filter
def modal(P):
    """
    We receive P[0]..P[8] with the pixels in the 3x3 surrounding window
    Sort neighbours and take N'th in list
    """
    N = 3
    P.sort()
    return P[N]

# Open image and make into Numpy array
im = Image.open('image.png').convert('L')
im = np.array(im)

# Run modal filter, change filter size here
result = generic_filter(im, modal, (3, 3))

# Save result
Image.fromarray(result).save('result.png')

您可以通过更改以下行将滤镜的大小/形状更改为5x5:

You can change the filter size/shape to, say 5x5 by changing this line:

result = generic_filter(im, modal, (5, 5))

实际上,它将是第三小的邻居,因为计数从0开始.因此,如果要在3x3邻居中使用最小值,请使用 N = 0 N= 8 (如果您想在3x3的邻里中获得最大数量).

And as it is, it will take the third smallest neighbour, as counting starts from 0. So, use N=0 if you want the minimum in the 3x3 neighbourhood, or N=8 if you want the maximum in a 3x3 neighbourhood.

另一种选择可能是使用 CImg ,这是一种易于使用的纯标题C ++库(DLLs/libXXX.a/libXXX.so文件),它们实际上可以原生地读写PGM文件,而无需外部任何东西.因此,您可以运行 ImageMagick 命令,并使其在 stdout 上写入一个PGM文件,并使用 CImg 对其进行读取,处理和写入再次过滤掉.这可能比编写 ImageMagick 处理模块" 容易,并且您不需要为每个发行版重新从源代码构建 ImageMagick .

Another option might be to use CImg which is a simple-to-use, header-only C++ library (no DLLs/libXXX.a/libXXX.so files) that can actually read and write PGM files natively without needing anything external. So you could run an ImageMagick command and cause it to write a PGM file on stdout and read that in using CImg, process it and write it out filtered again. That would probably be easier than writing an ImageMagick "process module" and you wouldn't need to build ImageMagick from source anew for every release.

关键字:ImageMagick,vip,libvips,过滤器,等级,排名,中位数,膨胀,腐蚀,窗口,3x3、5x5,NxN,百分位,Python,PIL,枕头,图像,图像处理

Keywords: ImageMagick, vips, libvips, filter, rank, ranking, median, dilate, erode, window, 3x3, 5x5, NxN, percentile, Python, PIL, Pillow, image, image processing.

这篇关于ImageMagick:自定义排名过滤器?像侵蚀,扩张或中位数,但等级不同,例如“百分位数"过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 07:02