您的位置:首页 > 编程语言 > C语言/C++

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

2014-02-19 17:43 375 查看
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.后果,结果(
result的名词复数 ); 成绩(包括比分、得票、获胜者或当选者名单等); 成功实现的事

v.发生( result的第三人称单数 ); 导致; 随着发生; 结果发生


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:
此数据结构表示了由其图像坐标




指定的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 coordinates

and

.
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.元组,数组

scalar英[ˈskeɪlə(r)]
[ˈskelɚ, -ˌlɑr]adj.梯状的;分等级的;数量的;标量的 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.
angle – Ellipse 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.
color – Ellipse 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] [b]美 [ɔlˈtɚnətɪv,
æl-]
[/b]

adj.非正统的,不寻常的; 两者择一的

n.二中择一; 可供选择的事物; 取舍; 非传统(或他择性)生活方式的追随者 (或鼓吹者)


inscribed 英 [ɪn'skraɪbd]

[ɪn'skraɪbd]

v.写,刻(
inscribe的过去式和过去分词 ); 内接


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.相配之衣物; <数>坐标( coordinate的名词复数 ); (颜色协调的)配套服装; [复数]女套服; 同等重要的人(或物)

v.使协调,使调和( coordinate的第三人称单数 ); 协调; 协同; 成为同等

C++: void fillPoly(Mat &img, const Point**
pts, const int* npts, int ncontours, const Scalar&
color, int lineType=8, intshift=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.外形,轮廓( contour的名词复数 ); 地图上表示相同海拔各点的)等高线


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 );
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: