18
18
namespace fastdeploy {
19
19
namespace vision {
20
20
21
- bool LetterBoxResize::operator ()(Mat* mat, ProcLib lib) {
21
+
22
+ bool LetterBoxResize::ImplByOpenCV (Mat* mat){
22
23
if (mat->Channels () != color_.size ()) {
23
24
FDERROR << " LetterBoxResize: Require input channels equals to size of color value, "
24
25
" but now channels = "
@@ -45,14 +46,117 @@ bool LetterBoxResize::operator()(Mat* mat, ProcLib lib) {
45
46
int bottom = std::round (padh + 0.1 );
46
47
int left = std::round (padw - 0.1 );
47
48
int right = std::round (padw + 0.1 );
48
- Resize::Run (mat, new_shape_w, new_shape_h, -1.0 , -1.0 , 3 , false , lib );
49
- Pad::Run (mat, top, bottom, left, right, color_, lib );
49
+ Resize::Run (mat, new_shape_w, new_shape_h, -1.0 , -1.0 , 3 , false );
50
+ Pad::Run (mat, top, bottom, left, right, color_);
50
51
return true ;
51
52
}
52
53
53
- bool LetterBoxResize::Run (Mat* mat, const std::vector<int >& target_size, const std::vector<float >& color, ProcLib lib) {
54
+ #ifdef ENABLE_FLYCV
55
+ bool LetterBoxResize::ImplByFlyCV (Mat* mat){
56
+ if (mat->Channels () != color_.size ()) {
57
+ FDERROR << " LetterBoxResize: Require input channels equals to size of color value, "
58
+ " but now channels = "
59
+ << mat->Channels ()
60
+ << " , the size of color values = " << color_.size () << " ."
61
+ << std::endl;
62
+ return false ;
63
+ }
64
+ // generate scale_factor
65
+ int origin_w = mat->Width ();
66
+ int origin_h = mat->Height ();
67
+ int target_h = target_size_[0 ];
68
+ int target_w = target_size_[1 ];
69
+ float ratio_h = static_cast <float >(target_h) / static_cast <float >(origin_h);
70
+ float ratio_w = static_cast <float >(target_w) / static_cast <float >(origin_w);
71
+ float resize_scale = std::min (ratio_h, ratio_w);
72
+ // get_resized_shape
73
+ int new_shape_w = std::round (origin_w * resize_scale);
74
+ int new_shape_h = std::round (origin_h * resize_scale);
75
+ // calculate pad
76
+ float padw = (target_size_[1 ] - new_shape_w) / 2 .;
77
+ float padh = (target_size_[0 ] - new_shape_h) / 2 .;
78
+ int top = std::round (padh - 0.1 );
79
+ int bottom = std::round (padh + 0.1 );
80
+ int left = std::round (padw - 0.1 );
81
+ int right = std::round (padw + 0.1 );
82
+ Resize::Run (mat, new_shape_w, new_shape_h, -1.0 , -1.0 , 3 , false , ProcLib::FLYCV);
83
+ Pad::Run (mat, top, bottom, left, right, color_, ProcLib::FLYCV);
84
+ return true ;
85
+ }
86
+ #endif
87
+
88
+ #ifdef ENABLE_CVCUDA
89
+ bool LetterBoxResize::ImplByCvCuda (Mat* mat){
90
+ if (mat->Channels () != color_.size ()) {
91
+ FDERROR << " LetterBoxResize: Require input channels equals to size of color value, "
92
+ " but now channels = "
93
+ << mat->Channels ()
94
+ << " , the size of color values = " << color_.size () << " ."
95
+ << std::endl;
96
+ return false ;
97
+ }
98
+ // generate scale_factor
99
+ int origin_w = mat->Width ();
100
+ int origin_h = mat->Height ();
101
+ int target_h = target_size_[0 ];
102
+ int target_w = target_size_[1 ];
103
+ float ratio_h = static_cast <float >(target_h) / static_cast <float >(origin_h);
104
+ float ratio_w = static_cast <float >(target_w) / static_cast <float >(origin_w);
105
+ float resize_scale = std::min (ratio_h, ratio_w);
106
+ // get_resized_shape
107
+ int new_shape_w = std::round (origin_w * resize_scale);
108
+ int new_shape_h = std::round (origin_h * resize_scale);
109
+ // calculate pad
110
+ float padw = (target_size_[1 ] - new_shape_w) / 2 .;
111
+ float padh = (target_size_[0 ] - new_shape_h) / 2 .;
112
+ int top = std::round (padh - 0.1 );
113
+ int bottom = std::round (padh + 0.1 );
114
+ int left = std::round (padw - 0.1 );
115
+ int right = std::round (padw + 0.1 );
116
+ Resize::Run (mat, new_shape_w, new_shape_h, -1.0 , -1.0 , 3 , false , ProcLib::CVCUDA);
117
+ Pad::Run (mat, top, bottom, left, right, color_, ProcLib::CVCUDA);
118
+ return true ;
119
+ }
120
+ #endif
121
+
122
+ #ifdef ENABLE_CUDA
123
+ bool LetterBoxResize::ImplByCuda (Mat* mat){
124
+ if (mat->Channels () != color_.size ()) {
125
+ FDERROR << " LetterBoxResize: Require input channels equals to size of color value, "
126
+ " but now channels = "
127
+ << mat->Channels ()
128
+ << " , the size of color values = " << color_.size () << " ."
129
+ << std::endl;
130
+ return false ;
131
+ }
132
+ // generate scale_factor
133
+ int origin_w = mat->Width ();
134
+ int origin_h = mat->Height ();
135
+ int target_h = target_size_[0 ];
136
+ int target_w = target_size_[1 ];
137
+ float ratio_h = static_cast <float >(target_h) / static_cast <float >(origin_h);
138
+ float ratio_w = static_cast <float >(target_w) / static_cast <float >(origin_w);
139
+ float resize_scale = std::min (ratio_h, ratio_w);
140
+ // get_resized_shape
141
+ int new_shape_w = std::round (origin_w * resize_scale);
142
+ int new_shape_h = std::round (origin_h * resize_scale);
143
+ // calculate pad
144
+ float padw = (target_size_[1 ] - new_shape_w) / 2 .;
145
+ float padh = (target_size_[0 ] - new_shape_h) / 2 .;
146
+ int top = std::round (padh - 0.1 );
147
+ int bottom = std::round (padh + 0.1 );
148
+ int left = std::round (padw - 0.1 );
149
+ int right = std::round (padw + 0.1 );
150
+ Resize::Run (mat, new_shape_w, new_shape_h, -1.0 , -1.0 , 3 , false , ProcLib::CUDA);
151
+ Pad::Run (mat, top, bottom, left, right, color_, ProcLib::CUDA);
152
+ return true ;
153
+ }
154
+ #endif
155
+
156
+
157
+ bool LetterBoxResize::Run (Mat* mat, const std::vector<int >& target_size, const std::vector<float >& color,ProcLib lib) {
54
158
auto l = LetterBoxResize (target_size,color);
55
- return l (mat, lib);
159
+ return l (mat,lib);
56
160
}
57
161
58
162
} // namespace vision
0 commit comments