在OpenCV中将RGB转换为黑白

时间:2021-12-03 21:18:46

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?

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

6 个解决方案

#1


130  

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

AFAIK,你必须把它转换成灰度值然后把它设为二进制。

1. Read the image as a grayscale image If 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_rgb into 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 binary You can use adaptive thresholding or fixed-level thresholding to 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);

#2


7  

Use cv2 and Python:

使用cv2和Python:

1- Grayscale Image

1 -灰度图像

import cv2
im_gray = cv2.imread('grayscale_image.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)

2- Convert image to Binary

2-将图像转换为二进制

(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

3- Store to Disck

3 - Disck商店

cv2.imwrite('bw_image.png', im_bw)

#3


2  

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);

#4


1  

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 cvBlobsLib library 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库检测打印到微阵列幻灯片上的斑点样本,但是图像必须转换为您提到的颜色->灰度->黑+白,以实现这一点。

#5


1  

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或等于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);

#6


0  

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 GaussianBlur to get a smooth black and white image.

你可以使用GaussianBlur得到一个平滑的黑白图像。

#1


130  

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

AFAIK,你必须把它转换成灰度值然后把它设为二进制。

1. Read the image as a grayscale image If 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_rgb into 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 binary You can use adaptive thresholding or fixed-level thresholding to 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);

#2


7  

Use cv2 and Python:

使用cv2和Python:

1- Grayscale Image

1 -灰度图像

import cv2
im_gray = cv2.imread('grayscale_image.png', cv2.CV_LOAD_IMAGE_GRAYSCALE)

2- Convert image to Binary

2-将图像转换为二进制

(thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

3- Store to Disck

3 - Disck商店

cv2.imwrite('bw_image.png', im_bw)

#3


2  

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);

#4


1  

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 cvBlobsLib library 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库检测打印到微阵列幻灯片上的斑点样本,但是图像必须转换为您提到的颜色->灰度->黑+白,以实现这一点。

#5


1  

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或等于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);

#6


0  

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 GaussianBlur to get a smooth black and white image.

你可以使用GaussianBlur得到一个平滑的黑白图像。