appkit  1.5.1
RoadNarrows Robotics Application Kit
rnr::WinCvIoI Class Reference

#include <WinCvIoI.h>

Public Member Functions

 WinCvIoI (cv::Size &sizeIoI, cv::Size &sizeTgt, RotOp eOpRot, AlignOp eOpAlign, bool bCropToFit)
 Image of Interest class initialization constructor. More...
 
virtual ~WinCvIoI ()
 Destructor.
 
void setTransformParams (cv::Size &sizeIoI, cv::Rect &rectRoIIoI, cv::Size &sizeTgt, RotOp eOpRot, AlignOp eOpAlign, bool bCropToFit)
 Set Image of Interest transfromation parameters. More...
 
virtual void transform (cv::Mat &imgIoI, cv::Mat &imgTgt)
 Transform Image of Interest into the target image. More...
 
cv::Point mapPoint (cv::Point &ptDisplay)
 Map display pixel coordinates to Image of Interest pixel coordinates. More...
 
cv::Size getOriginalSize ()
 
cv::Size getTransformedSize ()
 Get the transformed target size. More...
 
cv::Size calcMaxFit43 (cv::Size &sizeSrc, cv::Size &sizeTgt)
 Calculate the maximal size the source can fit into the target size while keeping a 4:3 aspect ratio. More...
 

Protected Attributes

cv::Size m_sizeIoI
 image native size
 
cv::Rect m_rectRoIIoI
 image of interest region of interest
 
cv::Size m_sizeTgt
 target workspace size
 
cv::Rect m_rectRoITgt
 target workspace region of interest
 
RotOp m_eOpRot
 image rotation operator
 
cv::Mat m_matRot
 2x3 transformation matrix
 
cv::Size m_sizeRotated
 rotated image size
 
bool m_bOpRotate
 do [not] apply rotation operation
 
bool m_bOpScale
 do [not] apply resize operation
 
cv::Size m_sizeScaled
 scaled image size
 
bool m_bOpCrop
 do [not] apply crop operation
 
cv::Rect m_rectRoICropped
 cropped region of interest
 
AlignOp m_eOpAlign
 image alignment operator
 

Detailed Description

Window OpenCV Image of Interest (IoI) Class.

An IoI is typically at a higher resolution the what can be showed on a display. Moreover, the display of IoI may require rotation and alignment.

Definition at line 81 of file WinCvIoI.h.

Constructor & Destructor Documentation

WinCvIoI::WinCvIoI ( cv::Size &  sizeIoI,
cv::Size &  sizeTgt,
RotOp  eOpRot,
AlignOp  eOpAlign,
bool  bCropToFit 
)

Image of Interest class initialization constructor.

Initialize the transformations needed to place a IoI in the window.

Parameters
sizeIoISize of hi-res Image of Interest.
sizeTgtSize of target workspace, oriented at 0 degrees.
eOpRotTarget image rotation operation. See rnmpwin_rot.
eOpAlignTarget image alignment operation. See rnmpwin_align.
bCropToFitIf true, the transformed image is cropped to fit into target size. Otherwise the transformed image is scaled to fit.

Definition at line 82 of file WinCvIoI.cxx.

87 {
88  Rect rectRoIIoI(0, 0, sizeIoI.width, sizeIoI.height);
89 
90  setTransformParams(sizeIoI, rectRoIIoI, sizeTgt,
91  eOpRot, eOpAlign, bCropToFit);
92 }
void setTransformParams(cv::Size &sizeIoI, cv::Rect &rectRoIIoI, cv::Size &sizeTgt, RotOp eOpRot, AlignOp eOpAlign, bool bCropToFit)
Set Image of Interest transfromation parameters.
Definition: WinCvIoI.cxx:94

Member Function Documentation

Size WinCvIoI::calcMaxFit43 ( cv::Size &  sizeSrc,
cv::Size &  sizeTgt 
)

Calculate the maximal size the source can fit into the target size while keeping a 4:3 aspect ratio.

