使用像素值出现率最高的图像滤镜 [英] Image Filter which uses the highest occurence of pixel values

查看:114
本文介绍了使用像素值出现率最高的图像滤镜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用图像滤镜,该滤镜应以发生率最高的邻居替换它处理的像素. 例如,如果像素的值为10,并且8个邻居的像素为9、9、9、27、27、200、200、210,则应选择9,因为9在邻居中的出现率最高.它也应该考虑像素本身.因此,例如,如果像素的值为27,而8个相邻像素的值为27、27、30、30、34、70、120、120,则应该选择27,因为27包含像素本身在内是3次. 我还应该有选择内核大小的选项. 我没有找到这样的过滤器.有一个吗?还是我必须自己创建它?我将opencv与python一起使用.

背景信息: 我不能只使用中值过滤器,因为我的图像是不同的.我有3到6个不同灰度值的灰度图像.因此,我无法使用某些形态转换.我没有得到想要的结果.中值过滤器将选择中值,因为这样的想法是,这些值以正确的方式表示图像.但是我的图像是kmeans的结果,并且3-6个不同的灰度值没有逻辑联系.

解决方案

您可以在 skimage 中使用模式过滤器,例如此处.


或者,如果您的需求略有不同,则可以尝试使用 scipy 中的generic_filter()(文档


请注意, OpenCV 图像可以与Numpy数组完全互换,因此您可以使用 OpenCV image = imread(),然后对该图像调用我上面建议的功能. /p>

关键字:Python,PIL,Pillow,skimage,简单过滤器,通用过滤器,均值,中位数,众数,模式,图像,图像处理,numpy

I want to use an image filter, which should replace the pixel it's dealing with with the highest occurence of the neighbors. For example if the pixel has the value 10, and the 8 neighbors have 9, 9, 9, 27, 27, 200, 200, 210, then it should pick 9, because 9 has the highest occurence in the neighborhood. It also should consider the pixel itself, too. So for example if the pixel has the value 27 and the 8 neighbors have 27, 27, 30, 30, 34, 70, 120, 120 then it should pick 27, because 27 is there 3 times, including the pixel itself. I also should have the option to choose the size of the kernel. I didn't find a filter like that. Is there one? Or do i have to create it on my own? I use opencv with python.

Background information: I can't just use Median filter, because my images are different. I have gray images with 3 to 6 different gray values. Therfore i can't use some morphological transformations. I don't get the result i want. The median filter would pick the median value, because the idea is that the values represent the image in the right way. But my images are the result of kmeans and the 3-6 different gray values don't have a logical connection.

解决方案

You can use the modal filter in skimage, example here, documentation here.


Or if your needs differ slightly, you could experiment with the generic_filter() in scipy (documentation here) along these lines:

#!/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"""
    mode = stats.mode(P)
    return mode.mode[0]

# Open image and make into Numpy array - or use OpenCV 'imread()'
im = Image.open('start.png').convert('L')
im = np.array(im)

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

# Save result or use OpenCV 'imwrite()'
Image.fromarray(result).save('result.png')


Note that OpenCV images are completely interchangeable with Numpy arrays, so you can use OpenCV image = imread() and then call the functions I am suggesting above with that image.

Keywords: Python, PIL, Pillow, skimage, simple filter, generic filter, mean, median, mode, image, image processing, numpy

这篇关于使用像素值出现率最高的图像滤镜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