04-29 00:53
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[OpenCV] Bitwise Operation 본문

Programming/OpenCV

[OpenCV] Bitwise Operation

cinema4dr12 2015. 9. 20. 10:03

이번 포스팅에서는 OpenCV의 bitwise operation 함수들에 대하여 알아보도록 하겠습니다.


우선 로딩된 이미지의 white를 "TRUE" 또는 "1"이라 하고, black을 "FALSE" 또는 "0"으로 합니다. 각각의 연산은 다음과 같습니다.

1. AND Operation

두 값 중 하나라도 FALSE이면 결과는 FALSE.


OpenCV 함수: bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())


 A

0

0

1

1

 B

0

1

0

1

 A and B

0

0

0

1

2. OR Operation

두 값 중 하나라도 TRUE이면 결과는 TRUE.


OpenCV 함수: bitwise_or(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())


 A

0

0

1

1

 B

0

1

0

1

 A or B

0

1

1

1

3. XOR Operation

두 값이 같으면 FALSE, 다르면 TRUE.


OpenCV 함수: bitwise_xor(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())


 A

0

0

1

1

 B

0

1

0

1

 A XOR B

1

0

0

1

4. NOT Operation

값이 TRUE이면 결과는 FALSEFALSE이면 결과는 TRUE.


OpenCV 함수: bitwise_not(InputArray src, OutputArray dst, InputArray mask=noArray())


 A

0

1

not A

1

0


  • src1 – first input array or a scalar.
  • src2 – second input array or a scalar.
  • src – single input array.
  • value – scalar value.
  • dst – output array that has the same size and type as the input arrays.
  • mask – optional operation mask, 8-bit single channel array, that specifies elements of the output array to be changed.

Example Code

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
31
32
33
34
35
36
37
38
39
40
#include "opencv2/opencv.hpp"
#include <iostream>
 
using namespace std;
using namespace cv;
 
 
/*/////////////////////////////////////
@ function: main
*//////////////////////////////////////
int main()
{
    cv::Mat image1 = cv::imread( "image_1.jpg", CV_8UC1 );
    cv::Mat image2 = cv::imread( "image_2.jpg", CV_8UC1 );
  
    cv::imshow( "Image 1", image1 );
    cv::imshow( "Image 2", image2 );
  
    cv::Mat res;
 
    // AND operation
    cv::bitwise_and( image1, image2, res );
    cv::imshow( "AND", res );
 
    // OR operation
    cv::bitwise_or( image1, image2, res );
    cv::imshow( "OR", res );
 
    // XOR operation
    cv::bitwise_xor( image1, image2, res );
    cv::imshow( "XOR", res );
 
    // NOT operation
    cv::bitwise_not( image1, res );
    cv::imshow( "NOT", res );
  
    cv::waitKey(0);
 
    return 0;
}
cs



Results


image1


 image2



image1 AND image2



image1 OR image2



image1 XOR image2


 

not image1


Comments