Rate This Document
Findability
Accuracy
Completeness
Readability

bitwise_and

Usage

Performs a bitwise AND operation on two images (or on an image and a mask) pixel by pixel. It is mainly used for logical operations in image processing, such as extracting a specific region and combining specific parts of an image.

Interface

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

Parameters

Parameter

Description

Value Range

Input/Output

src1

First input image.

Not null

Input

src2

Second input image.

Not null, with a same size as that of src1

Input

dst

Output image.

Mat class

Input

mask

(Optional) Mask for specifying computing regions.

Mat class

Input

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main() {
    // Read two images.
    Mat img1 = imread("image1.jpg");
    Mat img2 = imread("image2.jpg");

    if (img1.empty() || img2.empty()) {
        std::cerr << "Failed to load the image!" << std::endl;
        return -1;
    }

    // Adjust the image size to fit.
    resize(img2, img2, img1.size());

    // Perform the bitwise AND operation.
    Mat dst;
    bitwise_and(img1, img2, dst);

    // Display the result.
    imshow("Image 1", img1);
    imshow("Image 2", img2);
    imshow("Bitwise AND result", dst);
    waitKey(0);

    return 0;
}