OpenCV 2.4.9 Learning Note (3)--OPENCV automatically allocates memory for an output array (matrix)

Source: Internet
Author: User

  Most of the time, OpenCV automatically allocates memory for the output data (parameter of the method) in the OpenCV method, so if a method's arguments have one or more input arrays (Cv::mat instances) and some output arrays, OpenCV automatically allocates memory space for the output array or allocates memory space, and the size and data type of the memory are determined by the input array. This method, if needed, determines the other properties of the output array by other parameters.

Here's a piece of code to illustrate:

1#include"cv.h"2#include"highgui.h"3 4 using namespaceCV;5 6 intMainint,Char**)7 {8Videocapture Cap (0);9     if(!cap.isopened ())Ten     { One         return-1; A     } - Mat frame, edges; -Namedwindow ("edges",1); the      for(;;) -     { -Cap >>frame; - Cvtcolor (frame, edges, cv_bgr2gray); +Gaussianblur (edges, edges, Size (7,7),1.5,1.5); -Canny (edges, edges,0, -,3); +Imshow ("edges", edges); A         if(Waitkey ( -) >=0) at         { -              Break; -         } -     } -     return 0; -}

  >> will automatically allocate memory space for the array frame because the video Analysis Module Videocapture component has been able to obtain the resolution and bit depth information of the USB camera. While the Cvtcolor method automatically allocates memory for the edges array, the data information for the edges array is the same as the input array, and Cv_bgr2gray determines the color space type of the output image. In the above loop, edges's memory is only allocated once, and if the resolution of the input image changes, the memory is reassigned once. The key function of memory allocation is completed in Mat::create, it is responsible for obtaining the information required by the data, if edges size,depth and other data have, and do not need to change the Ceate method will not do anything, unless, The previously allocated memory space is freed (when released, you need to subtract the pointer counter by 1 and compare it with 0), freeing it to reallocate a memory space that fits your needs. Most of the methods in OpenCV call the Create method (if the mat is used), so the output array is allocated space.

There are also some that do not automatically allocate memory for the output array, such as Cv::mixchannels, Cv::rng::fill, etc., so you need to manually allocate memory space in advance.

  

OpenCV 2.4.9 Learning Note (3)--OPENCV automatically allocates memory for an output array (matrix)

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.