The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

d/ht: make blackbar work with non-square inputs

authored by

Moses Turner and committed by
Jakob Bornecrantz
ee429ac2 cb7563ec

+44 -7
+44 -7
src/xrt/drivers/ht/ht_image_math.hpp
··· 17 17 #include "ht_driver.hpp" 18 18 19 19 #include <opencv2/calib3d.hpp> 20 + #include <opencv2/core/matx.hpp> 20 21 #include <opencv2/imgproc.hpp> 21 22 #include <opencv2/core/types.hpp> 22 23 ··· 104 105 */ 105 106 106 107 static cv::Matx23f 107 - blackbar(cv::Mat &in, cv::Mat &out, xrt_vec2_i32 out_size) 108 + blackbar(cv::Mat &in, cv::Mat &out, xrt_size out_size) 108 109 { 109 - float aspect_ratio_input = in.cols / in.rows; 110 - float aspect_ratio_output = out_size.x / out_size.y; 110 + #if 1 111 + // Easy to think about, always right, but pretty slow: 112 + // Get a matrix from the original to the scaled down / blackbar'd image, then get one that goes back. 113 + // Then just warpAffine() it. 114 + // More expensive on the computer, but cheap in programmer time - I'm somewhat allergic to thinking in pixel 115 + // coordinates. We can come back and optimize later. 116 + 117 + // Do the black bars need to be on top and bottom, or on left and right? 118 + float scale_down_w = (float)out_size.w / (float)in.cols; // 128/1280 = 0.1 119 + float scale_down_h = (float)out_size.h / (float)in.rows; // 128/800 = 0.16 120 + 121 + float scale_down = fmin(scale_down_w, scale_down_h); // 0.1 122 + 123 + float width_inside = (float)in.cols * scale_down; 124 + float height_inside = (float)in.rows * scale_down; 111 125 126 + float translate_x = (out_size.w - width_inside) / 2; // should be 0 for 1280x800 127 + float translate_y = (out_size.h - height_inside) / 2; // should be (1280-800)/2 = 240 128 + 129 + cv::Matx23f go; 130 + // clang-format off 131 + go(0,0) = scale_down; go(0,1) = 0.0f; go(0,2) = translate_x; 132 + go(1,0) = 0.0f; go(1,1) = scale_down; go(1,2) = translate_y; 133 + // clang-format on 134 + 135 + cv::warpAffine(in, out, go, cv::Size(out_size.w, out_size.h)); 136 + 137 + cv::Matx23f ret; 138 + 139 + // clang-format off 140 + ret(0,0) = 1.0f/scale_down; ret(0,1) = 0.0f; ret(0,2) = -translate_x/scale_down; 141 + ret(1,0) = 0.0f; ret(1,1) = 1.0f/scale_down; ret(1,2) = -translate_y/scale_down; 142 + // clang-format on 143 + 144 + return ret; 145 + #else 146 + // Fast, always wrong if the input isn't square. You'd end up using something like this, plus some 147 + // copyMakeBorder if you want to optimize. 112 148 if (aspect_ratio_input == aspect_ratio_output) { 113 - cv::resize(in, out, {out_size.x, out_size.y}); 149 + cv::resize(in, out, {out_size.w, out_size.h}); 114 150 cv::Matx23f ret; 115 - float scale_from_out_to_in = (float)in.cols / (float)out_size.x; 151 + float scale_from_out_to_in = (float)in.cols / (float)out_size.w; 116 152 // clang-format off 117 - ret(0,0) = scale_from_out_to_in; ret(0,1) = 0.0f; ret(0,2) = 0.0f; 118 - ret(1,0) = 0.0f; ret(1,1) = scale_from_out_to_in; ret(1,2) = 0.0f; 153 + ret(0,0) = scale_from_out_to_in; ret(0,1) = 0.0f; ret(0,2) = 0.0f; 154 + ret(1,0) = 0.0f; ret(1,1) = scale_from_out_to_in; ret(1,2) = 0.0f; 119 155 // clang-format on 120 156 return ret; 121 157 } 122 158 assert(!"Uh oh! Unimplemented!"); 123 159 return {}; 160 + #endif 124 161 } 125 162 126 163 /*!