C++ 在 OpenCV 中将 RGB 转换为黑白

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1585535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:32:02  来源:igfitidea点击:

Convert RGB to Black & White in OpenCV

c++cimage-processingopencvcomputer-vision

提问by mohammed

I would like to know how to convert an RGB image into a black & white (binary) image.

我想知道如何将 RGB 图像转换为黑白(二进制)图像。

After conversion, how can I save the modified image to disk?

转换后,如何将修改后的图像保存到磁盘?

回答by Jacob

AFAIK, you have to convert it to grayscale and then threshold it to binary.

AFAIK,您必须将其转换为灰度,然后将其阈值转换为二进制。

1. Read the image as a grayscale imageIf you're reading the RGB image from disk, then you can directly read it as a grayscale image, like this:

1. 将图像读取为灰度图像如果您是从磁盘读取 RGB 图像,那么您可以直接将其读取为灰度图像,如下所示:

// C
IplImage* im_gray = cvLoadImage("image.jpg",CV_LOAD_IMAGE_GRAYSCALE);

// C++ (OpenCV 2.0)
Mat im_gray = imread("image.jpg",CV_LOAD_IMAGE_GRAYSCALE);

2. Convert an RGB image im_rgbinto a grayscale image: Otherwise, you'll have to convert the previously obtained RGB image into a grayscale image

2. 将RGB图像im_rgb转换为灰度图像:否则,您必须将之前获得的RGB图像转换为灰度图像

// C
IplImage *im_rgb  = cvLoadImage("image.jpg");
IplImage *im_gray = cvCreateImage(cvGetSize(im_rgb),IPL_DEPTH_8U,1);
cvCvtColor(im_rgb,im_gray,CV_RGB2GRAY);

// C++
Mat im_rgb  = imread("image.jpg");
Mat im_gray;
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);

3. Convert to binaryYou can use adaptive thresholdingor fixed-level thresholdingto convert your grayscale image to a binary image.

3. 转换为二进制您可以使用自适应阈值固定级别阈值将灰度图像转换为二进制图像。

E.g. in C you can do the following (you can also do the same in C++ with Mat and the corresponding functions):

例如,在 C 中,您可以执行以下操作(您也可以在 C++ 中使用 Mat 和相应的函数执行相同的操作):

// C
IplImage* im_bw = cvCreateImage(cvGetSize(im_gray),IPL_DEPTH_8U,1);
cvThreshold(im_gray, im_bw, 128, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

// C++
Mat img_bw = im_gray > 128;

In the above example, 128 is the threshold.

在上面的例子中,128 是阈值。

4. Save to disk

4.保存到磁盘

// C
cvSaveImage("image_bw.jpg",img_bw);

// C++
imwrite("image_bw.jpg", img_bw);

回答by nikithashr

This seemed to have worked for me!

这似乎对我有用!

Mat a_image = imread(argv[1]);

cvtColor(a_image, a_image, CV_BGR2GRAY);
GaussianBlur(a_image, a_image, Size(7,7), 1.5, 1.5);
threshold(a_image, a_image, 100, 255, CV_THRESH_BINARY);

回答by MD. Nazmul Kibria

Simple binary threshold method is sufficient.

简单的二元阈值方法就足够了。

include

包括

#include <string>
#include "opencv/highgui.h"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

int main()
{
    Mat img = imread("./img.jpg",0);//loading gray scale image
    threshold(img, img, 128, 255, CV_THRESH_BINARY);//threshold binary, you can change threshold 128 to your convenient threshold
    imwrite("./black-white.jpg",img);
    return 0;
}

You can use GaussianBlurto get a smooth black and white image.

您可以使用GaussianBlur来获得平滑的黑白图像。

回答by AndyUK

I do something similar in one of my blog postings. A simple C++ example is shown.

我在我的一篇博客文章中做了类似的事情。显示了一个简单的 C++ 示例。

The aim was to use the open source cvBlobsLiblibrary for the detection of spot samples printed to microarray slides, but the images have to be converted from colour -> grayscale -> black + white as you mentioned, in order to achieve this.

目的是使用开源cvBlobsLib库来检测打印到微阵列载玻片上的点样本,但图像必须如您提到的那样从彩色 -> 灰度 -> 黑色 + 白色转换,以实现这一点。

回答by alvaropgl

A simple way of "binarize" an image is to compare to a threshold: For example you can compare all elements in a matrix against a value with opencv in c++

“二值化”图像的一种简单方法是与阈值进行比较:例如,您可以使用 c++ 中的 opencv 将矩阵中的所有元素与值进行比较

cv::Mat img = cv::imread("image.jpg", CV_LOAD_IMAGE_GRAYSCALE); 
cv::Mat bw = img > 128;

In this way, all pixels in the matrix greater than 128 now are white, and these less than 128 or equals will be black

这样,矩阵中大于128的所有像素现在都是白色的,小于128或等于的都是黑色的

Optionally, and for me gave good results is to apply blur

可选,对我来说,好的结果是应用模糊

cv::blur( bw, bw, cv::Size(3,3) );

Later you can save it as said before with:

稍后您可以像之前所说的那样保存它:

cv::imwrite("image_bw.jpg", bw);