更新:我现在使用了新方法。
Mat MedianFilter(Mat img, ushort SizeY, ushort SizeX, ushort kernelSize){
// Alloc memory for images
Mat medianImg(SizeY, SizeX, CV_16U, Scalar(0)), padded;
copyMakeBorder(img, padded, kernelSize/2, kernelSize/2, kernelSize/2, kernelSize/2, BORDER_CONSTANT, Scalar(0));
for (unsigned y = 0; y < 50; y++){
for (unsigned x = 0; x < 50; x++){
// Set median at central pixel pos
medianImg.at<ushort>(y, x) = getMedian(padded(Rect(x, y, kernelSize, kernelSize)));//sorter[n];
//sorter.clear();
}
return medianImg;
}
并且get Median函数适用于opencv例程中的16位:
int getMedian(Mat& img){
int channels = 0;
Mat hist;
int hist_size = 65535;
float range[] = {0, 65535} ;
const float* ranges[] = {range};
calcHist(&img, 1, &channels, Mat(), hist, 1, &hist_size, ranges);
float *ptr = hist.ptr<float>();
int median = 0, sum = 0;
int thresh = (int)img.total() / 2;
while(sum < thresh && median < 65535) {
sum += static_cast<int>(ptr[median]);
median++;
}
return median;
}
现在的问题是,我不确定边框是否正确。