Arithmetic operation and logical operation of image in OpenCV

Source: Internet
Author: User
Tags scalar

Arithmetic operation and logical operation of image in OpenCV

In the image processing, there are two most important basic operations are image point operation and block operation, the simple point is that the image point operation is the image of each pixel point of the relevant logic and geometric operations, block operations most common is based on various operations of the convolution operator, the realization of a variety of different functions. I'm going to go with you today. The functions and applications of Image point operation in OpenCV are studied. Common arithmetic operations include addition, subtraction, multiplication, and addition, and logical operations include with, or, non-, XOR, or. Preparatory work:
Select two images that are consistent in size and display as follows when loaded successfully:


The result of the addition operation is as follows:

The subtraction operation results are as follows:

The multiplication operation results are as follows:

The result of the division operation is as follows:

The result of the weight addition operation is as follows:

XOR and non-operational results are as follows:

The code is as follows:

Mat Src1, Src2, DST; Src1 = Imread ("D:/vcprojects/images/test1.png"); SRC2 = Imread ("D:/vcprojects/images/moon.png");Const Char* Input_title1 ="Input image-1";Const Char* Input_title2 ="Input image-2";    Namedwindow (Input_title1, cv_window_autosize);    Namedwindow (Input_title2, cv_window_autosize);    Imshow (Input_title1, SRC1); Imshow (Input_title2, SRC2);//Create result windows and background image    Const Char* Output_title ="Result image";    Namedwindow (Output_title, cv_window_autosize);    Mat bgimg = Mat (Src1.size (), Src1.type ());    Mat whiteimg = Mat (Src1.size (), Src1.type ()); Whiteimg = Scalar (255,255,255);//Temporary imagesMat Skel (Src1.size (), CV_8UC1, Scalar (0));    Mat Temp (Src1.size (), CV_8UC1); Mat element = Getstructuringelement (Morph_cross, Size (3,3), point (-1, -1));BOOLDone =false;intindex =9C while(true) {Switch(index) { Case 1://GarcoAdd (Src1, Src2, DST, Mat (),-1); Imshow (Output_title, DST); Break; Case 2://minus operationSubtract (SRC1, Src2, DST, Mat (),-1); Imshow (Output_title, DST); Break; Case 3://Multiply OperationBgimg = Scalar (2,2,2); Multiply (Src1, bgimg, DST,1.0, -1); Imshow (Output_title, DST); Break; Case 4://except OperationBgimg = Scalar (2,2,2); Divide (Src1, bgimg, DST,1.0, -1); Imshow (Output_title, DST); Break; Case 5://Based on weight addition-adjust brightnessAddweighted (SRC1,1.5, SRC2,0.5,0, DST,-1); Imshow (Output_title, DST); Break; Case 6://Logical non-Bitwise_not (Src1, DST, Mat ()); Imshow (Output_title, DST); Break; Case 7: Subtract (whiteimg, Src1, DST, Mat (),-1); Imshow (Output_title, DST); Break; Case 8://Logical XOR orBgimg = Scalar (255,255,255);            Bitwise_xor (Src1, bgimg, DST, Mat ()); Imshow (Output_title, DST); Break;default: Imshow (Output_title, SRC2); Break; } C = Waitkey ( -);if((Char) c = = -) { Break; }if(C >0) {index = c%9; }    }

In addition, we can realize the skeleton extraction of two value image based on the operation of logic and morphology, the demo result is as follows:

The code is implemented as follows:

    //Extract Skeleton    //Turn grayscale with two valueCvtcolor (Src1, Src1, Color_bgr2gray); Threshold (SRC1, DST,127,255, cv_thresh_binary);//bitwise_not (SRC1, SRC1);     Do{//Open operation-Ensure that small interfering blocks are removedMorphologyex (Src1, temp, morph_open, Element);//Take back actionBitwise_not (temp, temp);//Get different from source imageBitwise_and (SRC1, temp, temp);//Use it to extract the skeleton and get it to be just one pixel smaller than the source imageBitwise_or (Skel, temp, Skel);//each cycle of corrosion, the frame is obtained by means of continuous corrosionErode (Src1, src1, Element);//To find the maximum value of the image after corrosion, if it is completely corroded        //Only the background black is left, skeleton has been obtained, exit loop        Double Max; Minmaxloc (SRC1,0, &Max); Done = (0==Max); } while(!done);//Display skeletonImshow (Output_title, Skel);

Summary:
The above code demonstrates that simple image arithmetic operations can also play a large role, based on the black background image and the original weight overlay can achieve image brightness adjustment, based on multiplication can achieve contrast adjustment. The skeleton extraction of two-value image can be realized based on logic operation and corrosion operation.

Arithmetic operation and logical operation of image in OpenCV

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.