| 188 | } |
| 189 | |
| 190 | void Classifier::Preprocess(const cv::Mat& img, std::vector<cv::Mat>* input_channels) |
| 191 | { |
| 192 | /* Convert the input image to the input image format of the network. */ |
| 193 | cv::Mat sample; |
| 194 | if(img.channels() == 3 && num_channels_ == 1) |
| 195 | cv::cvtColor(img, sample, cv::COLOR_BGR2GRAY); |
| 196 | else if(img.channels() == 4 && num_channels_ == 1) |
| 197 | cv::cvtColor(img, sample, cv::COLOR_BGRA2GRAY); |
| 198 | else if(img.channels() == 4 && num_channels_ == 3) |
| 199 | cv::cvtColor(img, sample, cv::COLOR_BGRA2BGR); |
| 200 | else if(img.channels() == 1 && num_channels_ == 3) |
| 201 | cv::cvtColor(img, sample, cv::COLOR_GRAY2BGR); |
| 202 | else |
| 203 | sample = img; |
| 204 | |
| 205 | cv::Mat sample_resized; |
| 206 | if(sample.size() != input_geometry_) |
| 207 | cv::resize(sample, sample_resized, input_geometry_); |
| 208 | else |
| 209 | sample_resized = sample; |
| 210 | |
| 211 | cv::Mat sample_float; |
| 212 | if(num_channels_ == 3) |
| 213 | sample_resized.convertTo(sample_float, CV_32FC3); |
| 214 | else |
| 215 | sample_resized.convertTo(sample_float, CV_32FC1); |
| 216 | |
| 217 | #ifdef MOBILE_NET |
| 218 | cv::Mat sample_normalized, sample_normalized1; |
| 219 | cv::subtract(sample_float, mean_, sample_normalized1); |
| 220 | cv::multiply(sample_normalized1, 0.017, sample_normalized); |
| 221 | #else |
| 222 | cv::Mat sample_normalized; |
| 223 | cv::subtract(sample_float, mean_, sample_normalized); |
| 224 | #endif |
| 225 | |
| 226 | /* This operation will write the separate BGR planes directly to the |
| 227 | * input layer of the network because it is wrapped by the cv::Mat |
| 228 | * objects in input_channels. */ |
| 229 | cv::split(sample_normalized, *input_channels); |
| 230 | |
| 231 | CHECK(reinterpret_cast<float*>(input_channels->at(0).data) == net_->input_blobs()[0]->cpu_data()) |
| 232 | << "Input channels are not wrapping the input layer of the network."; |
| 233 | } |
| 234 | |
| 235 | int main(int argc, char** argv) |
| 236 | { |