open cv+C++错误及经验总结(三)

1.We will iterate through them via pointers so the total number of elements depends from this number.

我们用指针在每一个通道上迭代,因此通道数就决定了需计算的元素总数。

原话:We create an output image with the same size and the same type as our input. As you can see in theHow the image matrix is stored in the memory? section, depending on the number of channels we may have one or more subcolumns. We will iterate through them via pointers so the total number of elements depends from this number.

2.We’ll use the plainC [] operator to access pixels.

利用C语言的[]操作符,我们能简单明了地访问像素。

3.Then simply access the right items with the [] operator.

有了这些指针后,我们使用[]操作符,就能轻松访问到目标元素。

4.For moving the output pointer ahead we simply increase this (with one byte) after each operation:

为了让输出指针向前移动,我们在每一次操作之后对输出指针进行了递增(移动一个字节):

5.On the borders of the image the upper notation results inexistent pixel locations (like minus one - minus one).

在图像的边界上,上面给出的公式会访问不存在的像素位置(比如(0,-1))。formula方式,准则,客套话

results  英 [rɪ'zʌlts]  美 [rɪ'zʌlts]

n.  

v.   

notation
英 [nəʊ'teɪʃn]

美 [noˈteʃən] n.记号,标记法



6.In these points our formula is undefined.

因此我们的公式对边界点来说是未定义的。 specifying v.详述,使具有特性

7.Then call the filter2D function specifying the input, the output image and the kernell to use:

然后调用 filter2D 函数,参数包括输入、输出图像以及用到的核。 verbose冗长的,啰嗦的

8.分析open cv 中imread函数--读入图像函数

imread的函数原型是:Mat imread(const string& filename, int flags = 1);

Mat是open cv里的一个数据结构,在这里我们定义一个Mat类型的变量img,用于保存读入的图像,在文本开始有写到,我们用imread函数来读取图像,第一个字段标识图像的文件名(包括扩展名),第二个字段用于指定读入图像的颜色和深度,它的取值可以有以下几种:

1)CV_LOAD_IMAGE_UNCHANGED(< 0),以原始图像读取(包括alpha通道) 

2) CV_LOAD_IMAGE_GRAYSCALE ( 0),以灰度图像读取

3) CV_LOAD_IMAGE_COLOR (>0),以RGB格式读取

9 open cv 中测量运行时间的函数

函数功能:GetTickCount返回(retrieve)从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD。

函数原型DWORD GetTickCount(void);

最开始的C接口中的是 cvGetTickCount()和cvGetTickFrequency(),在程序段的开始和结束时两次使用cvGetTickCount(),然后将两次的差除以cvGetTickFrequency()后就可以获得程序段的以微妙us为单位的运行时间,不是很精确但是一般足够了。

到了2.x之后在命名空间cv中又多了几个函数,getTickCount(),getTickFrequency(),getCPUTickCount(),此时仍然可以使用上面的C接口的函数,也可以使用这几个namespace cv中的函数,两次使用getTickCount(),然后再除以getTickFrequency(),不过需要注意的是这时计算出来的是以秒s为单位的时间,这个是与上面的带cv前缀的函数的不同,而要使用更精确的计时,就需要使用getCPUTickCount(),不过现代计算机CPU的频率会随着负载而变化所以没大有必要使用该函数,可以参看函数的介绍【Also, since a modern CPU varies the CPU frequency depending on the load, the number of CPU clocks spent in some code cannot be directly converted to time units. Therefore,getTickCount is generally a preferable solution for measuring execution time.】也就是使用getTickCount就足够了。

10.changing the contrase and brightness of an image

// CCv.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include"cv.h"
#include"highgui.h"
using namespace cv;
using namespace std;

double alpha; /**<simple contrast control */
int beta; /**<simple brightness control */
int _tmain(int argc, _TCHAR* argv[])
{	
	Mat I,J;
	I = imread(argv[1], CV_LOAD_IMAGE_UNCHANGED);
	if (!I.data) return 0;
	Mat new_image = Mat::zeros(I.size(), I.type());
	//Initialize values
	int dim = I.channels();
        alpha =  2.2; /**<simple contrast control */
        beta = 50;
	//Do the operation new_image(i,j) = alpha * image(i,j) + beta
	/*for (int y = 0; y < I.rows; y++)
	{
		for (int x = 0; x < I.cols; x++)
		{
			for (int c = 0; c < dim; c++)
			{
				new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>(alpha * (I.at<Vec3b>(y,x)[c]) + beta);
			}
		}
	}*/
	I.convertTo(new_image,-1,alpha,beta);
	namedWindow("origin", 1);
	namedWindow("transform",1);

	imshow("origin",I);
	imshow("transform", new_image);

	cvWaitKey(0);

	return 0;
}

 

