Image Filter which uses the highest occurence of p

2019-02-28 10:39发布

问题:

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.

回答1:

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.