前言

使用opencv对图像处理时,可能需要对图像按位操作,而opencv自带位操作运算函数,不必再手写遍历算法,位操作函数包括:
bitwise_and
bitwise_or
bitwise_not
bitwise_xor 异或


mask

关于掩膜mask请点击查看

\newline

像素按位操作函数

1. bitwise_and

src1src2每个像素的像素值按位与,比如某位置对应两个像素值分别为:23185,则输出像素值为17,因为23185的二进制分别为1011110111001,按位与得到1000117

/* 输入 src1,src2,可为灰度图或彩色图,src1 和 src2 大小需一样;
** 输出 dst,尺寸和类型与 src 保持一致;
** 掩膜 mask,可通俗理解为一个遮罩,只对 mask 设定的有效区域进行操作;
*/
void bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask = noArray());

\newline
src1如下图:
src1

\newline

src2如下图:

src2.png

\newline

bitwise_and效果如下图:

and.png

2. bitwise_or

src1src2每个像素的像素值按位或,比如某位置对应两个像素值分别为:23185,则输出像素值为191,因为23185的二进制分别为1011110111001,按位或得到10111111191

/* 输入 src1,src2,可为灰度图或彩色图,src1 和 src2 大小需一样;
** 输出 dst,尺寸和类型与 src 保持一致;
** 掩膜 mask,可通俗理解为一个遮罩,只对 mask 设定的有效区域进行操作;
*/
void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask = noArray());

\newline

bitwise_o效果r如下图:

or.png

3. bitwise_not

src 像素的像素值按位取非,比如某像素值为:23,则输出像素值为232,因为23的二进制为10111,按位取反得到11101000232

/* 输入 src 可为灰度图或彩色图;
** 输出 dst,尺寸和类型与 src 保持一致;
** 掩膜 mask,可通俗理解为一个遮罩,只对 mask 设定的有效区域进行操作;
*/
void bitwise_not(InputArray src, OutputArray dst, InputArray mask = noArray());

\newline

bitwise_not效果如下图:

not.png

4. bitwise_xor

src1src2每个像素的像素值按位异或,比如某位置对应两个像素值分别为:23185,则输出像素值为177,因为23185的二进制分别为1011110111001,按位异或得到10101110174

/* 输入 src1,src2,可为灰度图或彩色图,src1 和 src2 大小需一样;
** 输出 dst,尺寸和类型与 src 保持一致;
** 掩膜 mask,可通俗理解为一个遮罩,只对 mask 设定的有效区域进行操作;
*/
void bitwise_xor(InputArray src1, InputArray src2, OutputArray dst, InputArray mask = noArray());

\newline
bitwise_xor效果如下图:

xor.png

5.带掩膜操作

例如将src1中人头取反,其他保持不变,代码如下:

#include <opencv2/opencv.hpp>

using namespace cv;

int main() {
  Mat src1 = imread("img1.png", IMREAD_GRAYSCALE);
  Mat src2 = imread("img2.png", IMREAD_GRAYSCALE);
  cv::resize(src1, src1, Size(640, 480));
  cv::resize(src2, src2, Size(640, 480));
  imshow("src1", src1);
  imshow("src2", src2);
  // 将mask中包含人头的区域像素值设为255
  Mat mask = Mat::zeros(Size(640, 480), CV_8UC1);
  mask(Rect(320, 50, 260, 310)) = 255;

  Mat dst;
  // 只对人头取反
  bitwise_not(src1, dst, mask);
  imshow("mask1", mask);
  // 将mask反转,得到新的mask
  bitwise_not(mask, mask);
  imshow("mask2", mask);
  // 将src1中人头之外的区域拷贝到dst
  src1.copyTo(dst, mask);
  imshow("dst", dst);
  waitKey();

  return 0;
}

结果如下:

加载中,请稍后

mask1

\newline
加载中,请稍后
mask2

\newline
加载中,请稍后
dst

\newline


总结

主要记录bitwise_andbitwise_orbitwise_notbitwise_xor的作用,帮助理解与使用。

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 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