简体   繁体   中英

How to make an image black and white in opencv c++?

Actually I'm a complete beginner in opencv and in native C++ concepts so please excuse me if my question is so simple. Basically I want to convert an image to Black&White by using native C++ for that I tried to find tutorials but I couldn't find any. Thus can anyone please help me to learn this concept ?

cv::Mat colorMat, grayscaleMat, binaryMat;

First convert to grayscale:

cv::cvtColor(colorMat, grayscaleMat, CV_BGR2GRAY);

Then apply a binary threshold to the grayscale image:

cv::threshold(grayscaleMat, binaryMat, 100, 255, cv::THRESH_BINARY);

Also, you may want to consider using the OpenCV4Android SDK .

You should use the cvtColor function. Lets say 'image' is your original photo, and 'gray_image' is the variable where you will store your new gray photo:

cvtColor( image, gray_image, CV_BGR2GRAY );

('image' & 'gray_image' are 'Mat' type)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM