etutorialspoint
  • Home
  • PHP
  • MySQL
  • MongoDB
  • HTML
  • Javascript
  • Node.js
  • Express.js
  • Python
  • Jquery
  • R
  • Kotlin
  • DS
  • Blogs
  • Theory of Computation

OpenCV Logical Operators- Bitwise AND, OR, NOR, XOR

In the previous article, you will learn about the different arithmetic operations on images using Python OpenCV. In this article, you will learn to perform logical operations on images, like - bitwise AND, OR, NOR and XOR using Python OpenCV.

The concept of logical operators is very simple. We hope, you have definitely performed logical operations on numbers that returns some Boolean value. But, in the case of images, the logical operators are applied in a pixel-by-pixel way, i.e. the value of a pixel in the output image depends only on the values of the corresponding pixels in the input images. Hence, the input images must be of the same size and type.

OpenCV Bitwise AND

Python OpenCV provides cv2.bitwise_and() method to perform bitwise AND logical operation. It combines corresponding pixels of two image buffers by a bitwise AND operation. It commonly used for detecting differences in the input images. It returns an image highlighting the target regions with a binary mask.

Suppose, we have the following two images -

OpenCV Logical Operator

In the given example, we have applied the cv2.bitwise_and() method to perform bitwise AND operation on the above images-

import cv2

img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_and = cv2.bitwise_and(img2, img1)

cv2.imshow("bit_and", bitwise_and)

cv2.waitKey(0)
cv2.destroyAllWindows()

The above code returns the following output -

OpenCV Logical AND Operator



OpenCV Bitwise OR

Python OpenCV provides cv2.bitwise_or() method to perform bitwise OR logical operation. It combines corresponding pixels of two image buffers by a bitwise OR operation. The bitwise OR operator are useful for processing binary-valued images (0 or 1) to detect objects which have moved between frames. Here is the example -

import cv2

img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_or = cv2.bitwise_or(img2, img1)

cv2.imshow("bitwise_or", bitwise_or)

cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV Logical OR Operator

OpenCV Bitwise XOR

Python OpenCV provides cv2.bitwise_xor() method to perform bitwise XOR logical operation. It combines corresponding pixels of two image buffers by a bitwise XOR operation. It is also used for processing binary-valued images (0 or 1) to detect objects which have moved between frames.

import cv2

img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_xor = cv2.bitwise_xor(img2, img1)

cv2.imshow("bitwise_xor", bitwise_xor)

cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV Logical XOR Operator

OpenCV Bitwise NOT

Bitwise NOT or complement is a unary operation that performs logical negation on each bit. It is forming the ones' complement of the given binary value. It means the bits that are 1 become 0, and those that are 0 become 1. Python OpenCV provides cv2.bitwise_not() method to perform a bitwise NOT operation on each pixel of the input image. It inverts the image representation.

import cv2

img2 = cv2.imread("nature.jpg")
bitwise_not = cv2.bitwise_not(img2)

cv2.imshow("bitwise_not", bitwise_not)

cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV Logical NOT Operator

Similarly, when we perform bitwise NOT on the second image, we will get the following -

import cv2

img1 = cv2.imread("ocean.jpg")
img2 = cv2.imread("nature.jpg")
bitwise_not = cv2.bitwise_not(img1)

cv2.imshow("bitwise_not", bitwise_not)

cv2.waitKey(0)
cv2.destroyAllWindows()
OpenCV Logical NOT Operator



Related Articles

How to capture a video in Python OpenCV and save
Python OpenCV Overlaying or Blending Two Images
Contour Detection using Python OpenCV
Harris Corner Detection using Python OpenCV
Human Body Detection Program In Python OpenCV
Face Recognition OpenCV Source Code
Canny Edge Detector OpenCV Python
Python NumPy: Overview and Examples
Image processing using Python Pillow
Python OpenCV Histogram Equalization
Python OpenCV Histogram of Color Image
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Python OpenCV ColorMap
Python OpenCV Gaussian Blur Filtering
Python OpenCV Overview and Examples




