图像的打开、修改、显示和保存示例(>OpenCV 2.0)

代码如下:

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


2 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值