C++ 使用 OpenCV 将 RGB 图像转换为二进制图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16995712/
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:48:48  来源:igfitidea点击:

RGB image into binary image using OpenCV

c++opencv

提问by U23r

So, I'm super noob and I'm trying to do something that convert an RGB image to binary image in C/C++, using OpenCV. My first program was this:

所以,我是超级菜鸟,我正在尝试使用 OpenCV 将 RGB 图像转换为 C/C++ 中的二进制图像。我的第一个程序是这样的:

#include <opencv.hpp>

using namespace cv;

int main ( int argc, char **argv )
{
Mat im_gray = imread("img1.png",CV_LOAD_IMAGE_GRAYSCALE);

Mat im_rgb  = imread("img.png");
Mat im_gray;
cvtColor(im_rgb,im_gray,CV_RGB2GRAY);

Mat img_bw = im_gray > 128;

imwrite("image_bw.jpg", img_bw);

return 0;
}

But it says "There were build errors" and it opens just the original image. How can I do it right? Can anybody help me with this?

但它说“有构建错误”,它只打开原始图像。我怎样才能做对?有人可以帮我解决这个问题吗?

The output was:

输出是:

'opencv.exe': Loaded 'D:\Imagens\pibiti\opencv\Debug\opencv.exe', Symbols loaded.
'opencv.exe': Loaded 'C:\Windows\System32\ntdll.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\kernel32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\KernelBase.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'D:\Imagens\pibiti\opencv\opencv\opencv_core230d.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'D:\Imagens\pibiti\opencv\opencv\opencv_highgui230d.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\msvcp100d.dll', Symbols loaded.
'opencv.exe': Loaded 'C:\Windows\System32\msvcr100d.dll', Symbols loaded.
'opencv.exe': Loaded 'C:\Windows\System32\user32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\gdi32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\ole32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\oleaut32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\advapi32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\WinSxS\x86_microsoft.windows.common-controls_6595b64144ccf1df_5.82.9200.16384_none_bf100cd445f4d954\comctl32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\avifil32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\msvfw32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\avicap32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\combase.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\msvcrt.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\rpcrt4.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\sechost.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\winmm.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\msacm32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\shell32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\version.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\winmmbase.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\shlwapi.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\imm32.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\msctf.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\cryptbase.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\bcryptprimitives.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Windows\System32\uxtheme.dll', Cannot find or open the PDB file
'opencv.exe': Loaded 'C:\Program Files\Iminent\Iminent.WinCore.dll', Binary was not built with debug information.
'opencv.exe': Loaded 'C:\Windows\System32\dwmapi.dll', Cannot find or open the PDB file
The thread 'Win32 Thread' (0xd9c) has exited with code -1073741510 (0xc000013a).
WinCore .dll DLL_PROCESS_DETACH:The program '[4136] opencv.exe: Native' has exited with code -1073741510 (0xc000013a).

PS.: Using Windows 8 + Visual Studio 10 + OpenCV 2.3.

PS.:使用 Windows 8 + Visual Studio 10 + OpenCV 2.3。

The image I'm trying to convert to binary image: img1.png

我试图转换为二进制图像的图像:img1.png

回答by Zaw Lin

remove extra Mat im_gray;and include path is set to OpenCV2.3/build/includeand the program compiles.

remove extraMat im_gray;和 include path 设置为OpenCV2.3/build/include并且程序编译。

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace cv;

int main ( int argc, char **argv )
{
    Mat im_gray = imread("img1.png",CV_LOAD_IMAGE_GRAYSCALE);

    Mat im_rgb  = imread("img.png");
    cvtColor(im_rgb,im_gray,CV_RGB2GRAY);

    Mat img_bw = im_gray > 128;

    imwrite("image_bw.jpg", img_bw);

    return 0;
}    

edit:enter image description here

编辑:在此处输入图片说明

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;

int main( int argc, char** argv )
{
    Mat img = imread( "C:/test/img1.png");
    cvtColor(img,img,CV_BGR2GRAY);
    Mat left=img(Rect(0,0,300,151));
    Mat right=img(Rect(300,0,img.cols-300,151));
    threshold(left,left,0,255,CV_THRESH_OTSU);
    threshold(right,right,0,255,CV_THRESH_OTSU);
    imshow("img",img);
    waitKey(0);
    return 0;
}    

回答by Bull

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

//using namespace cv;

int main ( int argc, char **argv )
{
    cv::Mat im_gray;   // no nead to load the Mat with anything when declaring it.
    cv::Mat im_rgb  = cv::imread("img.png");
    cv::cvtColor(im_rgb, im_gray,CV_RGB2GRAY);
    // INSTEAD of the above two lines you could have cv::Mat im_gray = imread("img1.png",CV_LOAD_IMAGE_GRAYSCALE);

    // the following is an alternative to Mat img_bw = im_gray > 128
    cv::Mat img_bw;
    cv::threshold(im_gray, img_bw, 128.0, 255.0, THRESH_BINARY);

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

    return 0;
}