关闭

Load, Modify, and Save an Image

558人阅读 评论(0) 收藏 举报
分类:

In this tutorial you will learn how to:

  • Load an image using imread
  • Transform an image from BGR to Grayscale format by using cvtColor
  • Save your transformed image in a file on disk (using imwrite)




#include <cv.h>
#include <highgui.h>

using namespace cv;

int main( int argc, char** argv )
{
 char* imageName = argv[1];

 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, CV_BGR2GRAY );

 imwrite( "../../images/Gray_Image.jpg", gray_image );

 namedWindow( imageName, CV_WINDOW_AUTOSIZE );
 namedWindow( "Gray image", CV_WINDOW_AUTOSIZE );

 imshow( imageName, image );
 imshow( "Gray image", gray_image );

 waitKey(0);

 return 0;
}



  1. Now we are going to convert our image from BGR to Grayscale format. OpenCV has a really nice function to do this kind of transformations:

    cvtColor( image, gray_image, CV_BGR2GRAY );
    

    As you can see, cvtColor takes as arguments:

    • a source image (image)
    • a destination image (gray_image), in which we will save the converted image.
    • an additional parameter that indicates what kind of transformation will be performed. In this case we use CV_BGR2GRAY (because of imread has BGR default channel order in case of color images).





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1017301次
    • 积分:11054
    • 等级:
    • 排名:第1437名
    • 原创:59篇
    • 转载:608篇
    • 译文:36篇
    • 评论:224条
    最新评论