Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better ways to create a rectangular mask by openCV

Tags:

Creating a mask in openCV

      /** result I want           0 0 0 0 0 0 0 0           0 0 0 0 0 0 0 0           0 0 1 1 1 1 0 0           0 0 1 1 1 1 0 0           0 0 1 1 1 1 0 0           0 0 1 1 1 1 0 0           0 0 0 0 0 0 0 0           0 0 0 0 0 0 0 0       */     cv::Mat mask = cv::Mat::zeros(8, 8, CV_8U); std::cout<<"before : \n"<<mask<<std::endl; for(int i = 2; i != 6; ++i) {      auto ptr = mask.ptr<uchar>(i) + 2;      for(int j = 0; j != 4; ++j)      {          *ptr++ = 1;      } } std::cout<<"after : \n"<<mask<<std::endl;    

Do openCV provide us any build in function to create a mask like this? It is trivial to create a function fot this task, but the function of openCV always faster than naive handcrafted codes

like image 745
StereoMatching Avatar asked Aug 08 '13 20:08

StereoMatching


People also ask

What does mask mean in OpenCV?

This allows us to extract regions from images that are of completely arbitrary shape. Put simply; a mask allows us to focus only on the portions of the image that interests us. For example, let's say that we were building a computer vision system to recognize faces.


1 Answers

sure, there's an easier way, use the roi operator:

cv::Mat mask = cv::Mat::zeros(8, 8, CV_8U); // all 0 mask(Rect(2,2,4,4)) = 1; 

done!

like image 105
berak Avatar answered Sep 22 '22 16:09

berak