注:convertTo函数的作用等同于三个for循环的作用,他们输出的图像是完全一致的。
加强Mat函数,saturate_cast的运用。

11、Basic drawing 基本绘图

It represents a 2D points,specified by its image coordinates x and y.We can define it as:

此数据结构表示了由其图像坐标xy 指定的2D点。可定义为:

Point pt;

pt.x = 10;

pt.y = 8;

or

Point pt = Point(10,8);
 We can define it as:It represents a 2D point, specified by its image coordinatesx andy. We can define it as:Scalar

Represents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel values.

表示了具有4个元素的数组。此类型在OpenCV中被大量用于传递像素值

tuple英[tʌpl]美 [tʌpl] n.

  • 如何用  Point在图像中定义 2D 点
  • 如何以及为何使用  Scalar
  • 用OpenCV的函数 line直线
  • 用OpenCV的函数 ellipse椭圆
  • 用OpenCV的函数 rectangle矩形
  • 用OpenCV的函数 circle
  • 用OpenCV的函数 fillPoly填充的多边形
  • void ellipse(Mat &img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar&color, int thickness=1, int lineType=8, int shift=0)
    Parameters:
    • img – Image.
    • center – Center of the ellipse.
    • axes – Length of the ellipse axes.
    • angleEllipse rotation angle in degrees.
    • startAngle – Starting angle of the elliptic arc in degrees.
    • endAngle – Ending angle of the elliptic arc in degrees.
    • boxAlternative ellipse representation via RotatedRect or CvBox2D. This means that the function draws an ellipse inscribed in the rotated rectangle.
    • colorEllipse color.
    • thickness – Thickness of the ellipse arc outline, if positive. Otherwise, this indicates that a filled ellipse sector is to be drawn.
    • lineType – Type of the ellipse boundary. See the line()  description.
    • shift – Number of fractional bits in the coordinates of the center and values of axes.

    alternative  英 [ɔ:lˈtɜ:nətɪv] 美 [ɔlˈtɚnətɪv, æl-]

    adj. 

    n.   

    inscribed  英 [ɪn'skraɪbd]  


    美 [ɪn'skraɪbd]
           v. 



    elliptic  英 [ɪ'lɪptɪk] 美 [ɪ'lɪptɪk]

    adj.  

    fractional  英 [ˈfrækʃənl] 美 [ˈfrækʃənəl]

    adj.   

    coordinates  英 [kə'ʊɔ:dənəts] 美 [kə'ʊɔ:dənəts]

    n.    

    v.   


    C++: void fillPoly (Mat & img, const Point** pts, const int* npts, int   ncontours, const Scalar& color, int   lineType=8, int shift=0, Point offset=Point() )

     

    Parameters:
    • img – Image.
    • pts – Array of polygons where each polygon is represented as an array of points.
    • npts – Array of polygon vertex counters.
    • ncontours – Number of contours that bind the filled region.
    • color – Polygon color.
    • lineType – Type of the polygon boundaries. See the  line()  description.
    • shift – Number of fractional bits in the vertex coordinates.

    The function fillPoly fills an area bounded by several polygonal contours. The function can fill complex areas, for example, areas with holes, contours with self-intersections (some of thier parts), and so forth.

    polygonal  英 [pə'lɪɡənl] 美 [pə'lɪɡənl]

    adj. 多角形的,多边形的
    contours  [ˈkɔnˌtʊəz]
    n.  

    self-intersection  英 ['selfɪntəs'ekʃn] 美 ['selfɪntəs'ekʃn]

    自相交

    最后贴上测试程序:

    // CCv.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    #include"cv.h"
    #include"highgui.h"
    using namespace cv;
    using namespace std;
    
    void MyEllipse(Mat img, double angle);
    void MyLine( Mat img, Point start, Point end );
    void MyFilledCircle( Mat img, Point center );
    void MyPolygon( Mat img );
    
    	static int w = 100;
    int _tmain(int argc, _TCHAR* argv[])
    {	
    
    	//Windows names
    	//char atom_window[] = "Drawing 1: Atom";
    	//char rook_window[] = "Drawing 2: Rook";
    	
    	///Create black empty images 创建空全黑像素的空图像
    	Mat atom_image = Mat::zeros(w,w, CV_8UC3);
    	Mat rook_image = Mat::zeros(w,w,CV_8UC3);
    	///1.Draw a simple atom:画一个简单的原子。
    	///1.Creating ellipses
    	MyEllipse(atom_image,90);
    	MyEllipse(atom_image,0);
    	MyEllipse(atom_image,45);
    	MyEllipse(atom_image,-45);
    
    	MyFilledCircle(atom_image, Point(w/3.0, w/2.0));
    	///2.Draw a rook画一个赌棍
    	///2.a.Create a convex polygon创建一个凸多边形
    	MyPolygon(rook_image);
    	///2.b.Creating rectangles
    	rectangle(rook_image, Point(0, 7*w/8.0), Point(w,w), Scalar(0,255,255), -1, 8);
    	///2.c.Create a few lines
    	MyLine(rook_image, Point(0, 15 *w/16), Point(w, 15*w/16));
    	MyLine(rook_image, Point(w/4, 7 *w/8), Point(w/4, w));
    	MyLine(rook_image, Point(0, 7 *w/8), Point(w/2, w));
    	MyLine(rook_image, Point(3*w/4, 7 *w/8), Point(w*3/4, w));
    
    	rectangle(rook_image, Point(0,7*w/8.0), Point(w,w), Scalar(0, 255,255), -1, 8);
    
    	namedWindow("atom", 1);
    	namedWindow("rook", 1);
    
    	imshow("atom", atom_image);
    	imshow("rook", rook_image);
    
    	cvWaitKey(0);
    	return 0;
    }
    
    void MyEllipse(Mat img, double angle)
    {
    	int thickness = 2;
    	int lineType = 8;
    
    	ellipse(img, Point(w/3.0, w/2.0), Size(w/4.0, w/16.0), angle, 0, 360, 
    		Scalar(0,255,0), thickness, lineType);
    }
    void MyLine( Mat img, Point start, Point end )
    {
    	int thickness = 2;
    	int lineType = 8;
    	line( img,
    		start,
    		end,
    		Scalar( 0, 0, 0 ),
    		thickness,
    		lineType );
    }
    void MyFilledCircle( Mat img, Point center )
    {
    	int thickness = -1;
    	int lineType = 8;
    
    	circle( img,
    		center,
    		w/32.0,
    		Scalar( 0, 0, 255 ),
    		thickness,
    		lineType );
    }
    void MyPolygon( Mat img )
    {
    	int lineType = 8;
    
    	/** Create some points */
    	Point rook_points[1][20];
    	rook_points[0][0] = Point( w/4.0, 7*w/8.0 );
    	rook_points[0][1] = Point( 3*w/4.0, 7*w/8.0 );
    	rook_points[0][2] = Point( 3*w/4.0, 13*w/16.0 );
    	rook_points[0][3] = Point( 11*w/16.0, 13*w/16.0 );
    	rook_points[0][4] = Point( 19*w/32.0, 3*w/8.0 );
    	rook_points[0][5] = Point( 3*w/4.0, 3*w/8.0 );
    	rook_points[0][6] = Point( 3*w/4.0, w/8.0 );
    	rook_points[0][7] = Point( 26*w/40.0, w/8.0 );
    	rook_points[0][8] = Point( 26*w/40.0, w/4.0 );
    	rook_points[0][9] = Point( 22*w/40.0, w/4.0 );
    	rook_points[0][10] = Point( 22*w/40.0, w/8.0 );
    	rook_points[0][11] = Point( 18*w/40.0, w/8.0 );
    	rook_points[0][12] = Point( 18*w/40.0, w/4.0 );
    	rook_points[0][13] = Point( 14*w/40.0, w/4.0 );
    	rook_points[0][14] = Point( 14*w/40.0, w/8.0 );
    	rook_points[0][15] = Point( w/4.0, w/8.0 );
    	rook_points[0][16] = Point( w/4.0, 3*w/8.0 );
    	rook_points[0][17] = Point( 13*w/32.0, 3*w/8.0 );
    	rook_points[0][18] = Point( 5*w/16.0, 13*w/16.0 );
    	rook_points[0][19] = Point( w/4.0, 13*w/16.0) ;
    
    	const Point* ppt[1] = { rook_points[0] };
    	int npt[] = { 20 };
    
    	fillPoly( img,
    		ppt,
    		npt,
    		1,
    		Scalar( 255, 255, 255 ),
    		lineType );
    }


     

     

     

     

     


     

     

     

     

     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值