Open / SD Filter OpenCV

I throw it there in the hope that someone will try something so funny earlier. My goal is to take an input image and segment it based on the standard deviation of a small window around each pixel. Bascially, this should mathematically resemble a gauss or box filter, because it will be applied to the user size (or even runtime) time for each pixel, and the target array will contain SD information on each pixel, in the image the same size as the original.

The idea is to do this on an image in HSV space so that I can easily find areas of uniform color (i.e. those that have small local SD arrays in the Hue and Sat planes), and extract them from the image, processing depths.

So the question is, has anyone ever created a custom filter similar to this before? I don’t know how to make SD in a simple box-type filter kernel, like the ones used for gauss and blur, so I guess I have to use the FilterEngine construct. Also, I forgot to mention that I am doing this in C ++.

Your advice and thoughts are greatly appreciated.

+8
filter opencv image-segmentation standard-deviation
source share
1 answer

Wikipedia has a good explanation for standard deviation that you can use for a standard deviation filter.

Basically, it comes down to blurring the image using the box filter, blurring the square of the image using the box filter, and extracting the square root of their difference.

UPDATE: This is probably better shown using the equation from Wikipedia ... enter image description here

You can think of the OpenCV blur function as representing the expected value (ie E [X], as well as the average value of the sample) of the neighborhood of interest. Random samples X in this case are represented by image pixels in a local neighborhood. Therefore, using the above equivalence, we have something like sqrt(blur(img^2) - blur(img)^2) in OpenCV. The implementation of this method allows you to calculate local tools and standard deviations.

In addition, just in case, you are interested in learning a mathematical proof. This equivalence is known as the computational formula for rejection .

Here's how you can do it in OpenCV:

 #include <iostream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace std; using namespace cv; Mat mat2gray(const Mat& src) { Mat dst; normalize(src, dst, 0.0, 1.0, NORM_MINMAX); return dst; } int main() { Mat image = imread("coke-can.jpg", 0); Mat image32f; image.convertTo(image32f, CV_32F); Mat mu; blur(image32f, mu, Size(3, 3)); Mat mu2; blur(image32f.mul(image32f), mu2, Size(3, 3)); Mat sigma; cv::sqrt(mu2 - mu.mul(mu), sigma); imshow("coke", mat2gray(image32f)); imshow("mu", mat2gray(mu)); imshow("sigma",mat2gray(sigma)); waitKey(); return 0; } 

This results in the following images:

Original

enter image description here

Average

enter image description here

Standard deviation

enter image description here

Hope this helps!

+29
source share

All Articles