Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

denoise binary image by density c++

i have amethods likebelow 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?

denoise binary image by density c++

i have amethods likebelow 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 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;
        }
    }
}

denoise binary image by density c++

i have amethods likebelow 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;
        }
    }
}