代码如下:
#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)

该博客介绍了如何使用OpenCV 2.0库进行图像处理,包括用imread函数以不同模式读取图像,cvtColor函数将图像从一种颜色空间转换到另一种,以及namedWindow和imshow函数用于显示图像。文章还提到了waitKey函数等待按键输入。特别强调了在颜色转换和显示时对图像通道顺序和数值范围的注意,以及窗口大小的设置选项。
最低0.47元/天 解锁文章
6655

被折叠的 条评论
为什么被折叠?



