Ask Your Question
0

Smoothing Floatingpoint Mat, with a mask

asked 2013-12-28 12:51:20 -0600

gaepi gravatar image

Is there a way to apply a Gaussianblur or median smoothing filter to a mat of floating points while supplying a mask of pixels that should be ignored?

could you please help me? ty.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
4

answered 2013-12-30 04:18:45 -0600

I have not used the OpenCV to implement it.I've already implemented this code in ANSI C language. This code simply by changing the name of its functions is to become the Opencv C Edition.Stationary Gaussian kernel is used in the regular version but in the gaussian filter with mask for each pixel non zero in the mask you have to compute gaussian kernel for it.

typedef struct BhImageA
{
    int width;       //Image width in pixels
    int height;      // Image height in pixels
    int widthStep;   // The size of an aligned image row, in bytes
    byte* imageData; //A pointer to the aligned image data
    byte depth;      //Channel depth in bits
    BhRectA roi;     // Region Of Interest (ROI). If not NULL, only this image region will be processed
}BhImageA;

typedef struct BhRectA
{
    int x;  // Offset (usually the top-left corner) and size of a rectangle
    int y;  // y-coordinate of the top-left corner
    int width;  // y-coordinate of the top-left corner
    int height;  // y-coordinate of the top-left corner
}BhRectA;

#define BH_DEPTH_8  8
#define BH_DEPTH_32 32
#define BH_DEPTH_32_F 64
BhSizeA bhSizeA(int width,int height)
{
    BhSizeA result;
    result.width = width;
    result.height = height;
    return result;
}

BhRectA bhGetImageRectA(BhImageA* srcImage)
{
    BhRectA result;
    result.x = 0;
    result.y = 0;
    result.width = srcImage->width;
    result.height = srcImage->height;
    return result;
}
int bhGetWidthStepA(int width)
{
    return  int(ceil(width / 4.0)*4);
}
void bhMulSA(BhImageA* srcImage,BhImageA* dstImage,double value)
{
    if (srcImage->depth == BH_DEPTH_8)
    {
        byte v=(byte)value;
        for (int i = 0; i < srcImage->height; i++)
        {
            byte* srcRow = srcImage->imageData + srcImage->widthStep * i;
            byte* dstRow = dstImage->imageData + dstImage->widthStep * i;
            for (int j = 0; j < srcImage->width; j++)
                dstRow[j] = srcRow[j]* v;

        }
    }
    else if (srcImage->depth == BH_DEPTH_32)
    {
        int v=(int)value;
        for (int i = 0; i < srcImage->height; i++)
        {
            int* srcRow = (int*)srcImage->imageData + srcImage->widthStep * i;
            int* dstRow = (int*)dstImage->imageData + dstImage->widthStep * i;
            for (int j = 0; j < srcImage->width; j++)
                dstRow[j] = srcRow[j]* v;


        }

    }
    else if (srcImage->depth == BH_DEPTH_32_F)
    {
        float v=(double)value;
        for (int i = 0; i < srcImage->height; i++)
        {
            float* srcRow = (float*)srcImage->imageData + srcImage->widthStep * i;
            float* dstRow = (float*)dstImage->imageData + dstImage->widthStep * i;
            for (int j = 0; j < srcImage->width; j++)
                dstRow[j] = srcRow[j]* v;

        }
    }
}

double bhSumA(BhImageA* srcImage,BhImageA* maskImage)
{
    double sum = 0;
    BhRectA roi = srcImage->roi;
    if (maskImage == NULL)
    {
        if (srcImage->depth == BH_DEPTH_8)
        {
            for (int i = roi.y; i < roi.y + roi.height; i++)
            {
                byte* row = srcImage->imageData + srcImage->widthStep * i;
                for (int j = roi.x; j < roi.x + roi.width; j++)
                    sum += row[j];

            }
        }
        else if (srcImage->depth == BH_DEPTH_32)
        {
            for (int i = roi.y; i < roi.y + roi.height ; i++)
            {
                int* row = (int*)srcImage->imageData + srcImage->widthStep * i;
                for (int j = roi.x ; j < roi.x + roi.width; j++)
                    sum += row[j];

            }

        }
        else if (srcImage->depth == BH_DEPTH_32_F)
        {
            for (int i = roi.y; i < roi.y + roi.height ; i++)
            {
                float* row = (float*)srcImage->imageData + srcImage->widthStep * i;
                for (int j = roi.x; j < roi.x + roi.width; j++)
                    sum += row[j];

            }

        }
    }
    else if (maskImage != NULL)
    {
        if (srcImage->width != maskImage- ...
(more)
edit flag offensive delete link more

Comments

And someone said that it's not out of the box °.°''

that's a lot of code :I anyway I've wrote my version of this in c++ and opencv.. it's quite simple.

gaepi gravatar imagegaepi ( 2013-12-30 09:19:50 -0600 )edit
-2

answered 2013-12-29 06:47:09 -0600

Guanta gravatar image

Not out of the box. However, you can do the following: fist safe the matrix you want to blur (e.g. via clone()), then set all pixels to 0 which should be ignored, apply gauessian-blur, and then copy back the ignored pixels from the original matrix (i.e. the saved one).

edit flag offensive delete link more

Comments

nah, this way the pixel on the edges of the mask point will be smoothed with the points that i don't want to consider. I think i have to implement my own gaussian function with masking.

gaepi gravatar imagegaepi ( 2013-12-29 12:22:36 -0600 )edit

setting to 0 the pixels in the image, will produce an incorrect smoothing. Please read question first.

gaepi gravatar imagegaepi ( 2013-12-29 12:26:04 -0600 )edit

i figured it out. in 22 hours i can post the answer to my own question :/

gaepi gravatar imagegaepi ( 2013-12-29 13:55:08 -0600 )edit

Question Tools

Stats

Asked: 2013-12-28 12:51:20 -0600

Seen: 1,082 times

Last updated: Dec 30 '13