Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

python 3.x - Image Filter which uses the highest occurence of pixel values

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...