OpenCV Tutorial 2 - Chapter 3

 Author: Noah Kuntz (2009)
Contact: nk752@drexel.edu

Keywords: OpenCV, computer vision, data type, alpha

My Vision Tutorials Index

This tutorial assumes the reader:
(1) Has a basic knowledge of Visual C++
(2) Has some familiarity with computer vision concepts
(3) Has read the previous tutorials in this series

The rest of the tutorial is presented as follows:

Important Note!(重要的注意事项)

More information on the topics of these tutorials can be found in this book(更多关于这些教程的主题的信息中可以找到这本书):Learning OpenCV: Computer Vision with the OpenCV Library

Step 1: Data Type Concepts(数据类型概念)

Chapter 3 is largely concerned with some key data types in open cv, for matrices and specifically images, and the various basic functions for manipulating them(第三章主要是涉及OpenCV中的一些关键数据,矩阵和具体图象,以及它们各种基本功能的操作). I suggest that you read the chapter to full understand these topics, as I cannot repeat all of the information from the book(我建议你阅读本章充分了解这些主题,因为我不能重复书中的所有信息). I will briefly touch on some of the basics in this section before going on to a couple examples(我将简要地触及本节中的一些基本知识,然后再举两个例子).

Some Basic Data Types(一些基本的数据类型):
CvPoint - point in an image (x,y)
CvSize - size of an image (width, height)
CvRect - portion of an image (x, y, width, height)
CvScaler - RBGA value for a pixel (val[4])
cvMat* - matrix (rows, cols, type) for a 2 dimensional matrix

