OpenCV-如何将vale添加到图像一部分的所有像素

OpenCV - How to add a vale to all the pixels in a part of an image?

本文关键字:一部分 像素 图像 vale 添加 OpenCV-      更新时间:2023-10-16

我正在尝试为灰度图像的矩形部分添加一个常数值。

这就是我尝试过的:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
cv::Mat src = imread("img.png", IMREAD_GRAYSCALE);
Mat temp;
temp = src(Range(10, 150), Range(10, 210));
temp.setTo(50);
imshow("src", src);
cvWaitKey(0);
return 0;
}

但是,这会使该区域全部变黑(Scalar(50)(。我想将50添加到该范围内的所有像素中。在不使用循环的情况下,这可能吗?

只需调用子矩阵的operator+=

temp += 50;

这里列出了矩阵表达式。其中之一是A+s,其中Atemp子矩阵,s是标量-50。