0
点赞
收藏
分享

微信扫一扫

OpenCV + CPP 系列(四)图像逻辑运算(加、减、乘、除 | 并集、差集、补集、异或)


文章目录

  • ​​一、图像像素的逻辑运算(加、减、乘、除 )​​
  • ​​二、逻辑运算(并集、差集、补集、异或)​​

一、图像像素的逻辑运算(加、减、乘、除 )

​saturate_cast<uchar>()​​​ 防止像素(数据)溢出:保持数据类型为uchar/uint8
即:像素值的范围(0~255),为负,则转为0,结果超出255,则为255。

​multiply()​​​ 执行效果:类似 saturate_cast(a*b)
​​​addWeighted()​​​ ​​添加玻璃效果​​

void(InputArray src1, double alpha, InputArray src2, double beta, double gamma, OutputArray dst, int dtype=-1);

  • 第一个参数,InputArray类型的src1,表示需要加权的第一个数组,一般是一个Mat;
    第二个参数,double类型的alpha,表示第一个数组的权重
    第三个参数,InputArray类型的src2,表示需要加权的第二个数组,它需要和第一个数组拥有相同的尺寸和通道数;
    第四个参数,double类型的beta,表示第二个数组的权重;
    第五个参数,double类型的gamma,加到权重总和上的标量值(类似于偏置);
    第六个参数,OutputArray类型的dst,输出的数组,它和输入的两个数组有相同的尺寸和通道数;
    第七个参数,int类型的dtype,输出阵列的可选深度,默认值是-1。当两个输入数组有相同的深度时,设置为-1.
  • 下面数学公式表示:用addWeighted函数计算两个数组的加权和:
    dst = src1 * alpha + src2 * beta + gamma;

头文件 ​​quick_opencv.h​​:用于声明类与公共函数

#pragma
#include <opencv2\opencv.hpp>
using namespace cv;

class QuickDemo {
public:
...
void operators_Demo(Mat& image);
void bitwise_Demo(Mat& image);
};

仍然在主函数调用该类的公共成员函数

#include <opencv2\opencv.hpp>
#include <quick_opencv.h>
#include <iostream>
using namespace cv;

int main(int argc, char** argv) {
Mat src = imread("D:\\Desktop\\pandas.jpg");
if (src.empty()) {
printf("Could not load images...\n");
return -1;
}

QuickDemo qk; //实例化
...
qk.operators_Demo(src);
qk.bitwise_Demo(src);
waitKey(0);
destroyAllWindows();
return 0;
}

源文件 ​​quick_demo.cpp​​:实现类与公共函数

#include <quick_opencv.h>



void QuickDemo::operators_Demo(Mat& image) {
Mat dst1 = Mat::zeros(image.size(), image.type());

// 加减除法:被因数可以是相同数据类型的矩阵,也可以是一个标量。
Mat m1 = Mat::zeros(image.size(), image.type());
m1 = Scalar(5, 5, 5);
dst1 = image - m1;
dst1 = dst1 + Scalar(30, 30, 30);
imshow("加法操作", dst1);

Mat dst2 = Mat::zeros(image.size(), image.type());

// 乘法:被因数必须是相同数据类型的矩阵(multiply函数对溢出uchar进行截断)。
Mat m2(image.size(), image.type(),Scalar(2,2,2));
multiply(image, m2, dst2);
imshow("乘法操作", dst2);

// 乘法的具体实现效果
Mat dst3 = Mat::zeros(image.size(), image.type());
int width = image.cols;
int height = image.rows;
int channel = image.channels();
for (int h = 0; h < height; h++) {
for (int w = 0; w < width; w++) {
Vec3b p1 = image.at<Vec3b>(h, w);
Vec3b p2 = m2.at<Vec3b>(h, w);
dst3.at<Vec3b>(h, w)[0] = saturate_cast<uchar>(p1[0] + p2[0]);
dst3.at<Vec3b>(h, w)[1] = saturate_cast<uchar>(p1[1] + p2[1]);
dst3.at<Vec3b>(h, w)[2] = saturate_cast<uchar>(p1[2] + p2[2]);
}
}
imshow("image遍历加法", dst3);

// 使用opencv自带函数,速度肯定更快
Mat m_test(image.size(),image.type(),Scalar(20,20,20));
Mat m_divide(image.size(),image.type(),Scalar(2,2,2));
Mat dst4 = Mat::zeros(image.size(), image.type());
Mat dst5 = Mat::zeros(image.size(), image.type());
Mat dst6 = Mat::zeros(image.size(), image.type());

add(image, m_test, dst4);
subtract(image, m_test, dst5);
divide(image, m_divide, dst6);
imshow("image加法", dst4);
imshow("image减法", dst5);
imshow("image除法", dst6);

}

OpenCV + CPP 系列(四)图像逻辑运算(加、减、乘、除 | 并集、差集、补集、异或)_opencv


关于动态鼠标实时调整亮度与对比度: ​​点击查看​​

二、逻辑运算(并集、差集、补集、异或)

CV_EXPORTS_W void rectangle(InputOutputArray img, Rect rec,
const Scalar& color, int thickness = 1,
int lineType = LINE_8, int shift = 0);

线宽:thickness =-1 表示填充区域


绘制线条类型:lineType = LINE_8 一般绘制,取值为LINE_AA时执行反锯齿绘制,会很慢。

源文件 ​​quick_demo.cpp​​:实现类与公共函数

void QuickDemo::bitwise_Demo(Mat& image) {
Mat m1 = Mat::zeros(Size(256, 256), CV_8UC3);
Mat m2 = Mat::zeros(Size(256, 256), CV_8UC3);

rectangle(m1, Rect(50, 50, 80, 80), Scalar(255, 255, 0), -1, LINE_8, 0);
//rectangle(m2, Rect(100, 100, 80, 80), Scalar(0, 255, 255), -1, LINE_8, 0);
circle(m2, Point(100, 100), 40, Scalar(0, 255, 255), -1, LINE_8, 0);

imshow("m1", m1);
imshow("m2", m2);
Mat dst0, dst1, dst00, dst11, dst3;
bitwise_and(m1.clone(), m2.clone(), dst0);
bitwise_or(m1.clone(), m2.clone(), dst1);

bitwise_not(dst0, dst00);
dst11 = ~dst1;

bitwise_xor(m1.clone(), m2.clone(), dst3);
imshow("and_交集_dst", dst0);
imshow("or_并集_dst", dst1);
imshow("not_交集取反_dst", dst00);
imshow("not_并集取反_dst", dst11);
imshow("xor_异或集_dst", dst3);
}

OpenCV + CPP 系列(四)图像逻辑运算(加、减、乘、除 | 并集、差集、补集、异或)_数组_02


举报

相关推荐

0 条评论