# denoise binary image by density c++

i have amethods like below that have good results:

void TargetExtractor::denoise(int ksize, int threshold)
{
int r = (ksize - 1) / 2;
if (r <= 0) {
return;
}

Mat density;

for (int i = r; i < mMask.rows - r; i++) {
for (int j = r; j < mMask.cols - r; j++) {
int count = density.at<int>(i, j);
if (count < threshold) {
}
}
}
}


But its time is 120-140 ms!!! can you suggest me another methods?

in the code most time related to calcdensity:

/ this function is an alternative to the old one which is implemented with 'sum'
void calcDensity(const Mat& mask, Mat& density, int ksize)
{
int r = (ksize - 1) / 2;
if (r <= 0) {
return;
}

int rowBound = density.rows - r, colBound = density.cols - r;

Rect rect(0, 0, ksize, ksize);
density.at<int>(r, r) = cvRound(sum(mask(rect))[0] / 255);

for (int j = r + 1; j < colBound; j++) {
int col1 = j - r - 1, col2 = j + r;
int delta = 0;
for (int k = 0; k < ksize; k++) {
}
density.at<int>(r, j) = density.at<int>(r, j - 1) + delta / 255;
}

for (int i = r + 1; i < rowBound; i++) {
int row1 = i - r - 1, row2 = i + r;
int delta = 0;
for (int k = 0; k < ksize; k++) {
}
density.at<int>(i, r) = density.at<int>(i - 1, r) + delta / 255;
}

for (int i = r + 1; i < rowBound; i++) {
for (int j = r + 1; j < colBound; j++) {
int delta = (mask.at<uchar>(i + r, j + r) - mask.at<uchar>(i - r - 1, j + r) -
mask.at<uchar>(i + r, j - r - 1) + mask.at<uchar>(i - r - 1, j - r - 1)) / 255;
density.at<int>(i, j) = density.at<int>(i - 1, j) + density.at<int>(i, j - 1) -
density.at<int>(i - 1, j - 1) + delta;
}
}
}

edit retag close merge delete

Sort by ยป oldest newest most voted

You can use operator < and setTo method :

Mat mask=density<thresold;

more

i edited post, your solution was good , but how about density? tnx for guide me

@LBerger

( 2017-04-17 00:07:38 -0600 )edit

What 's mathematics formulation of cacDensity function?

( 2017-04-17 01:53:06 -0600 )edit

i dont know actualy ,this is part of an open source project,i adjusted it but its slow @LBerger

( 2017-04-17 05:46:24 -0600 )edit

you can use ParallelLoopBody and ptr

( 2017-04-17 07:16:57 -0600 )edit

Official site

GitHub

Wiki

Documentation