代码如下:
#include <opencv2/opencv.hpp>
using namespace cv;
int main( int argc, char** argv )
{
char* imageName = argv[1];
char* outName = argv[2];
Mat image;
image = imread( imageName, 1 );//打开
if( argc != 2 || !image.data )
{
printf( " No image data \n " );
return -1;
}
Mat gray_image;
cvtColor( image, gray_image, COLOR_BGR2GRAY );//修改
imwrite( outName, gray_image );//保存
namedWindow( imageName, WINDOW_AUTOSIZE );
namedWindow( "Gray image", WINDOW_AUTOSIZE );
imshow( imageName, image );//显示
imshow( "Gray image", gray_image );//显示
waitKey(0);
return 0;
}
1 cv::imread 读取
Mat cv::imread (const String & filename, int flags = IMREAD_COLOR )
- IMREAD_UNCHANGED (<0) 按照图像原有的模式打开 (包含alpha通道)
- IMREAD_GRAYSCALE ( 0) 以灰度模式打开图像
- IMREAD_COLOR (>0) 打开图像为 RGB通道模式
The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix (Mat::data==NULL ). Currently, the following file formats are supported:
- Windows bitmaps - *.bmp, *.dib (always supported)
- JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
- JPEG 2000 files - *.jp2 (see the Notes section)
- Portable Network Graphics - *.png (see the Notes section)
- WebP - *.webp (see the Notes section)
- Portable image format - *.pbm, *.pgm, *.ppm (always supported)
- Sun rasters - *.sr, *.ras (always supported)
- TIFF files - *.tiff, *.tif (see the Notes section)