Reputation: 677
I am translating the code written in Matlab to OpenCV. In Matlab, there is ordfilt2 function. Is there any similar implementation in OpenCV? Thanks
Upvotes: 0
Views: 2593
Reputation: 1
dilation = ordfilt2(proj, 5, ones(1,5)); %in matlab
Following is my implementation in opencv, proj is 1D matrix of size (26,1)
Mat dilation = proj.clone(); Mat array = Mat::zeros(5, 1, CV_32FC1);
for (int i = 0; i < 26; i++)
{
(i - 2) < 0 ? array.at<float>(0, 0) = 0 : array.at<float>(0, 0) = proj.at<float>(i - 2, 0);
(i - 1) < 0 ? array.at<float>(1, 0) = 0 : array.at<float>(1, 0) = proj.at<float>(i - 1, 0);
(i) < 0 ? array.at<float>(2, 0) = 0 : array.at<float>(2, 0) = proj.at<float>(i, 0);
(i + 1) >= 26? array.at<float>(3, 0) = 0 : array.at<float>(3, 0) = proj.at<float>(i + 1, 0);
(i + 2) >= 26? array.at<float>(4, 0) = 0 : array.at<float>(4, 0) = proj.at<float>(i + 2, 0);
float temp = 0;
for (int j = 0; j < 5; j++)
{
if (temp < array.at<float>(j, 0))
temp = array.at<float>(j, 0);
}
dilation.at<float>(i, 0) = temp;
}
Upvotes: 0
Reputation: 677
I implemented the ordfilt2() in Matlab using erosion or dilation in OpenCV. ordfilt2(A,1,ones(3,3))
in Matlab is erosion function and can be replaced with cv::erode(im, mx, mk,cv::Point(-1,-1),1,BORDER_CONSTANT );
in OpenCV. ordfilt2(A,9,ones(3,3)) in Matlab is dilation and can be replaced with cv::dilate(im, mx, mk,cv::Point(-1,-1),1,BORDER_CONSTANT );
.
Upvotes: 1
Reputation: 5139
I don't think there is a general one. Browse here to find if something suits you.
From Matlab help:
B = ordfilt2(A, order, domain) replaces each element in A by the orderth element in the sorted set of neighbors specified by the nonzero elements in domain.
So you have an image A, and your slide a kernel(domain) which sorts pixel values and you pick always one specific order.
OpenCV steps (for a grayscale image):
1) clone image canvas
2) slide on each pixel of the canvas
3) create a vector of a subregion of the same pixel on your image
4) sort the vector
5) set the new image pixel value to the desired vector element.
Upvotes: 0