- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to invert a binary image in OpenCV using C++?
Inverting a binary image means inverting the pixel values. From a visual perspective, when we invert a binary image, white pixels will be converted to black, and black pixels will be converted to white.
The basic form of this function is −
cvtColor(original_image, grayscale_image, COLOR_BGR2GRAY);
The next line is converting the grayscale image into a binary image and storing the converted image into 'binary_image' matrix.
threshold(grayscale_image, binary_image, 100, 255, THRESH_BINARY);
Here 'grayscale_image' is the source matrix, 'binary_image' is the destination matrix. After that, there are two values 100 and 255. These two values represent the threshold range. In this line, the threshold range represents the grayscale pixel values to be converted.
bitwise_not(source matrix, destination matrix);
The bitwise_not() function inverse the source matrix's pixel values and store them in the destination matrix. The source matrix is 'binary_image', and the destination matrix is 'inverted_binary_image'.
The following program performs the binary image inversion −
Example
#include<iostream> #include<opencv2/highgui/highgui.hpp> #include<opencv2/imgproc/imgproc.hpp> using namespace cv; using namespace std; int main(int argc, char** argv) { Mat original_image;//declaring a matrix to load the original image// Mat grayscale_image;//declaring a matrix to store converted image// Mat binary_image;//declaring a matrix to store the binary image Mat inverted_binary_image;//declaring a matrix to store inverted binary image namedWindow("Binary Image");//declaring window to show binary image// namedWindow("Inverted Binary Image");//declaring window to show inverted binary image// original_image = imread("mountain.jpg");//loading image into matrix// cvtColor(original_image, grayscale_image,COLOR_BGR2GRAY);//Converting BGR to Grayscale image and storing it into 'converted' matrix// threshold(grayscale_image, binary_image, 100, 255, THRESH_BINARY);//converting grayscale image stored in 'converted' matrix into binary image// bitwise_not(binary_image, inverted_binary_image);//inverting the binary image and storing it in inverted_binary_image matrix// imshow("Binary Image", binary_image);//showing binary image// imshow("Inverted Binary Image", inverted_binary_image);//showing inverted binary image// waitKey(0); return 0; }
Output
- Related Articles
- How to create a binary image in OpenCV using C++?
- OpenCV Python – How to convert a colored image to a binary image?
- How to invert a binary search tree using recursion in C#?
- Performing binary thresholding on an image using OpenCV
- Performing inverse binary thresholding on an image using OpenCV
- How to Invert the color of an image using JavaFX?
- Program to invert a binary tree in Python
- How to write an image to a file using OpenCV?
- How to create a black image and a white image using OpenCV Python?
- How to convert a negative image to positive image using Java OpenCV library?
- How to convert a colored image to Sepia image using Java OpenCV library?
- Invert Binary Tree in Python
- How to save an Image in OpenCV using C++?
- How to rotate an image in OpenCV using C++?
- How to resize an image in OpenCV using Python?
