Matrix subtraction normalization [closed]

asked 2016-02-15 09:16:10 -0600

chr0x gravatar image

updated 2016-02-15 13:58:11 -0600

Hello everyone.

Recently i had to implement an algorithm to subtract an image from this same image blurred. I first tried to:

Mat img = imread("some_image");
GaussianBlur(img, blurred, Size(7, 7), 0, 0);
Mat result = img - blurred;

But my output (result) was displayed as an black image.

So I found this normalization steps to solve the problem:

result_pixel = (pixel_image - pixel_blurred_image) / 2 + 127 ; for each pixel on image:

void sub(Mat & src, Mat & src2, Mat & result ) {

        for (int i = 0; i < src.cols ; i++) {
                for (int j = 0; j < src.rows; j++) {

                        int px1  = int(src.at<uchar>(j, i));
                        int px2 = int(src2.at<uchar>(j, i));
                        int px   = ( (px1 - px2 ) / 2) + 127;

                        result.at<uchar>(j, i) = px;

                }
        }
}

This kinda of normalization seems trivial to me. So I was wondering, doesn't Opencv already provides any option to apply this normalization automatically?

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2020-10-07 12:59:02.420967

Comments

Yes, your image seems it is blank because the noise has very low values, so you cannot see something with your eyes, you need a normalization. I would suggest you to transform images in CV_32F first then do the subtraction and then apply the normalize function on the result to make it more visible

thdrksdfthmn gravatar imagethdrksdfthmn ( 2016-02-15 09:34:22 -0600 )edit

1) you have a buffer overflow: for (int i = 0; i <= src.cols ; i++) must be < , not <= same for rows 2) you only subtract half of px2, your code does not implement the formula above

berak gravatar imageberak ( 2016-02-15 10:07:05 -0600 )edit

ty @berak . updated.

chr0x gravatar imagechr0x ( 2016-02-15 14:02:01 -0600 )edit
1

There are several ways to do it. Like thdrksdfthmn said, the normalize function. If you know the specific numbers to scale and add, you can use convertTo()

Tetragramm gravatar imageTetragramm ( 2016-02-15 18:05:31 -0600 )edit