MCPcopy Create free account
hub / github.com/apache/singa / resize

Function resize

src/io/image_transformer.cc:87–198  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

85
86#ifdef USE_OPENCV
87Tensor resize(Tensor& input, const size_t resize_height,
88 const size_t resize_width, const string& image_dim_order) {
89 CHECK_LE(input.nDim(), 4u);
90 CHECK_GE(input.nDim(), 2u);
91 if (!resize_height || !resize_width) return input;
92 Tensor output;
93 cv::Mat mat;
94 const auto* in = input.data<float>();
95 if (input.nDim() == 4u) {
96 /// TODO
97 /// batch based resize
98 LOG(FATAL) << "Not implemented";
99 } else if (input.nDim() == 3u) {
100 if (image_dim_order == "CHW") {
101 size_t height = input.shape(1), width = input.shape(2),
102 channel = input.shape(0);
103 if (channel == 3u) {
104 mat = cv::Mat(height, width, CV_32FC3, cv::Scalar(0, 0, 0));
105 for (size_t i = 0; i < height; i++)
106 for (size_t j = 0; j < width; j++)
107 for (size_t k = 0; k < channel; k++)
108 mat.at<cv::Vec3f>(i, j)[k] = in[k * height * width + i * width + j];
109 } else if (channel == 1u) {
110 mat = cv::Mat(height, width, CV_32FC1);
111 for (size_t i = 0; i < height; i++)
112 for (size_t j = 0; j < width; j++)
113 mat.at<cv::Vec<float, 1>>(i, j)[0] = in[i * width + j];
114 } else LOG(FATAL) << "Invalid channel size: " << channel;
115 } else if (image_dim_order == "HWC") {
116 size_t height = input.shape(0), width = input.shape(1),
117 channel = input.shape(2);
118 if (channel == 3u) {
119 mat = cv::Mat(height, width, CV_32FC3, cv::Scalar(0, 0, 0));
120 for (size_t i = 0; i < height; i++)
121 for (size_t j = 0; j < width; j++)
122 for (size_t k = 0; k < channel; k++)
123 mat.at<cv::Vec3f>(i, j)[k] =
124 in[i * width * channel + j * channel + k];
125 } else if (channel == 1u) { /// 2D gray image
126 mat = cv::Mat(height, width, CV_32FC1);
127 for (size_t i = 0; i < height; i++)
128 for (size_t j = 0; j < width; j++)
129 mat.at<cv::Vec<float, 1>>(i, j)[0] = in[i * width + j];
130 } else LOG(FATAL) << "Invalid channel size: " << channel;
131 } else {
132 LOG(FATAL) << "Unknow dimension order for images " << image_dim_order
133 << " Only support 'HWC' and 'CHW'";
134 }
135 } else { /// 2D gray image
136 size_t height = input.shape(0), width = input.shape(1);
137 mat = cv::Mat(height, width, CV_32FC1);
138 for (size_t i = 0; i < height; i++)
139 for (size_t j = 0; j < width; j++)
140 mat.at<cv::Vec<float, 1>>(i, j)[0] = in[i * width + j];
141 }
142 cv::Size size(resize_width, resize_height);
143 cv::Mat resized;
144 cv::resize(mat, resized, size);

Callers 1

ApplyMethod · 0.70

Calls 4

nDimMethod · 0.80
shapeMethod · 0.80
sizeMethod · 0.45
channelsMethod · 0.45

Tested by

no test coverage detected