Talk about OpenCV's saturate_cast anti-overflow

Source: Internet
Author: User
Tags scalar

The role of the

Saturate_cast function in OpenCV is to prevent data overflow, when we directly manipulate the pixel point, if the value of the result is assigned or more than 255, there is no way to display in the picture, this is the role of anti-data overflow, then when there will be the risk of data overflow? , which is more common in image convolution operations.
Below we give a chestnut bar:
Select a 3*3-sharpening convolution core, designed as follows:
(0,-1, 0,
-1, 5,-1,
0,-1, 0)
using OpenCV's filter2d function and writing The Convlution function implements the convolution of a picture:

#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp
> Using namespace std;

using namespace CV;
Mat Kernel_test_3_3 = (mat_<double> (3,3) << 0,-1,0, -1,5,-1, 0,-1,0);
    void Convlution (Mat inputimage,mat outputimage,mat kernel) {//Calculate convolution core radius int sub_x = KERNEL.COLS/2;
    int sub_y = KERNEL.ROWS/2; Traverse picture for (int image_y=0;image_y<inputimage.rows-2*sub_y;image_y++) {for (int image_x=0;image_x<i
            nputimage.cols-2*sub_x;image_x++) {int pix_value = 0; for (int kernel_y = 0;kernel_y<kernel.rows;kernel_y++) {for (int kernel_x = 0;kernel_x<k   ernel.cols;kernel_x++) {Double weihgt = kernel.at<double> (kernel_y,kernel_x)
                    ; 
                    int value = (int) inputimage.at<uchar> (image_y+kernel_y,image_x+kernel_x); Pix_value +=WEIHGT*value;
            }} outputimage.at<uchar> (image_y+sub_y,image_x+sub_x) = (Uchar) pix_value;
            Outputimage.at<uchar> (image_y+sub_y,image_x+sub_x) = saturate_cast<uchar> ((int) pix_value); if ((int) pix_value!= (int) saturate_cast<uchar> ((int) pix_value)) {//cout<< "No anti-overflow" &L
                t;< (int) pix_value<<endl;
                cout<< "anti-Overflow" << (int) saturate_cast<uchar> ((int) pix_value) <<endl; cout<< "There are no anti-overflow writes.
                "<< (int) outputimage.at<uchar> (image_y+sub_y,image_x+sub_x) <<endl;
            cout<<endl;
    }}}} int main () {Mat srcimage = Imread ("1.jpg", 0);
    Namedwindow ("Srcimage", window_autosize);

    Imshow ("original", Srcimage);
    Filter2d convolution Mat Dstimage_oprncv (srcimage.rows,srcimage.cols,cv_8uc1,scalar (0));; Filter2d (Srcimage,dstimage_oprncv,srcimage.depth (), Kernel_test_3_3);
    Imshow ("Filter2d convolution map", DSTIMAGE_OPRNCV);

    Imwrite ("1.jpg", DSTIMAGE_OPRNCV);
    Custom convolution Mat Dstimage_mycov (srcimage.rows,srcimage.cols,cv_8uc1,scalar (0));
    Convlution (Srcimage,dstimage_mycov,kernel_test_3_3);
    Imshow ("convolutional figure 3", Dstimage_mycov);

    Imwrite ("2.jpg", Dstimage_mycov);
    Waitkey (0);

return 0; }

The effect is as follows without the use of anti-overflow:

Original:

Use filter2d for grayscale images of the original image:

Use Convlution for grayscale images of the original image:

Then we add the anti-overflow, and then look at the effect:

Use Convlution for grayscale images of the original image:

The effect of the discovery and filter2d function is no different, because the function design does not consider the situation of the boundary filling, so the surrounding is black edge, but this is not the focus of this article, temporarily ignore it, then why join the anti-spill effect is so much worse, we notice that in the above program, We've commented on a few lines of code, and if we untie it, we'll see what the print looks like:

No anti-overflow-30
Anti-Overflow 0
There are no anti-overflow writes to what. 226

No anti-overflow 257
Anti-overflow 255
There are no anti-overflow writes to what. 1

We take out two print results to see, when the calculated pixel value exceeds 255, then the anti-overflow will become 255, if the calculated pixel value is more than less than 0, then the anti-overflow will become 0, and if there is no anti-overflow, directly into the picture is written into what value.

-30 becomes 226.
257 becomes 1

Can see, OPENCV in order to let the picture can display normally, will put a negative value plus 256, a positive value of more than 256 minus 256, so that the above kind of strange result.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.