Parameters
sizeSrcSource size.
sizeTgtTarget size.
Returns
Maximum size.

Definition at line 365 of file WinCvIoI.cxx.

References rnr::ar43height(), and rnr::ar43width().

Referenced by getTransformedSize().

366 {
367  Size sizeScaled = sizeSrc;
368 
369  // fit width into target size
370  if( sizeSrc.width > sizeTgt.width )
371  {
372  sizeScaled = ar43width(sizeTgt);
373  }
374 
375  // fit scaled-by-width height into target size
376  if( sizeScaled.height > sizeTgt.height )
377  {
378  sizeScaled = ar43height(sizeTgt);
379  }
380 
381  return sizeScaled;
382 }
cv::Size ar43height(cv::Size &siz)
Calculate the nearest 4:3 aspect ratio from the height component of the target size.
Definition: WinOpenCv.h:307
cv::Size ar43width(cv::Size &siz)
Calculate the nearest 4:3 aspect ratio from the width component of the target size.
Definition: WinOpenCv.h:272
cv::Size rnr::WinCvIoI::getOriginalSize ( )
inline

Get the IoI original size.

Returns
Size.

Definition at line 157 of file WinCvIoI.h.

References m_sizeIoI.

158  {
159  return cv::Size(m_sizeIoI.width, m_sizeIoI.height);
160  }
cv::Size m_sizeIoI
image native size
Definition: WinCvIoI.h:184
cv::Size rnr::WinCvIoI::getTransformedSize ( )
inline

Get the transformed target size.

Returns
Size.

Definition at line 167 of file WinCvIoI.h.

References calcMaxFit43(), and m_rectRoITgt.

168  {
169  return cv::Size(m_rectRoITgt.width, m_rectRoITgt.height);
170  }
cv::Rect m_rectRoITgt
target workspace region of interest
Definition: WinCvIoI.h:187
Point WinCvIoI::mapPoint ( cv::Point &  ptDisplay)

Map display pixel coordinates to Image of Interest pixel coordinates.

Parameters
ptDisplayPoint in display coordinates.
Returns
Cooresponding point in target IoI.

Definition at line 302 of file WinCvIoI.cxx.

References rnr::RotOp0, rnr::RotOp180, rnr::RotOp270, and rnr::RotOp90.

Referenced by ~WinCvIoI().

