C # EMGUCV Learning notes (i)---image addition and subtraction operation

Source: Internet
Author: User

image addition:

Using the Image.add () method, you can add two pictures together, or have the current image add a color value, and you can use the Cvinvoke.add () method to perform the same action.

The Image.add () method is internally implemented by calling the Cvinvoke.add () method.

Image.add () has 3 implementations, and the return of each implementation is an image object of the same color space and value type:

<summary> The current picture is added to another picture, and the other picture must be of the same type and size (or same ROI size) as the current picture </summary>
2       ///<param name= " Img2 "> Picture added with current picture </param>
3       ///<returns> added results </returns> 4 public       image< TColor, tdepth> Add (Image<tcolor, tdepth> img2)


<summary> The current picture is added to another picture (ret (i) =SRC1 (i) +src2 (i) if mask (i)!=0), and another picture must be of the same type and size (or the same ROI size) as the current picture </ Summary>
2       ///<param name= "Img2" > Another picture </param>
3       ///<param name= "Mask" > Mask picture </param>
4       ///<returns> using mask picture added results </returns> 5 public       Image<tcolor, tdepth > Add (Image<tcolor, tdepth> img2, Image<gray, byte> mask)


<summary> current picture plus a color value </summary>
2       ///<param name= "val" > Color value </param>
3       The result of///<returns> addition <paramref name= "Val"/> from the current image</returns>
4 public       Image<tcolor, Tdepth> Add (TColor val)

Add 4 PictureBox controls to the design interface to display the processed pictures:


Go to code:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using EMGU.CV;
Using Emgu.CV.Structure;
Using Emgu.util; Namespace image Roi {public partial class Form1:form {public Form1 () {Initializecompo
            Nent ();
            IMAGE&LT;BGR, byte> image1 = new IMAGE&LT;BGR, byte> (properties.resources._2);

        IMAGE&LT;BGR, byte> image2 = new IMAGE&LT;BGR, byte> (properties.resources._22); #region///<summary>///Two photos add///</summary>///<para name= "image 1 "> Add the Picture 1</param>///<para name=" Image2 "> Add 2</param>
        s> public static image<bgr,byte> Justadd (Image<bgr,byte>image1,image<bgr,byte>image2) {return IMAEes.
        ADD (Image2);
        #endregion #region///<summary>///uses masks for image manipulation///</summary> <para name= "Image1" > Add a picture 1</param>///<para name= "Image2" > Add a picture 2</param>///&L t;returns></returns> public static image<bgr,byte> Addusingmask (image<bgr,byte> image1,Image& Lt Bgr,byte> image2) {var rect = new Rectangle (new point (0, 0), New Size (Image1. WIDTH/2, Image1.
            Height)); using (var mask = new Image<gray, byte> (Image1. Size)) {mask. Setzero ()//Set all values to 0 mask.
                ROI = rect; Mask. SetValue (255);//Set the value of ROI to 255 mask.
                ROI = rectangle.empty;//Remove roi//if mask (i)!=0,res (i) =img1 (i) +img2 (i); var resimage = Image1.
                ADD (Image2, mask); Mask._not ()//reverse image1. Copy (Resimage, mask); If Mask (I)!= 0, put Image1The value of the copy into the Resimage return resimage; } #endregion private void Form1_Load (object sender, EventArgs e) {IMAGE&L T
            BGR, byte> image1 = new IMAGE&LT;BGR, byte> (properties.resources._2);
            IMAGE&LT;BGR, byte> image2 = new IMAGE&LT;BGR, byte> (properties.resources._22); pictureBox1.Image = Image1.
            Tobitmap (); Picturebox2.image = Image2.
            Tobitmap (); Picturebox3.image = Justadd (Image1, Image2).
            Tobitmap (); Picturebox4.image = Addusingmask (Image1, Image2).

            Tobitmap (); Create a mask picture var mask = new Image<gray, byte> (image2.
            Size); Mask.
            Setzero (); Mask. ROI = new Rectangle (new point (0, 0), New Size (Image2. WIDTH/2, Image2. Height));//roi, is to select the display area mask.
            SetValue (255); Mask. ROI = rectangle.empty;////picturebox3.image = Mask to remove the selected ROI.
        Tobitmap (); }
    }
}

The results appear as follows:


Image subtraction:

With Image.sub (), you can have the current image subtract another image, or the current image subtracts another value. You can also use the Cvinvoke.subtract () method to perform the same operation.

The Image.sub () method is internally implemented by calling the Cvinvoke.subtract () method.

Similar to addition, Image.sub () has 3 implementations, each of which returns an Image object of the same color control and value type.

<summary> current picture minus a picture, the subtracted picture must be the same type and size as the current picture (or the same ROI size) </summary>
2        ///<param name= "Img2" > Minus picture </param>
3        ///<returns> results </returns>
4 public        Image<tcolor, Tdepth> Sub (Image<tcolor, tdepth> img2)
<summary> current picture minus another picture (ret (i) =SRC1 (i)-src2 (i) if mask (i)!=0), the subtracted picture must be of the same type and size (or the same ROI size) as the current picture </summary >
2        ///<param name= "Img2" > Reduced picture </param>
3        ///<param name= "Mask" > Mask picture </ Param>
4        ///<returns> using mask Picture subtraction results </returns> 5 public        Image<tcolor, tdepth> Sub (Image<tcolor, tdepth> img2, Image<gray, byte> mask)
<summary> current picture minus a color value </summary>
2        ///<param name= "val" > subtracted color value </param>
3        ///<returns> minus the result of the color value </returns>
4        


The above method is called in the Form1, and a brighter picture can be obtained in addition, and a darker image is obtained in subtraction.

Code implementation:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Using System.Windows.Forms;
Using EMGU.CV;
Using Emgu.CV.Structure;
Using Emgu.util; Namespace image Subtraction {public partial class Form1:form {public Form1 () {Initializecompon
        ENT (); #region Image Subtraction///<summary>///image subtraction///</summary>///<para name= "I Mage1 "> Subtract pictures 1</param>///<para name=" Image2 "> Subtract pictures 2</param>///<returns></re turns> public static IMAGE&LT;BGR, byte> justsub (IMAGE&LT;BGR, byte> image1, IMAGE&LT;BGR, byte> Image 2) {return image1.
        Sub (Image2); #endregion #region Mask Operation///<summary>///in the case of a mask picture, with Image1-image2///</ summary>>///<param name= "IMage1 "></param>///<param name=" Iamge2 "></param>///<return></return>
            public static image<bgr,byte> Subusingmask (Image<bgr,byte>image1,image<bgr,byte>image2) { var rect = new Rectangle (new point (0, 0), New Size (Image1. WIDTH/2, Image1.
            Height)); using (var mask = new Image<gray, byte> (Image1. Size)) {mask.
                Setzero (); Mask.
                ROI = rect; Mask.
                SetValue (255); Mask.
                ROI = Rectangle.empty; var resimage = Image1.
                Sub (Image2, mask);
                Mask._not (); Image1.
                Copy (Resimage, mask);
            return resimage; } #endregion private void Form1_Load (object sender, EventArgs e) {image&lt ;
            BGR, byte> image1 = new IMAGE&LT;BGR, byte> (properties.resources._2); IMAGE&LT;BGR, byte> image2 = new Image&LT;BGR, byte> (properties.resources._22); pictureBox1.Image = Image1.
            Tobitmap (); Picturebox2.image = Image2.
            Tobitmap (); Picturebox3.image = Justsub (Image1, Image2).

            Tobitmap (); Picturebox4.image = Subusingmask (Image1, Image2).
        Tobitmap (); }
    }

}


Processing results:



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.