OpenCV image processing

OpenCV image processing

Introduction of some functions of opencv image operation

Special note: the following functions, the data type, size and number of channels of the pictures participating in the operation must be the same before the operation can be performed.

Arithmetic operation

1: Add function: add()

CV_EXPORTS_W void add(InputArray src1, InputArray src2, OutputArray dst,
                      InputArray mask = noArray(), int dtype = -1);

/*****************************************************************************************************************************************/

Parameter 1: first input diagram

Parameter 2: second input diagram

Parameter 3: output diagram

Parameter 4: mask pixels (optional)

Parameter 5: depth of output map (optional)

/*****************************************************************************************************************************************/

2: Subtraction: subtract();

CV_EXPORTS_W void subtract(InputArray src1, InputArray src2, OutputArray dst,
                           InputArray mask = noArray(), int dtype = -1);

/*****************************************************************************************************************************************/

Parameter 1: first input diagram

Parameter 2: second input diagram

Parameter 3: output diagram

Parameter 4: mask pixels (optional)

Parameter 5: depth of output map (optional)

/*****************************************************************************************************************************************/

3: Multiplication: multiply();

CV_EXPORTS_W void multiply(InputArray src1, InputArray src2,
                           OutputArray dst, double scale = 1, int dtype = -1);

/*****************************************************************************************************************************************/

Parameter 1: first input diagram

Parameter 2: second input diagram

Parameter 3: output diagram

Parameter 4: scaling factor, multiplied by the scaling factor based on the first figure * the second figure

Parameter 5: depth of output map (optional)

/*****************************************************************************************************************************************/

4: Division operation: divide();

CV_EXPORTS_W void divide(InputArray src1, InputArray src2, OutputArray dst,
                         double scale = 1, int dtype = -1);

/*****************************************************************************************************************************************/

Parameter 1: first input diagram

Parameter 2: second input diagram

Parameter 3: output diagram

Parameter 4: scaling factor, multiplied by the scaling factor based on the first figure * the second figure

Parameter 5: depth of output map (optional)

/*****************************************************************************************************************************************/

5: Mixed operation: addWeighted();

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

/*****************************************************************************************************************************************/

According to the formula: dst=alpha* img1+beta* img2+gamma;

Parameter 1: first input diagram

Parameter 2: input the proportion of Figure 1 (alpha)

Parameter 3: second input diagram

Parameter 4: input figure 2 proportion (beta)

Parameter 5: added value (gamma)

Parameter 6: output diagram

Parameter 7: depth of output map (optional)

/*****************************************************************************************************************************************/

Bit operation

1: Bitwise AND: bitwise_and();

CV_EXPORTS_W void bitwise_and(InputArray src1, InputArray src2,
                              OutputArray dst, InputArray mask = noArray());

/*****************************************************************************************************************************************/

Parameter 1: first input diagram

Parameter 2: second input diagram

Parameter 3: output diagram

Parameter 4: mask pixel, optional

/*****************************************************************************************************************************************/

2: Bitwise OR: bitwise_or();

CV_EXPORTS_W void bitwise_or(InputArray src1, InputArray src2,
                             OutputArray dst, InputArray mask = noArray());

/*****************************************************************************************************************************************/

Parameter 1: first input diagram

Parameter 2: second input diagram

Parameter 3: output diagram

Parameter 4: mask pixel, optional

/*****************************************************************************************************************************************/

3: Non operation: bitwise_not();

CV_EXPORTS_W void bitwise_not(InputArray src, OutputArray dst,
                              InputArray mask = noArray());

/*****************************************************************************************************************************************/

Parameter 1: input diagram

Parameter 3: output diagram

Parameter 4: mask pixel, optional

/*****************************************************************************************************************************************/

4: Exclusive or operation: bitwise_xor();

V_EXPORTS_W void bitwise_xor(InputArray src1, InputArray src2,
                              OutputArray dst, InputArray mask = noArray());

/*****************************************************************************************************************************************/

Parameter 1: first input diagram

Parameter 2: second input diagram

Parameter 3: output diagram

Parameter 4: mask pixel, optional

/*****************************************************************************************************************************************/

The arithmetic operation code is as follows:

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class ImgOpreations
{
public:
	//Over 255, keep the original color
	ImgOpreations():img1(imread("1.bmp")),img2(imread("2.bmp")) 
	{
		result = Mat::zeros(img1.size(),img2.type());//Initialize and store the results of two picture operations
	}
	void Add(string WName="Add")
	{
		add(img1,img2,result);//Add
		imshow(WName,result);
		waitKey(0);
	}
	void Sub(string WName="Sub")
	{
		subtract(img1,img2,result);//subtract 
		imshow(WName,result);
		waitKey(0);
	}
	void Mul(string WName = "Mul") 
	{
		multiply(img1,img2,result,1);//Multiply
		imshow(WName,result);
		waitKey(0);
	}
	void Div(string WName = "Div")
	{
		divide(img1,img2,result,1);//be divided by
		imshow(WName, result);
		waitKey(0);
	}
	void AddWeighted(string WName="AddW")
	{
		addWeighted(img1,0.5,img2,0.5,0,result);//Mixed operation
		imshow(WName,result);
		waitKey(0);
	}
private:
protected:
	Mat img1;
	Mat img2;
	Mat result;
};
int main() 
{
	ImgOpreations* test1 = new ImgOpreations;
	test1->Add();
	test1->Sub();
	test1->Mul();
	test1->Div();
	test1->AddWeighted();
    delete test1;
    return 0;
}

The bit operation code is as follows:

#include<iostream>
#include<opencv2/opencv.hpp>
using namespace std;
using namespace cv;
class BitWise 
{
public:
	BitWise():img1(imread("1.bmp")),img2(imread("2.bmp"))
	{
		result = Mat::zeros(img1.size(),img2.type());
	}
	void And(string WName="And")//Bitwise AND
	{
		bitwise_and(img1,img2,result);
		imshow(WName,result);
		waitKey(0);
	}
	void Or(string WName = "Or")//Bitwise OR
	{
		bitwise_or(img1, img2, result);
		imshow(WName, result);
		waitKey(0);
	}
	void Not(string WName = "Not")//Non operation
	{
		bitwise_not(img1,result);
		imshow(WName+"1", result);
		waitKey(0);
		bitwise_not(img2, result);
		imshow(WName+"2", result);
		waitKey(0);
	}
	void Xor(string WName = "Xor")//Exclusive or operation
	{
		bitwise_xor(img1, img2, result);
		imshow(WName, result);
		waitKey(0);
	}
private:
	Mat img1;
	Mat img2;
	Mat result;
protected:
};
int main() 
{
	BitWise* test2 = new BitWise;
	test2->And();
	test2->Not();
	test2->Or();
	test2->Xor();
	delete test2;
    return 0;
}

Tags: Python OpenCV Computer Vision

Posted by callmubashar on Sat, 13 Aug 2022 21:31:19 +0530