Ask Your Question
1

OpenCV reduce(), can I use median function?

asked 2016-11-04 08:03:57 -0500

anauta gravatar image

Hi,

I have used the reduce() function often. From time to time I would like to use the median instead of min, max, avg, or sum. E.g I would need CV_REDUCE_MEDIAN.

I looked in the source code but could not find an easy way to accomplish this.

Can anyone help? Best regards, Auke

edit retag flag offensive close merge delete

Comments

1

may be you can use row or col method to copy to a vector and used sort

LBerger gravatar imageLBerger ( 2016-11-04 10:15:48 -0500 )edit

opencv can sort Mat's too.

berak gravatar imageberak ( 2016-11-04 10:21:24 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2016-11-04 10:52:32 -0500

LBerger gravatar image

May be something like this :

Mat x(32,64,CV_16UC1);
for (int i=0;i<x.rows;i++)
    for (int j=0;j<x.cols;j++)
        x.at<ushort>(i,j)=i+j;

Mat v;
cv::sort(x, v, CV_SORT_EVERY_ROW);
for (int i = 0; i < x.rows; i++)
{
     cout<<"Line "<<i<< "median = "<< v.at<ushort>( i,v.cols / 2) <<"\n";
}
cv::sort(x, v, CV_SORT_EVERY_COLUMN);
for (int j = 0; j < x.cols; j++)
{
    cout << "Col " << j << "median = " << v.at<ushort>(v.rows /2,j ) << "\n";
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2016-11-04 08:03:57 -0500

Seen: 334 times

Last updated: Nov 04 '16