303 {
304  Point pt;
305  int t;
306  double fScaleX;
307  double fScaleY;
308 
309  // adjust of image offset
310  pt.x = ptDisplay.x - m_rectRoITgt.x;
311  pt.y = ptDisplay.y - m_rectRoITgt.y;
312 
313  // point not within target image of interest
314  if( (pt.x < 0) || (pt.x >= m_rectRoITgt.width) ||
315  (pt.y < 0) || (pt.y >= m_rectRoITgt.height) )
316  {
317  pt.x = pt.y = -1;
318  return pt;
319  }
320 
321  // remap x,y relative to image of interest origin (ulc)
322  switch( m_eOpRot )
323  {
324  case RotOp270:
325  t = pt.x;
326  pt.x = pt.y + m_rectRoICropped.y;
327  pt.y = m_rectRoITgt.width - (t - m_rectRoICropped.x) - 1;
328  fScaleX = (double)(m_rectRoIIoI.width) / (double)(m_rectRoITgt.height);
329  fScaleY = (double)(m_rectRoIIoI.height) / (double)(m_rectRoITgt.width);
330  break;
331 
332  case RotOp180:
333  pt.x = m_rectRoITgt.width - pt.x - 1;
334  pt.y = m_rectRoITgt.height - pt.y - 1;
335  fScaleX = (double)(m_rectRoIIoI.width) / (double)(m_rectRoITgt.width);
336  fScaleY = (double)(m_rectRoIIoI.height) / (double)(m_rectRoITgt.height);
337  break;
338 
339  case RotOp90:
340  t = pt.x;
341  pt.x = m_rectRoITgt.height - pt.y - m_rectRoICropped.y - 1;
342  pt.y = t + m_rectRoICropped.x;
343  fScaleX = (double)(m_rectRoIIoI.width) / (double)(m_rectRoITgt.height);
344  fScaleY = (double)(m_rectRoIIoI.height) / (double)(m_rectRoITgt.width);
345  break;
346 
347  case RotOp0:
348  default:
349  fScaleX = (double)(m_rectRoIIoI.width) / (double)(m_rectRoITgt.width);
350  fScaleY = (double)(m_rectRoIIoI.height) / (double)(m_rectRoITgt.height);
351  break;
352  }
353 
354  // scale coordinate to match image of interest real size
355  pt.x = (int)((double)(pt.x) * fScaleX);
356  pt.y = (int)((double)(pt.y) * fScaleY);
357 
358  // adjust of target image of interest ROI
359  pt.x += m_rectRoIIoI.x;
360  pt.y += m_rectRoIIoI.y;
361 
362  return pt;
363 }
180 rotation (flip verically)
Definition: Win.h:101
cv::Rect m_rectRoIIoI
image of interest region of interest
Definition: WinCvIoI.h:185
270 rotation (-90 &deg;)
Definition: Win.h:102
cv::Rect m_rectRoITgt
target workspace region of interest
Definition: WinCvIoI.h:187
90 rotation
Definition: Win.h:100
RotOp m_eOpRot
image rotation operator
Definition: WinCvIoI.h:188
cv::Rect m_rectRoICropped
cropped region of interest
Definition: WinCvIoI.h:195
0 &deg; rotation (none)
Definition: Win.h:99
void WinCvIoI::setTransformParams ( cv::Size &  sizeIoI,
cv::Rect &  rectRoIIoI,
cv::Size &  sizeTgt,
RotOp  eOpRot,
AlignOp  eOpAlign,
bool  bCropToFit 
)

Set Image of Interest transfromation parameters.

Initialize the transformations needed to place a IoI in the window.

Parameters
sizeIoISize of hi-res Image of Interest.
rectRoIIoIRegion of Interest within Image of Interest.
sizeTgtSize of target workspace, oriented at 0 degrees.
eOpRotTarget image rotation operation. See rnmpwin_rot.
eOpAlignTarget image alignment operation. See rnmpwin_align.
bCropToFitIf true, the transformed image is cropped to fit into target size. Otherwise the transformed image is scaled to fit.

Definition at line 94 of file WinCvIoI.cxx.

References rnr::AlignOpCenter, rnr::AlignOpDefault, rnr::AlignOpLeft, rnr::AlignOpRight, rnr::RotOp0, rnr::RotOp180, rnr::RotOp270, and rnr::RotOp90.

Referenced by ~WinCvIoI().

