源码

#include <iostream>
#include "cv.h"
#include "highgui.h"
using namespace std;
int main( int argc, char** argv )
{
//load color image specified by first argument
IplImage *source = cvLoadImage( argv[1]);
//create new image structure 
//for the grayscale output image
IplImage *destination = cvCreateImage( 
cvSize( source->width, source->height ), IPL_DEPTH_8U, 1 );

//set type CV_RGB2GRAY to convert 
//RGB image to grayscale 
cvCvtColor( source, destination, CV_RGB2GRAY );

//save grayscale image to a file specified by 
//second argument
cvSaveImage( argv[2], destination );
return 0;
}

编译

g++ `pkg-config opencv --cflags --libs` convert_grayscale.cpp -o convert_grayscale

用法

./convert_grayscale test.jpg gray_test.jpg
OpenCV如何将颜色转换为灰度

下面是一个将彩色图像转换成灰度级的C++程序。

日期:2020-07-07 20:54:48 来源:oir作者:oir