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
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.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With