100 {
101  Point2f center; // rotation axis
102  Size sizeScaled; // size of rotated and resized image
103  int x, y; // working coordinates
104  int width, height; // working dimensions
105 
106  m_sizeIoI = sizeIoI;
107  m_rectRoIIoI = rectRoIIoI;
108  m_sizeTgt = sizeTgt;
109  m_eOpRot = eOpRot;
110  m_bOpRotate = false;
111  m_eOpAlign = eOpAlign;
112  m_bOpScale = false;
113  m_bOpCrop = false;
114 
115  // rotate about upper left corner (makes translation calculations easier)
116  center.x = 0;
117  center.y = 0;
118 
119  //
120  // Rotation and resized images
121  //
122  switch( eOpRot )
123  {
124  // 270 degree rotation = -90 degrees
125  case RotOp270:
126  // set 2x3 rotation matrix
127  m_matRot = getRotationMatrix2D(center, -90.0, 1.0);
128 
129  // translate
130  cvSetReal2D(&m_matRot, 0, 2, sizeIoI.height);
131  cvSetReal2D(&m_matRot, 1, 2, 0);
132 
133  // rotated IoI
134  m_sizeRotated = Size(rectRoIIoI.height, rectRoIIoI.width);
135  m_bOpRotate = true;
136 
137  break;
138 
139  // 180 degree rotation
140  case RotOp180:
141  // set 2x3 rotation matrix
142  m_matRot = getRotationMatrix2D(center, 180.0, 1.0);
143 
144  // translate
145  cvSetReal2D(&m_matRot, 0, 2, sizeIoI.width);
146  cvSetReal2D(&m_matRot, 1, 2, sizeIoI.height);
147 
148  // rotated IoI
149  m_sizeRotated = cvSize(rectRoIIoI.width, rectRoIIoI.height);
150  m_bOpRotate = true;
151 
152  break;
153 
154  // 90 degree rotation
155  case RotOp90:
156  // set 2x3 rotation matrix
157  m_matRot = getRotationMatrix2D(center, 90.0, 1.0);
158 
159  // translate
160  cvSetReal2D(&m_matRot, 0, 2, 0);
161  cvSetReal2D(&m_matRot, 1, 2, sizeIoI.width);
162 
163  // rotated IoI
164  m_sizeRotated = cvSize(rectRoIIoI.height, rectRoIIoI.width);
165  m_bOpRotate = true;
166 
167  break;
168 
169  // 0 degree rotation = no rotations
170  case RotOp0:
171  default:
172  // set 2x3 rotation matrix
173  m_matRot = getRotationMatrix2D(center, 0.0, 1.0);
174 
175  // rotated IoI
176  m_sizeRotated = cvSize(rectRoIIoI.width, rectRoIIoI.height);
177  m_bOpRotate = false;
178 
179  break;
180  }
181 
182  //
183  // Crop image to fit
184  //
185  if( bCropToFit )
186  {
187  // initialize
188  x = 0;
189  y = 0;
190  width = m_sizeRotated.width;
191  height = m_sizeRotated.height;
192  m_bOpCrop = false;
193 
194  if( m_sizeRotated.width > sizeTgt.width )
195  {
196  x = (m_sizeRotated.width - sizeTgt.width) / 2;
197  width = sizeTgt.width;
198  m_bOpCrop = true;
199  }
200  if( m_sizeRotated.height > sizeTgt.height )
201  {
202  y = (m_sizeRotated.height - sizeTgt.height) / 2;
203  height = sizeTgt.height;
204  m_bOpCrop = true;
205  }
206 
207  if( m_bOpCrop )
208  {
209  m_rectRoICropped = Rect(x, y, width, height);
210  }
211  }
212 
213  //
214  // Scale image to fit
215  //
216  else
217  {
218  // initialize
219  m_bOpScale = false;
220 
221  // best fit in a 4:3 aspect ratio
222  // RDK TODO allow for other aspect ratios
223  sizeScaled = calcMaxFit43(m_sizeRotated, sizeTgt);
224 
225  if( (sizeScaled.width < m_sizeRotated.width) ||
226  (sizeScaled.height < m_sizeRotated.height) )
227  {
228  m_sizeScaled = sizeScaled;
229  m_bOpScale = true;
230  }
231 
232  width = sizeScaled.width;
233  height = sizeScaled.height;
234  }
235 
236  //
237  // Alignment ROI
238  //
239  switch( eOpAlign )
240  {
241  // align center
242  case AlignOpCenter:
243  x = (sizeTgt.width - width) / 2;
244  y = (sizeTgt.height - height) / 2;
245  m_rectRoITgt = cvRect(x, y, width, height);
246  break;
247 
248  // align right
249  case AlignOpRight:
250  x = (sizeTgt.width - width) / 2;
251  y = sizeTgt.height - 1;
252  m_rectRoITgt = cvRect(x, y, width, height);
253  break;
254 
255  // align left
256  case AlignOpLeft:
257  case AlignOpDefault:
258  default:
259  x = 0;
260  y = (sizeTgt.height - height) / 2;
261  m_rectRoITgt = cvRect(x, y, width, height);
262  break;
263  }
264 }
180 rotation (flip verically)
Definition: Win.h:101
cv::Rect m_rectRoIIoI
image of interest region of interest
Definition: WinCvIoI.h:185
270 rotation (-90 &deg;)
Definition: Win.h:102
rigth alignment
Definition: Win.h:86
cv::Size calcMaxFit43(cv::Size &sizeSrc, cv::Size &sizeTgt)
Calculate the maximal size the source can fit into the target size while keeping a 4:3 aspect ratio...
Definition: WinCvIoI.cxx:365
bool m_bOpRotate
do [not] apply rotation operation
Definition: WinCvIoI.h:191
cv::Size m_sizeTgt
target workspace size
Definition: WinCvIoI.h:186
bool m_bOpScale
do [not] apply resize operation
Definition: WinCvIoI.h:192
left alignment
Definition: Win.h:84
cv::Size m_sizeRotated
rotated image size
Definition: WinCvIoI.h:190
cv::Rect m_rectRoITgt
target workspace region of interest
Definition: WinCvIoI.h:187
cv::Size m_sizeIoI
image native size
Definition: WinCvIoI.h:184
bool m_bOpCrop
do [not] apply crop operation
Definition: WinCvIoI.h:194
cv::Mat m_matRot
2x3 transformation matrix
Definition: WinCvIoI.h:189
90 rotation
Definition: Win.h:100
RotOp m_eOpRot
image rotation operator
Definition: WinCvIoI.h:188
cv::Rect m_rectRoICropped
cropped region of interest
Definition: WinCvIoI.h:195
0 &deg; rotation (none)
Definition: Win.h:99
AlignOp m_eOpAlign
image alignment operator
Definition: WinCvIoI.h:196
cv::Size m_sizeScaled
scaled image size
Definition: WinCvIoI.h:193
default alignment (left)
Definition: Win.h:83
center alignment
Definition: Win.h:85
void WinCvIoI::transform ( cv::Mat &  imgIoI,
cv::Mat &  imgTgt 
)
virtual

