11/*
2- * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved.
2+ * Copyright (c) 2024-2025 Qualcomm Innovation Center, Inc. All rights reserved.
33 * SPDX-License-Identifier: Apache-2.0
44*/
55
@@ -12,49 +12,52 @@ class FcvWarpPerspectiveLoop_Invoker : public cv::ParallelLoopBody
1212{
1313 public:
1414
15- FcvWarpPerspectiveLoop_Invoker (InputArray _src1, InputArray _src2, OutputArray _dst1, OutputArray _dst2, InputArray _M0,
16- Size _dsize) : cv::ParallelLoopBody()
17- {
18- src1 = _src1.getMat ();
19- src2 = _src2.getMat ();
20- dsize = _dsize;
21-
22- _dst1.create (dsize, src1.type ());
23- _dst2.create (dsize, src2.type ());
24- dst1 = _dst1.getMat ();
25- dst2 = _dst2.getMat ();
26-
27- M = _M0.getMat ();
28- }
15+ FcvWarpPerspectiveLoop_Invoker (const Mat& _src1, const Mat& _src2, Mat& _dst1, Mat& _dst2,
16+ const float * _M, fcvInterpolationType _interpolation = FASTCV_INTERPOLATION_TYPE_NEAREST_NEIGHBOR,
17+ fcvBorderType _borderType = fcvBorderType::FASTCV_BORDER_UNDEFINED, const int _borderValue = 0 )
18+ : ParallelLoopBody(), src1(_src1), src2(_src2), dst1(_dst1), dst2(_dst2), M(_M), interpolation(_interpolation),
19+ borderType (_borderType), borderValue(_borderValue)
20+ {}
2921
3022 virtual void operator ()(const cv::Range& range) const CV_OVERRIDE
3123 {
32- uchar* dst1_ptr = dst1.data + range.start *dst1.step ;
33- uchar* dst2_ptr = dst2.data + range.start *dst2.step ;
24+ uchar* dst1_ptr = dst1.data + range.start * dst1.step ;
3425 int rangeHeight = range.end - range.start ;
3526
3627 float rangeMatrix[9 ];
37- rangeMatrix[0 ] = M.at <float >(0 ,0 );
38- rangeMatrix[1 ] = M.at <float >(0 ,1 );
39- rangeMatrix[2 ] = M.at <float >(0 ,2 )+range.start *M.at <float >(0 ,1 );
40- rangeMatrix[3 ] = M.at <float >(1 ,0 );
41- rangeMatrix[4 ] = M.at <float >(1 ,1 );
42- rangeMatrix[5 ] = M.at <float >(1 ,2 )+range.start *M.at <float >(1 ,1 );
43- rangeMatrix[6 ] = M.at <float >(2 ,0 );
44- rangeMatrix[7 ] = M.at <float >(2 ,1 );
45- rangeMatrix[8 ] = M.at <float >(2 ,2 )+range.start *M.at <float >(2 ,1 );
46-
47- fcv2PlaneWarpPerspectiveu8 (src1.data , src2.data , src1.cols , src1.rows , src1.step , src2.step , dst1_ptr, dst2_ptr,
48- dsize.width , rangeHeight, dst1.step , dst2.step , rangeMatrix);
28+ rangeMatrix[0 ] = M[0 ];
29+ rangeMatrix[1 ] = M[1 ];
30+ rangeMatrix[2 ] = M[2 ]+range.start *M[1 ];
31+ rangeMatrix[3 ] = M[3 ];
32+ rangeMatrix[4 ] = M[4 ];
33+ rangeMatrix[5 ] = M[5 ]+range.start *M[4 ];
34+ rangeMatrix[6 ] = M[6 ];
35+ rangeMatrix[7 ] = M[7 ];
36+ rangeMatrix[8 ] = M[8 ]+range.start *M[7 ];
37+
38+ if ((src2.empty ()) || (dst2.empty ()))
39+ {
40+ fcvWarpPerspectiveu8_v5 (src1.data , src1.cols , src1.rows , src1.step , src1.channels (), dst1_ptr, dst1.cols , rangeHeight,
41+ dst1.step , rangeMatrix, interpolation, borderType, borderValue);
42+ }
43+ else
44+ {
45+ uchar* dst2_ptr = dst2.data + range.start * dst2.step ;
46+ fcv2PlaneWarpPerspectiveu8 (src1.data , src2.data , src1.cols , src1.rows , src1.step , src2.step , dst1_ptr, dst2_ptr,
47+ dst1.cols , rangeHeight, dst1.step , dst2.step , rangeMatrix);
48+ }
4949 }
5050
5151 private:
52- Mat src1;
53- Mat src2;
54- Mat dst1;
55- Mat dst2;
56- Mat M;
57- Size dsize;
52+
53+ const Mat& src1;
54+ const Mat& src2;
55+ Mat& dst1;
56+ Mat& dst2;
57+ const float * M;
58+ fcvInterpolationType interpolation;
59+ fcvBorderType borderType;
60+ int borderValue;
5861
5962 FcvWarpPerspectiveLoop_Invoker (const FcvWarpPerspectiveLoop_Invoker &); // = delete;
6063 const FcvWarpPerspectiveLoop_Invoker& operator = (const FcvWarpPerspectiveLoop_Invoker &); // = delete;
@@ -68,8 +71,108 @@ void warpPerspective2Plane(InputArray _src1, InputArray _src2, OutputArray _dst1
6871 CV_Assert (!_src2.empty () && _src2.type () == CV_8UC1);
6972 CV_Assert (!_M0.empty ());
7073
74+ Mat src1 = _src1.getMat ();
75+ Mat src2 = _src2.getMat ();
76+
77+ _dst1.create (dsize, src1.type ());
78+ _dst2.create (dsize, src2.type ());
79+ Mat dst1 = _dst1.getMat ();
80+ Mat dst2 = _dst2.getMat ();
81+
82+ Mat M0 = _M0.getMat ();
83+ CV_Assert ((M0.type () == CV_32F || M0.type () == CV_64F) && M0.rows == 3 && M0.cols == 3 );
84+ float matrix[9 ];
85+ Mat M (3 , 3 , CV_32F, matrix);
86+ M0.convertTo (M, M.type ());
87+
88+ int nThreads = getNumThreads ();
89+ int nStripes = nThreads > 1 ? 2 *nThreads : 1 ;
90+
91+ cv::parallel_for_ (cv::Range (0 , dsize.height ),
92+ FcvWarpPerspectiveLoop_Invoker (src1, src2, dst1, dst2, matrix), nStripes);
93+ }
94+
95+ void warpPerspective (InputArray _src, OutputArray _dst, InputArray _M0, Size dsize, int interpolation, int borderType,
96+ const Scalar& borderValue)
97+ {
98+ Mat src = _src.getMat ();
99+
100+ _dst.create (dsize, src.type ());
101+ Mat dst = _dst.getMat ();
102+
103+ Mat M0 = _M0.getMat ();
104+ CV_Assert ((M0.type () == CV_32F || M0.type () == CV_64F) && M0.rows == 3 && M0.cols == 3 );
105+ float matrix[9 ];
106+ Mat M (3 , 3 , CV_32F, matrix);
107+ M0.convertTo (M, M.type ());
108+
109+ // Do not support inplace case
110+ CV_Assert (src.data != dst.data );
111+ // Only support CV_8U
112+ CV_Assert (src.depth () == CV_8U);
113+
114+ INITIALIZATION_CHECK;
115+
116+ fcvBorderType fcvBorder;
117+ uint8_t fcvBorderValue = 0 ;
118+ fcvInterpolationType fcvInterpolation;
119+
120+ switch (borderType)
121+ {
122+ case BORDER_CONSTANT:
123+ {
124+ // Border value should be same
125+ CV_Assert ((borderValue[0 ] == borderValue[1 ]) &&
126+ (borderValue[0 ] == borderValue[2 ]) &&
127+ (borderValue[0 ] == borderValue[3 ]));
128+
129+ fcvBorder = fcvBorderType::FASTCV_BORDER_CONSTANT;
130+ fcvBorderValue = static_cast <uint8_t >(borderValue[0 ]);
131+ break ;
132+ }
133+ case BORDER_REPLICATE:
134+ {
135+ fcvBorder = fcvBorderType::FASTCV_BORDER_REPLICATE;
136+ break ;
137+ }
138+ case BORDER_TRANSPARENT:
139+ {
140+ fcvBorder = fcvBorderType::FASTCV_BORDER_UNDEFINED;
141+ break ;
142+ }
143+ default :
144+ CV_Error (cv::Error::StsBadArg, cv::format (" Border type:%d is not supported" , borderType));
145+ }
146+
147+ switch (interpolation)
148+ {
149+ case INTER_NEAREST:
150+ {
151+ fcvInterpolation = FASTCV_INTERPOLATION_TYPE_NEAREST_NEIGHBOR;
152+ break ;
153+ }
154+ case INTER_LINEAR:
155+ {
156+ fcvInterpolation = FASTCV_INTERPOLATION_TYPE_BILINEAR;
157+ break ;
158+ }
159+ case INTER_AREA:
160+ {
161+ fcvInterpolation = FASTCV_INTERPOLATION_TYPE_AREA;
162+ break ;
163+ }
164+ default :
165+ CV_Error (cv::Error::StsBadArg, cv::format (" Interpolation type:%d is not supported" , interpolation));
166+ }
167+
168+ int nThreads = cv::getNumThreads ();
169+ int nStripes = nThreads > 1 ? 2 *nThreads : 1 ;
170+
171+ // placeholder
172+ Mat tmp;
173+
71174 cv::parallel_for_ (cv::Range (0 , dsize.height ),
72- FcvWarpPerspectiveLoop_Invoker (_src1, _src2, _dst1, _dst2, _M0, dsize ), 1 );
175+ FcvWarpPerspectiveLoop_Invoker (src, tmp, dst, tmp, matrix, fcvInterpolation, fcvBorder, fcvBorderValue ), nStripes );
73176}
74177
75178} // fastcv::
0 commit comments