IVT
PrimitivesDrawerCV.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: PrimitivesDrawerCV.cpp
37 // Author: Pedram Azad
38 // Date: 2004
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "PrimitivesDrawerCV.h"
49 
50 #include "Math/Constants.h"
51 #include "Structs/Structs.h"
52 #include "Image/IplImageAdaptor.h"
53 #include "Image/ByteImage.h"
54 
55 #include <cv.h>
56 
57 
58 
59 // ****************************************************************************
60 // Functions
61 // ****************************************************************************
62 
63 void PrimitivesDrawerCV::DrawCircle(CByteImage *pImage, const Vec2d &center, double radius, int r, int g, int b, int thickness)
64 {
65  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
66  cvCircle(pIplImage, cvPoint(int(center.x + 0.5), int(center.y + 0.5)), int(radius + 0.5), CV_RGB(b, g, r), thickness);
67  cvReleaseImageHeader(&pIplImage);
68 }
69 
70 void PrimitivesDrawerCV::DrawEllipse(CByteImage *pImage, const Ellipse2d &ellipse, int r, int g, int b, int thickness)
71 {
72  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
73  cvEllipse(pIplImage, cvPoint(int(ellipse.center.x + 0.5), int(ellipse.center.y + 0.5)), cvSize(int(ellipse.radius_x + 0.5), int(ellipse.radius_y + 0.5)), -ellipse.angle * FLOAT_RAD2DEG, 0.0, 360.0, CV_RGB(b, g, r), thickness);
74  cvReleaseImageHeader(&pIplImage);
75 }
76 
77 void PrimitivesDrawerCV::DrawLine(CByteImage *pImage, const PointPair2d &line, int r, int g, int b, int thickness)
78 {
79  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
80  cvLine(pIplImage, cvPoint(int(line.p1.x + 0.5), int(line.p1.y + 0.5)), cvPoint(int(line.p2.x + 0.5), int(line.p2.y + 0.5)), CV_RGB(b, g, r), thickness);
81  cvReleaseImageHeader(&pIplImage);
82 }
83 
84 void PrimitivesDrawerCV::DrawLine(CByteImage *pImage, const Vec2d &p1, const Vec2d &p2, int r, int g, int b, int thickness)
85 {
86  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
87  cvLine(pIplImage, cvPoint(int(p1.x + 0.5), int(p1.y + 0.5)), cvPoint(int(p2.x + 0.5), int(p2.y + 0.5)), CV_RGB(b, g, r), thickness);
88  cvReleaseImageHeader(&pIplImage);
89 }
90 
91 void PrimitivesDrawerCV::DrawRegion(CByteImage *pImage, const MyRegion &region, int r, int g, int b, int thickness)
92 {
93  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
94  cvRectangle(pIplImage, cvPoint(region.min_x, region.min_y), cvPoint(region.max_x, region.max_y), CV_RGB(b, g, r), thickness);
95  cvReleaseImageHeader(&pIplImage);
96 }
97 
98 void PrimitivesDrawerCV::DrawConvexPolygon(CByteImage *pImage, int *pPoints, int nPoints, int r, int g, int b, int thickness)
99 {
100  if (nPoints < 2)
101  {
102  printf("error: at least to points must be provided for PrimitivesDrawerCV::DrawConvexPolygon\n");
103  return;
104  }
105 
106  CvPoint *points = new CvPoint[nPoints];
107  for (int i = 0, index = 0; i < nPoints; i++, index += 2)
108  {
109  points[i].x = pPoints[index];
110  points[i].y = pPoints[index + 1];
111  }
112 
113 
114  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
115  if (thickness == -1)
116  cvFillConvexPoly(pIplImage, points, nPoints, CV_RGB(b, g, r));
117  else
118  {
119  for (int i = 0; i < nPoints - 1; i++)
120  cvLine(pIplImage, points[i], points[i + 1], CV_RGB(b, g, r), thickness);
121 
122  if (points[0].x != points[nPoints - 1].x || points[0].y != points[nPoints - 1].y)
123  cvLine(pIplImage, points[nPoints - 1], points[0], CV_RGB(b, g, r), thickness);
124  }
125  cvReleaseImageHeader(&pIplImage);
126 
127  delete [] points;
128 }
129 
130 void PrimitivesDrawerCV::PutText(CByteImage *pImage, const char *pText, double x, double y, double scale_x, double scale_y, int r, int g, int b, int thickness)
131 {
132  CvFont font;
133  cvInitFont(&font, CV_FONT_VECTOR0, scale_x, scale_y, 0, thickness);
134  IplImage *pIplImage = IplImageAdaptor::Adapt(pImage);
135  cvPutText(pIplImage, pText, cvPoint(int(x + 0.5), int(y + 0.5)), &font, CV_RGB(b, g, r));
136  cvReleaseImageHeader(&pIplImage);
137 }
Data structure for the representation of a 2D vector.
Definition: Math2d.h:82
void DrawEllipse(CByteImage *pImage, const Ellipse2d &ellipse, int r=255, int g=255, int b=255, int thickness=1)
Deprecated.
void DrawConvexPolygon(CByteImage *pImage, int *pPoints, int nPoints, int r=255, int g=255, int b=255, int thickness=1)
Draws a polygon into a CByteImage.
float radius_x
The radius in horizontal direction of the ellipse (horizontal refers to when angle = 0)...
Definition: Structs.h:147
float x
Definition: Math2d.h:84
int max_y
Definition: Structs.h:336
float radius_y
The radius in vertical direction of the ellipse (vertical refers to when angle = 0).
Definition: Structs.h:152
int max_x
Definition: Structs.h:335
IplImage * Adapt(const CByteImage *pImage, bool bAllocateMemory=false)
Converts a CByteImage to an IplImage.
void PutText(CByteImage *pImage, const char *pText, double x, double y, double scale_x, double scale_y, int r=255, int g=255, int b=255, int thickness=1)
Draws text into a CByteImage.
float y
Definition: Math2d.h:84
void DrawCircle(CByteImage *pImage, const Vec2d &mid_point, double radius, int r=255, int g=255, int b=255, int thickness=1)
Deprecated.
int min_y
Definition: Structs.h:334
Vec2d center
The center of the ellipse.
Definition: Structs.h:142
Vec2d p1
Definition: Structs.h:73
#define FLOAT_RAD2DEG
Definition: Constants.h:51
int min_x
Definition: Structs.h:333
Data structure for the representation of a 2D ellipse.
Definition: Structs.h:137
void DrawRegion(CByteImage *pImage, const MyRegion &region, int r=255, int g=255, int b=255, int thickness=1)
Deprecated.
float angle
The rotiation angle of the ellipse, given in radians.
Definition: Structs.h:161
Data structure for the representation of 8-bit grayscale images and 24-bit RGB (or HSV) color images ...
Definition: ByteImage.h:80
void DrawLine(CByteImage *pImage, const PointPair2d &line, int r=255, int g=255, int b=255, int thickness=1)
Deprecated.
Vec2d p2
Definition: Structs.h:74