[Ubuntu + opencv] Have you been occupied by mask ?? In opencv, for example, cvcopy (* SRC, * DST, * mask), what is the role of mask?

Source: Internet
Author: User

 

 

In fact, opencv
Many functions contain a mask.
Many people do not know how to use the parameter, as if they ignore it in actual use, opencv designers can ignore it by default ).

Here, I will introduce you to a detailed analysis of the mask in a common function cvcopy.
Hope you can give us some guidance.


Cvcopy
This function is very familiar. You can use it a lot. Haha ......

The following content is a PDF file in the installation folder of opencv.


Copies one array to another.

// Copy an array to another Array


Void cvcopy (const cvarr * SRC, cvarr * DST, const cvarr * mask = NULL );


SRC
The source array

// Source array. Who should I copy ?? From whom?

// Insert an aside. The array mentioned in opencv is not an array in the general sense. It is a structure such as matrix and image ......


DST
The destination Array

// Target array. Who will the copied content be ?? To whom?

Mask

-- The following is the focus. The monks think that many people do not fully understand the role of this mask (do not take my shoes or try again ......) --

Operation mask, 8-bit single channel array; specifies elements of the destination array
Be changed

// Mask operation. The mask is an 8-bit single-channel array. The mask specifies the elements in the target array (DST) that can be changed.

 

The above sentence is not very important. The focus is on the formula below. How many people understand this formula? According to the information on the Internet, few people understand it, or the experts are writing online, so we can only wait for the truth ...... Child, have you been occupied by mask?
(The first mask is the mask array in opencv, and the second mask is the meaning of this English word. If you don't understand it, Google will go )?? The truth is coming !!!

 

The function copies selected elements from an input array to an output array:
DST (I) = SRC (I) If mask (I) = 0.

// This function selects the elements in the input array (SRC array) (which can be considered as marked, but who makes these marks ?? Yes, it's mask. You're so smart, kid) copy ....................................................................................

........................................................................ Where? Come on !! Copy to the DST array ......

 

DST (I) = SRC (I) If mask (I )! = 0.

That is to say, if the mask is not null, that is, the mask is an array, and it is an array of the same size as DST or SRC.


Traverse every element of SRC,

(1) In position I, if the value corresponding to the mask is not 0



(I)



Copy the value to DST



(I)



.


(2) If mask (I) is 0, DST (I) is set to 0 no matter what SRC (I) is.



For example, it is enough to create a one-dimensional model without being too complicated.


 

 

If any of the passed arrays is of iplimage type, then its ROI and COI fields are used. Both
Arrays must have the same type, the same number of dimensions, and the same size. The function
Can also copy sparse arrays (mask is not supported in this case ).

// If the array passed to SRC is of the image type, the ROI or CoI will be used. The SRC array and DST array must have the same data type, the same array dimension, and the same size. This function can also be used to copy a sparse matrix (but in this case, the mask does not work ).

 

 

Okay, now we have finished writing. Do you understand why the mask name is used? The Mask means that an operation can be performed when it is not 0 (the specific operation depends on the specific function) 0 to cover up the operation failures.

 

Add one point
: Mask = NULL means there is no template, no mask operation is used, so what should the function do ......

 

Come on, let's look at a flesh-and-blood example (this article replies on opencv)
:

# Include "CV. H "<br/> # include" cxcore. H "<br/> # include" highgui. H "<br/> # include <stdio. h> <br/> int main () <br/> {<br/> float arr1 [] = {1, 2, 4, 5, 6 ,}; <br/> float arr2 [] = {-12,154,786, 0.154,-119}; <br/> float arr3 [6] = }; <br/> int mask_arr [6] = {1}; <br/> cvmat src1; <br/> cvinitmatheader (& src1, 2, 3, cv_32fc1, arr1, cv_autostep ); <br/> cvmat src2; <br/> cvinitmatheader (& src2, 2,3, cv_32fc1, arr2, cv_autostep); <br/> cvmat DST; <br/> cvinitmatheader (& DST, 2,3, cv_32fc1, arr3, cv_autostep); <br/> cvmat * mask = cvcreatemat (2, 3, cv_32fc1 ); <br/> cvinitmatheader (mask, 2,3, cv_8uc1, mask_arr, cv_autostep); <br/> cvadd (& src1, & src2, & DST, mask ); <br/> int X; <br/> int y; <br/> for (y = 0; y <2; ++ y) <br/> {<br/> float * PTR = (float *) (DST. data. PTR + y * DST. step); <br/> for (x = 0; x <3; X ++) <br/> {<br/> printf ("/T % F ", PTR [x]); <br/>}< br/> printf ("/N"); <br/>}< br/> printf ("/N "); <br/> return 0; <br/>}

The result is as follows:
-3.0000000.0000000.000000
0.0000000.0000000.000000

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.