55 #include <sys/types.h> 64 #include "rnr/rnrconfig.h" 66 #include "opencv2/core/core.hpp" 67 #include "opencv2/highgui/highgui.hpp" 82 WinCvIoI::WinCvIoI(Size &sizeIoI,
88 Rect rectRoIIoI(0, 0, sizeIoI.width, sizeIoI.height);
90 setTransformParams(sizeIoI, rectRoIIoI, sizeTgt,
91 eOpRot, eOpAlign, bCropToFit);
94 void WinCvIoI::setTransformParams(Size &sizeIoI,
107 m_rectRoIIoI = rectRoIIoI;
111 m_eOpAlign = eOpAlign;
127 m_matRot = getRotationMatrix2D(center, -90.0, 1.0);
130 cvSetReal2D(&m_matRot, 0, 2, sizeIoI.height);
131 cvSetReal2D(&m_matRot, 1, 2, 0);
134 m_sizeRotated = Size(rectRoIIoI.height, rectRoIIoI.width);
142 m_matRot = getRotationMatrix2D(center, 180.0, 1.0);
145 cvSetReal2D(&m_matRot, 0, 2, sizeIoI.width);
146 cvSetReal2D(&m_matRot, 1, 2, sizeIoI.height);
149 m_sizeRotated = cvSize(rectRoIIoI.width, rectRoIIoI.height);
157 m_matRot = getRotationMatrix2D(center, 90.0, 1.0);
160 cvSetReal2D(&m_matRot, 0, 2, 0);
161 cvSetReal2D(&m_matRot, 1, 2, sizeIoI.width);
164 m_sizeRotated = cvSize(rectRoIIoI.height, rectRoIIoI.width);
173 m_matRot = getRotationMatrix2D(center, 0.0, 1.0);
176 m_sizeRotated = cvSize(rectRoIIoI.width, rectRoIIoI.height);
190 width = m_sizeRotated.width;
191 height = m_sizeRotated.height;
194 if( m_sizeRotated.width > sizeTgt.width )
196 x = (m_sizeRotated.width - sizeTgt.width) / 2;
197 width = sizeTgt.width;
200 if( m_sizeRotated.height > sizeTgt.height )
202 y = (m_sizeRotated.height - sizeTgt.height) / 2;
203 height = sizeTgt.height;
209 m_rectRoICropped = Rect(x, y, width, height);
223 sizeScaled = calcMaxFit43(m_sizeRotated, sizeTgt);
225 if( (sizeScaled.width < m_sizeRotated.width) ||
226 (sizeScaled.height < m_sizeRotated.height) )
228 m_sizeScaled = sizeScaled;
232 width = sizeScaled.width;
233 height = sizeScaled.height;
243 x = (sizeTgt.width - width) / 2;
244 y = (sizeTgt.height - height) / 2;
245 m_rectRoITgt = cvRect(x, y, width, height);
250 x = (sizeTgt.width - width) / 2;
251 y = sizeTgt.height - 1;
252 m_rectRoITgt = cvRect(x, y, width, height);
260 y = (sizeTgt.height - height) / 2;
261 m_rectRoITgt = cvRect(x, y, width, height);
266 void WinCvIoI::transform(Mat &imgIoI, Mat &imgTgt)
271 img = Mat(imgIoI, m_rectRoIIoI);
278 warpAffine(img, imgTgt, m_matRot, m_sizeRotated, INTER_LINEAR);
290 resize(imgTgt, imgTgt, m_sizeScaled, 0.0, 0.0, INTER_LINEAR);
298 imgTgt = Mat(imgTgt, m_rectRoICropped);
302 Point WinCvIoI::mapPoint(Point &ptDisplay)
310 pt.x = ptDisplay.x - m_rectRoITgt.x;
311 pt.y = ptDisplay.y - m_rectRoITgt.y;
314 if( (pt.x < 0) || (pt.x >= m_rectRoITgt.width) ||
315 (pt.y < 0) || (pt.y >= m_rectRoITgt.height) )
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);
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);
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);
349 fScaleX = (double)(m_rectRoIIoI.width) / (double)(m_rectRoITgt.width);
350 fScaleY = (double)(m_rectRoIIoI.height) / (double)(m_rectRoITgt.height);
355 pt.x = (int)((
double)(pt.x) * fScaleX);
356 pt.y = (int)((
double)(pt.y) * fScaleY);
359 pt.x += m_rectRoIIoI.x;
360 pt.y += m_rectRoIIoI.y;
365 Size WinCvIoI::calcMaxFit43(Size &sizeSrc, Size &sizeTgt)
367 Size sizeScaled = sizeSrc;
370 if( sizeSrc.width > sizeTgt.width )
376 if( sizeScaled.height > sizeTgt.height )
180 rotation (flip verically)
cv::Size ar43height(cv::Size &siz)
Calculate the nearest 4:3 aspect ratio from the height component of the target size.
RoadNarrows Robotics OpenCV Image of Interest class interface.
RoadNarrows Robotics Win abstract base class interface.
cv::Size ar43width(cv::Size &siz)
Calculate the nearest 4:3 aspect ratio from the width component of the target size.
RoadNarrows Robotics OpenCV Utilities.