Transform Image of Interest into the target image.

Parameters
imgIoIImage of Interest.
[in,out]

Definition at line 266 of file WinCvIoI.cxx.

Referenced by ~WinCvIoI().

267 {
268  Mat img;
269  Size dsize;
270 
271  img = Mat(imgIoI, m_rectRoIIoI);
272 
273  //
274  // Rotate IoI
275  //
276  if( m_bOpRotate )
277  {
278  warpAffine(img, imgTgt, m_matRot, m_sizeRotated, INTER_LINEAR);
279  }
280  else
281  {
282  imgTgt = img;
283  }
284 
285  //
286  // Scale IoI
287  //
288  if( m_bOpScale )
289  {
290  resize(imgTgt, imgTgt, m_sizeScaled, 0.0, 0.0, INTER_LINEAR);
291  }
292 
293  //
294  // Crop IoI
295  //
296  if( m_bOpCrop )
297  {
298  imgTgt = Mat(imgTgt, m_rectRoICropped);
299  }
300 }
cv::Rect m_rectRoIIoI
image of interest region of interest
Definition: WinCvIoI.h:185
bool m_bOpRotate
do [not] apply rotation operation
Definition: WinCvIoI.h:191
bool m_bOpScale
do [not] apply resize operation
Definition: WinCvIoI.h:192
cv::Size m_sizeRotated
rotated image size
Definition: WinCvIoI.h:190
bool m_bOpCrop
do [not] apply crop operation
Definition: WinCvIoI.h:194
cv::Mat m_matRot
2x3 transformation matrix
Definition: WinCvIoI.h:189
cv::Rect m_rectRoICropped
cropped region of interest
Definition: WinCvIoI.h:195
cv::Size m_sizeScaled
scaled image size
Definition: WinCvIoI.h:193

The documentation for this class was generated from the following files: