opencv c++ (3): Pixel operation 3

1. Bit operations

Bit operations are the AND or NOT of bits. It is very simple. Just give it to the code.

//像素基本操作
//1. 位操作
#include <iostream>
#include<opencv.hpp>
#include<opencv2\highgui\highgui.hpp>

using namespace std;
using namespace cv;

int main()
{
    
    


	Mat src = imread("src.jpg");

	if (src.empty())
	{
    
    
		printf("open file failed");
		return -1;
	}

	// 1. 取反
	// 相当于255 - 像素, 可以翻看前面的代码
	Mat m1;
	Mat mask = Mat::zeros(src.size(), CV_8UC1);
	//构建mask mask 必须是8bit 单通道
	for (int row = 0; row < src.rows / 2; row++)
	{
    
    
		for (int col = 0; col < src.cols / 2; col++)
		{
    
    
			mask.at<uchar>(row, col) = 100;
		}
	}
	//maks非0区域才进行取反操作,0区域为0
	bitwise_not(src, m1, mask);
	imshow("m1", m1);

	//2. and mask区域为ROI区域
	Mat m2;
	bitwise_and(src, src, m2, mask);
	imshow("m2", m2);

	//3. or 
	Mat m3;
	bitwise_or(src, src, m3, mask);
	imshow("m3", m3);

	//4. xor
	Mat m4;
	bitwise_xor(src, src, m4, mask);
	imshow("m4", m4);
	waitKey();
	destroyAllWindows();
	return 0;
}

There is nothing to say, just look at the code comments and you will understand. The main thing is the understanding of mask. Mask means that only the areas where maks is not 0 are operated, and the area where maks is 0 is black.

Guess you like

Origin blog.csdn.net/m0_59156726/article/details/134188493