Matrix subtraction normalization [closed]
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?
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
1) you have a buffer overflow:
for (int i = 0; i <= src.cols ; i++)
must be<
, not<=
same for rows 2) you only subtracthalf
of px2, your code does not implement the formula abovety @berak . updated.
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()