opencv-2.4.9入门练习02

这篇博客介绍了OpenCV中imread函数的使用,包括不同参数设置对图像读取的影响,如颜色类型转换、深度处理等。还展示了如何结合多个图像,创建一个包含百度Logo和美女照片的组合显示窗口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

以一个简单的实例作为起步,如下代码所示。

#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>

using namespace cv; 

int main()
{
    Mat lena = imread("lena.jpg");
    namedWindow("lean");
    imshow("lena", lena);

    waitKey();
    return 0;
}


(1) imread,Loads an image from a file.

 imread(const string &filename, int flags=1

  • filename – Name of file to be loaded.
  • flags –Flags specifying the color type of a loaded image:
    • CV_LOAD_IMAGE_ANYDEPTH - If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.若载入的图像的深度为16位或者32位,就返回对应深度的图像,否则,就转换为8位图像再返回
    • CV_LOAD_IMAGE_COLOR - If set, always convert image to the color one 彩色单通道
    • CV_LOAD_IMAGE_GRAYSCALE - If set, always convert image to the grayscale one 灰度
    • >0 Return a 3-channel color image.(彩色)
    • =0 Return a grayscale image.(灰度)
    • <0 Return the loaded image as is (with alpha channel).返回包括appha通道的图像,默认不加载

(2)namedWindow,creates a window
void namedWindow(const string &name, int flags = WINDOW_AUTOSIZE)
name  – Name of the window in the window caption that may be used as a window identifier.
flags –FLAG of the window
WINDOW_NORMAL  If this is set, the user can resize the window (no constraint).用户可以重新定义窗口的大小
WINDOW_AUTOSIZE If this is set, the window size is automatically adjusted to fit the displayed image,and you cannot change the window size manually.,可原始图片大小一致,不可更改
WINDOW_OPENGL  If this is set, the window will be created with OpenGL support. 窗口创建支持openGL

(3)imshow,Displays an image in the specified window.展现图片
void imshow(const string& winname, InputArray mat)
winname  – Name of the window.window的mingc
mat-需要展现的图像
typedef const _InputArray & InputArray ;
 
The function  imshow  displays an image in the specified window. If the window was created with the  CV_WINDOW_AUTOSIZE  flag, the image is shown with its original size. Otherwise, the image is scaled to fit the window. The function may scale the image, depending on its depth:imshow在特定的窗口展现图片,如果窗口使用CV_WINDOW_AUTOSIZE的模式创建,则这张图片以原始尺寸展现。否则,图片进行缩放以满足窗口大小。
  • If the image is 8-bit unsigned, it is displayed as is.如果图片是8比特位,展现原始图片
  • If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].如果图片是16比特或者32比特,像素值需要除以256,将其归一到[0,255]的范围
  • If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].如果图片为32位浮点型,则将像素值乘以255,将其归一到[0,255]的范围。

If window was created with OpenGL support, imshow also support ogl::Buffer , ogl::Texture2D and gpu::GpuMat as input.

如果窗口创建时支持OPENGL。则图片展现也需要支持 ogl::Buffer , ogl::Texture2D and gpu::GpuMat作为输入。


一个实现照片组合的代码段。

  1 /*
  2  *测试展现和拼接图片
  3  by eli
  4  20141021
  5  * */
  6 
  7 #include<opencv2/core/core.hpp>
  8 #include<opencv2/highgui/highgui.hpp>
  9 
 10 using namespace cv;
 11 
 12 int main()
 13 {
 14     
 15     //test add
 16     Mat image = imread("lena02.jpg",199);
 17     Mat logo = imread("baidulogo.jpg");
 18     
 19     namedWindow("source");
 20     //imshow("source", image);
 21     
 22     namedWindow("logo");
 23     //imshow("logo", logo);
 24     Mat imageSource ;;
 25     imageSource= image(Rect(80,35,logo.cols,logo.rows));
 26     
 27     addWeighted(imageSource,0.5,logo,0.3,0.,imageSource);
 28     imshow("source+logo", image);
 29     waitKey();
 30     return 0;
 31 } 


输出的窗口为百度logo和美女照片的组合




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值