Creating a Matrix(创建矩阵)
The most basic way to make a matrix is: CvMat* mat = cvCreateMat( 5, 5, CV32FC1 ); (建一个矩阵的最基本方法是:CvMat *mat= cvCreateMat(5,5,CV32FC1);)And it can be accessed with:float element_3_2 = CV_MAT_ELEM( *mat, float, 3, 2 );(它可以通过float element_3_2 = CV_MAT_ELEM(*mat,float,3,2)被访问: There are better more elegant ways to access matrix values, in particular via pointer incrementation(更好的来访问特定的矩阵值方式是通过指针递增). This code would sum all the elements in a three-channel matrix(此代码将总和三通道矩阵的所有元素):

 

float sum( const CvMat* mat ){

        float s = 0.0f;

        for(int row=0; rowrows; row++ ){

               const float* ptr = (const float*)(mat->data.ptr + row * mat->step);

               for( col=0; colcols; col++){

                       s += *ptr++;

               }

        }

}

 

The other important thing to note from this chapter is the IplImage Data Structure(这一章另外需要重要讲解的内容是IplImage的数据结构:):

 

typedef struct _IplImage {

        int                    nSize;

        int                    ID;

        int                    nChannels;

        int                    alphaChannel;

        int                    depth;

        char                   colorModel[4];

        char                   channelSeq[4];

        int                    dataOrder;

        int                    origin;

        int                    align;

        int                    width;

        int                    height;

        struct _IplROI*        roi;

        struct _IplImage*      maskROI;

        void*                  imageId;

        struct _IplTileInfo*   tileInfo;

        int                    imageSize;

        char*                  imageData;

        int                    widthStep;

        int                    BorderMode[4];

        int                    BorderConst[3];

        char*                  imageDataOrigin;

} IplImage;

 

Some important factors here are obviously the width and height, then depth in terms of colors, IPL_DEPTH_8U being the most common, and nChannels for whether the image is grayscale (1), RGB (3), or RGBA (4)(这里明显的一些重要因素是宽度和高度,然后是颜色的深度,IPL_DEPTH_8U是最常见的,和n个通道,它们判断图像是否是灰度(1),RGB(3),或RGBA(4)).

Step 2: Alpha Blend with                                                                     


Part of an image alpha blended with OpenCV


One simple operation to perform on an image is an alpha blend(一个简单的操作,执行图像是一个alpha混合). To perform this on just part of an image can be achieved by setting a region of interestcvSetImageROI and then performing a blend with cvAddWeighted(要执行一个图像的一部分,只是这可以通过设置一个cvSetImageROI区域,然后执行一个融合cvAddWeighted).Check the chapter for many other matrix operations(检查很多其他的矩阵操作的篇章).


 

int _tmain(int argc, _TCHAR* argv[])

{

        IplImage* src1 = cvLoadImage( "MGC.jpg" );

        IplImage* src2 = cvLoadImage( "wheel.jpg" );

        int x = 280;

        int y = 80;

        int width = 60;

        int height = 60;

        double alpha = 0.5;

        double beta = 0.5;

        cvSetImageROI(src1, cvRect(x,y,width,height));

        cvAddWeighted(src1, alpha, src2, beta, 0.0, src1);

        cvResetImageROI(src1);

        cvNamedWindow("Alpha_blend", 1);

        cvShowImage("Alpha_blend", src1);

        cvWaitKey();

}

 


Step 3: Drawing and Text(绘画和文字)


Lines and text drawn on an image(线条和文字图像绘制)


Another basic image manipulation is adding lines, shapes, and text(另一个基本的图像处理是添加线条,形状和文本。). In this example a line is drawn, a circle is drawn, and text is added to the image(在这个例子中绘制一条线,绘制一个圆,和添加文本到图像。). Points must be created withcvPoint to do any of these actions, and scalers must be created to represent colors, by usingCV_RGB(r,g,b)(点必须用cvPoint创建做这些操作,创建倍线器必须用CV_RGB(r,g,b)创建来代表颜色). We are able to draw a line withcvLine(src1,pt1,pt2,color,thickness,connectivity), and a circle with cvCircle(src1,pt2,radius,blue,thickness,connectivity)(我们可以用cvLine(src1,pt1,pt2,color,thickness,connectivity)绘制一条线,并用cvCircle(src1,pt2,radius,blue,thickness,connectivity)绘制一个圆).Text is a little more complex, first a font must be initialized withCvFontfont1;cvInitFont(&font1,CV_FONT_HERSHEY_DUPLEX,hscale,vscale,shear,thickness,line_type) (文本要稍微复杂一点,首先字体必须用CvFontfont1;cvInitFont(&font1,CV_FONT_HERSHEY_DUPLEX,hscale,vscale,shear,thickness,line_type)进行初始化), and then the text can be created withcvPutText(src1,text,pt1,&font1,blue)(然后这个文本可以用cvPutText(src1,text,pt1,&font1,blue)来创建). Here is the code(以下是代码):


 

int _tmain(int argc, _TCHAR* argv[])

{

        IplImage* src1 = cvLoadImage( "MGC.jpg" );

       

        // Line variables

        CvPoint pt1 = cvPoint(250,60);

        CvPoint pt2 = cvPoint(405,195);

        CvScalar red = CV_RGB(250,0,0);

        int thickness = 2;

        int connectivity = 8;

 

        // Circle variables

        int radius = 30;

        CvScalar blue = CV_RGB(0,0,250);

 

        // Text variables

        const char* text = "testing";

        double hscale = 1.0;

        double vscale = 0.8;

        double shear = 0.2;

        int thickness2 = 1;

        int line_type = 8;

 

        CvFont font1;

        cvInitFont(&font1,CV_FONT_HERSHEY_DUPLEX,hscale,vscale,shear,thickness,line_type);

 

        cvLine(src1,pt1,pt2,red,thickness,connectivity);

        cvCircle(src1,pt2,radius,blue,thickness,connectivity);

        cvPutText(src1,text,pt1,&font1,blue);

       

        cvNamedWindow("Drawing_and_Text", 1);

        cvShowImage("Drawing_and_Text", src1);

        cvWaitKey();

 

        return 0;

}

 


 

Final Words(结束语)

This tutorial's objective was to show how to do some additional image manipulation(本教程的目标是展示如何做一些额外的图像处理). The book should be followed to learn more about the new data types introduced(该书应遵循以了解更多新的数据类型介绍).

Click here to email me.
Click here to return to my Tutorials page.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值