Most Popular Development Resources
Retrieve Data From Database Without Page refresh Using AJAX, PHP and Javascript
-----------------
Characteristics of a Good Computer Program
-----------------
How to get data from XML file in PHP
-----------------
PHP code to send email using SMTP
-----------------
PHP Create Word Document from HTML
-----------------
Hypertext Transfer Protocol Overview
-----------------
PHP MySQL PDO Database Connection and CRUD Operations
-----------------
Create Dynamic Pie Chart using Google API, PHP and MySQL
-----------------
How to encrypt password in PHP
-----------------
Splitting MySQL Results Into Two Columns Using PHP
-----------------
How to get current directory, filename and code line number in PHP
-----------------
Dynamically Add/Delete HTML Table Rows Using Javascript
-----------------
Get current visitor\'s location using HTML5 Geolocation API and PHP
-----------------
How to Sort Table Data in PHP and MySQL
-----------------
PHP MYSQL Advanced Search Feature
-----------------
Simple star rating system using PHP, jQuery and Ajax
-----------------
Simple pagination in PHP with MySQL
-----------------
Fibonacci Series Program in PHP
-----------------
How to add multiple custom markers on google map
-----------------
jQuery loop over JSON result after AJAX Success
-----------------
PHP user registration and login/ logout with secure password encryption
-----------------
Submit a form data using PHP, AJAX and Javascript
-----------------
Recover forgot password using PHP7 and MySQLi
-----------------
Php file based authentication
-----------------
PHP Secure User Registration with Login/logout
-----------------
jQuery File upload progress bar with file size validation
-----------------
Polling system using PHP, Ajax and MySql
-----------------
Simple PHP File Cache
-----------------
SQL Injection Prevention Techniques
-----------------
How to generate QR Code in PHP
-----------------
Simple File Upload Script in PHP
-----------------
PHP User Authentication by IP Address
-----------------
Calculate the distance between two locations using PHP
-----------------
Preventing Cross Site Request Forgeries(CSRF) in PHP
-----------------
To check whether a year is a leap year or not in php
-----------------
CSS Simple Menu Navigation Bar
-----------------
Detect Mobile Devices in PHP
-----------------
PHP Server Side Form Validation
-----------------
Simple way to send SMTP mail using Node.js
-----------------
Set and Get Cookies in PHP
-----------------
Date Timestamp Formats in PHP
-----------------
Get Visitor\'s location and TimeZone
-----------------
Convert MySQL to JSON using PHP
-----------------
Simple Show Hide Menu Navigation
-----------------
PHP Programming Error Types
-----------------
PHP Sending HTML form data to an Email
-----------------
Driving route directions from source to destination using HTML5 and Javascript
-----------------
How to print specific part of a web page in javascript
-----------------
Google Street View API Example
-----------------
How to select/deselect all checkboxes using Javascript
-----------------
How to add google map on your website and display address on click marker
-----------------
PHP Getting Document of Remote Address
-----------------
PHP Connection and File Handling on FTP Server
-----------------
File Upload Validation in PHP
-----------------
R Plot Types
-----------------


Most Popular Blogs
Most in demand programming languages
Best mvc PHP frameworks in 2019
MariaDB vs MySQL
Most in demand NoSQL databases for 2019
Best AI Startups In India
Kotlin : Android App Development Choice
Kotlin vs Java which one is better
Top Android App Development Languages in 2019
Web Robots
Data Science Recruitment of Freshers - 2019


Interview Questions Answers
Basic PHP Interview
Advanced PHP Interview
MySQL Interview
Javascript Interview
HTML Interview
CSS Interview
Programming C Interview
Programming C++ Interview
Java Interview
Computer Networking Interview
NodeJS Interview
ExpressJS Interview
R Interview


Popular Tutorials
PHP Tutorial (Basic & Advance)
MySQL Tutorial & Exercise
MongoDB Tutorial
Python Tutorial & Exercise
Kotlin Tutorial & Exercise
R Programming Tutorial
HTML Tutorial
jQuery Tutorial
NodeJS Tutorial
ExpressJS Tutorial
Theory of Computation Tutorial
Data Structure Tutorial
Javascript Tutorial




General Knowledge

listen
listen
listen
listen
listen
listen
listen
listen
listen


Learn Popular Language

listen
listen
listen
listen
listen

Blogs

  • Jan 3

    Stateful vs Stateless

    A Stateful application recalls explicit subtleties of a client like profile, inclinations, and client activities...

  • Dec 29

    Best programming language to learn in 2021

    In this article, we have mentioned the analyzed results of the best programming language for 2021...

  • Dec 20

    How is Python best for mobile app development?

    Python has a set of useful Libraries and Packages that minimize the use of code...

  • July 18

    Learn all about Emoji

    In this article, we have mentioned all about emojis. It's invention, world emoji day, emojicode programming language and much more...

  • Jan 10

    Data Science Recruitment of Freshers

    In this article, we have mentioned about the recruitment of data science. Data Science is a buzz for every technician...

Follow us

  • etutorialspoint facebook
  • etutorialspoint twitter
  • etutorialspoint linkedin
etutorialspoint youtube
About Us      Contact Us


  • eTutorialsPoint©Copyright 2016-2021. All Rights Reserved.