Ask Your Question
1

denoise binary image by density c++

asked 2017-04-16 03:51:49 -0600

sabra gravatar image

updated 2017-04-17 00:06:10 -0600

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;
    calcDensity(mMask, density, ksize);

    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) {
                mMask.at<uchar>(i, j) = 0;
            }
        }
    }
}

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

edit 2: tnx to reply guys please notice that the:

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;
    }

    density = Mat::zeros(mask.size(), CV_32SC1);

    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++) {
            delta += mask.at<uchar>(k, col2) - mask.at<uchar>(k, col1);
        }
        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++) {
            delta += mask.at<uchar>(row2, k) - mask.at<uchar>(row1, 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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-04-16 05:28:55 -0600

LBerger gravatar image

You can use operator < and setTo method :

Mat mask=density<thresold;
mMask.setTo(0,mask);
edit flag offensive delete link more

Comments

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

@LBerger

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

What 's mathematics formulation of cacDensity function?

LBerger gravatar imageLBerger ( 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

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

you can use ParallelLoopBody and ptr

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

Question Tools

1 follower

Stats

Asked: 2017-04-16 03:51:49 -0600

Seen: 525 times

Last updated: Apr 17 '17