Sharpie Sharpie - 1 month ago 23
C++ Question

Stdfilt in Opencv

I am currently looking through he documentation of openCV atempting to find the matlab equivelent of stdfilt could anyone point me in the correct direction?

thanks.

Answer

Looking at the source code inside the stdfilt.m file, we can see it is implemented using convolution..

I ported the code to Python, it should be straightforward to rewrite in C\C++:

import cv2
import numpy as np

img = cv2.imread('fruits.jpg', True)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
img = img / 255.0

# c = imfilter(I,h,'symmetric');
h = np.ones((3,3))
n = h.sum()
n1 = n - 1
c1 = cv2.filter2D(img**2, -1, h/n1, borderType=cv2.BORDER_REFLECT)
c2 = cv2.filter2D(img, -1, h, borderType=cv2.BORDER_REFLECT)**2 / (n*n1)
J = np.sqrt( np.maximum(c1-c2,0) )

cv2.imshow('stdfilt', J)
cv2.waitKey(0)
cv2.destroyWindow('stdfilt')

The result:

fruits python_stdfilt

Compare against MATLAB's version:

I = imread('fruits.jpg');
I = im2double(rgb2gray(I));
imshow(stdfilt(I))

MATLAB_stdfilt

Comments