OpenCV:mask的作用,如何制作掩模mask
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv
免费下载资源
·
前言
OpenCV中的很多函数用到mask,mask是什么?怎么作一个mask?将是本文的主要内容。
一、mask是什么?
mask 不是马斯克,是掩膜,可以用来遮盖非感兴趣区,突出感兴趣区,使得图像处理只专注于ROI部分。
二、OpenCV生成mask的几种方法
注意:mask最终需要与要作用到的输入图像的尺寸与类型保持一致
矩形
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("img1.png", IMREAD_GRAYSCALE);
imshow("src", src);
// 定义mask,大小640*480,像素全0
Mat mask = cv::Mat::zeros(Size(640, 480), CV_8UC1);
// 作一个从坐标(220,120),宽200,高200的矩形框,框内填充白色,从方法1,2,3中任选一
// 方法1
rectangle(mask, cv::Rect(220, 120, 200, 200), Scalar(255), -1);
// 方法2
mask(cv::Rect(320, 50, 240, 310)) = 255;
// 方法3
mask(cv::Rect(320, 50, 240, 310)).setTo(255);
Mat dst;
// 将src中对应对应掩膜ROI中区域拷贝到dst
src.copyTo(dst, mask);
imshow("mask",mask);
imshow("dst", dst);
waitKey();
return 0;
}
运行结果如下:
圆形
同理
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat src = imread("img1.png", IMREAD_GRAYSCALE);
// 定义mask,大小640*480,像素全0
Mat mask = cv::Mat::zeros(Size(640, 480), CV_8UC1);
// 作一个以点坐标(320,50)为圆心,150为半径的圆,圆内填充白色
circle(mask, Point(440, 205), 150, Scalar(255),-1);
Mat dst;
// 将src中对应对应掩膜ROI中区域拷贝到dst
src.copyTo(dst, mask);
imshow("mask",mask);
imshow("dst", dst);
waitKey();
return 0;
}
mask可以是各种形状,各式各样,方法也有很多,这里只略举一二。
总结
定义mask,设置ROI,将ROI内部填充白色,其他填充黑色,即可对ROI区域操作,遮盖其他区域。也可以将mask反转,遮盖矩形框内数据,处理框外数据。
GitHub 加速计划 / opencv31 / opencv
142
15
下载
OpenCV: 开源计算机视觉库
最近提交(Master分支:3 个月前 )
d9a139f9
Animated WebP Support #25608
related issues #24855 #22569
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
1 天前
09030615
V4l default image size #25500
Added ability to set default image width and height for V4L capture. This is required for cameras that does not support 640x480 resolution because otherwise V4L capture cannot be opened and failed with "Pixel format of incoming image is unsupported by OpenCV" and then with "can't open camera by index" message. Because of the videoio architecture it is not possible to insert actions between CvCaptureCAM_V4L::CvCaptureCAM_V4L and CvCaptureCAM_V4L::open so the only way I found is to use environment variables to preselect the resolution.
Related bug report is [#25499](https://github.com/opencv/opencv/issues/25499)
Maybe (but not confirmed) this is also related to [#24551](https://github.com/opencv/opencv/issues/24551)
This fix was made and verified in my local environment: capture board AVMATRIX VC42, Ubuntu 20, NVidia Jetson Orin.
### Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
- [X] I agree to contribute to the project under Apache 2 License.
- [X] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [X] The PR is proposed to the proper branch
- [X] There is a reference to the original bug report and related work
- [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
1 天前
更多推荐
已为社区贡献3条内容
所有评论(0)