第1种 float* KDAI::blobFromImage(cv::Mat& img) { ?? ?float* blob = new float[img.total() * 3]; ?? ?int channels = 3; ?? ?int img_h = img.rows; ?? ?int img_w = img.cols; ?? ?for (size_t c = 0; c < channels; c++) ?? ?{ ?? ??? ?for (size_t h = 0; h < img_h; h++) ?? ??? ?{ ?? ??? ??? ?for (size_t w = 0; w < img_w; w++) ?? ??? ??? ?{ ?? ??? ??? ??? ?blob[c * img_w * img_h + h * img_w + w] = ?? ??? ??? ??? ??? ?(float)img.at<cv::Vec3b>(h, w)[c]; ?? ??? ??? ?} ?? ??? ?} ?? ?} ?? ?return blob; }
// 第2种 float* KDAI::blobFromImage(cv::Mat& img) { ?? ?float* blob = new float[img.total() * img.channels()]; ?? ?cv::Mat blobImg = cv::dnn::blobFromImage(img); ?? ?memcpy(blob, blobImg.data, blobImg.total() * blobImg.channels() * sizeof(float)); ?? ?return blob; }
//第3种 float* KDAI::blobFromImage(cv::Mat& img) { ?? ?std::vector<float> dst_data; ?? ?std::vector<cv::Mat> bgrChannels(3); ?? ?cv::split(img, bgrChannels); ?? ?for (auto i = 0; i < bgrChannels.size(); i++) ?? ?{ ?? ??? ?std::vector<float> data = std::vector<float>(bgrChannels[i].reshape(1, 1)); ?? ??? ?dst_data.insert(dst_data.end(), data.begin(), data.end()); ?? ?} ?? ?float* blob = new float[dst_data.size()]; ?? ?memcpy(blob, dst_data.data(), sizeof(float) * dst_data.size()); ?? ?return blob; }
// 第4种 float* KDAI::blobFromImage(cv::Mat& img) { ?? ?float* blob = new float[img.total() * 3]; ?? ?int channels = 3; ?? ?int img_h = img.rows; ?? ?int img_w = img.cols; ?? ?for (size_t h = 0; h < img_h; h++) ?? ?{ ?? ??? ?for (size_t w = 0; w < img_w; w++) ?? ??? ?{ ?? ??? ??? ?blob[0 * img_w * img_h + h * img_w + w] =(float)img.at<cv::Vec3b>(h, w)[0]; ?? ??? ??? ?blob[1 * img_w * img_h + h * img_w + w] = (float)img.at<cv::Vec3b>(h, w)[1]; ?? ??? ??? ?blob[2 * img_w * img_h + h * img_w + w] = (float)img.at<cv::Vec3b>(h, w)[2]; ?? ??? ?} ?? ?} ?? ?return blob; }
|