在用c++部署测试mobilenet的时候,需要把Mat型图像转成TYPE*进行图像预处理,现在记录一下学习的过程:
opencv的Mat型与TYPE*的相处转换:
Mat转TYPE*
// 将cv::Mat转换为byte*类型
BYTE* matToBytePointer(const cv::Mat& image) {
BYTE* imageData = new BYTE[image.rows * image.cols * image.channels()];
memcpy(imageData, image.data, image.rows * image.cols * image.channels());
return imageData;
}
TYPE*转Mat
cv::Mat bytePointerToMat(BYTE* imageData, int width, int height, int channels) {
return cv::Mat(height, width, CV_MAKETYPE(CV_8U, channels), imageData);
}
c++实现TYPE*类型的图像的letterbox填充方法:
void preprocess_letterbox_myresize(BYTE* imageData, int width, int height, int channels, int newWidth, int newHeight, float out_data[]) {
// 计算缩放的比例
float x_r = static_cast<float>(width) / newWidth;
float y_r = static_cast<float>(height) / newHeight;
// 根据较长的边调整size,等比例缩放
int H, W;
if (x_r < y_r) {
H = newHeight;
W = (width * 1.0) / y_r;
}
else {
H = height / x_r;
W = newWidth;
}
// 用于存放resize之后的图像
BYTE* resizedImageData = new BYTE[W * H * channels];
// 最近邻插值resize
float x_ratio = static_cast<float>(width) / W;
float y_ratio = static_cast<float>(height) / H;
int offset = 0;
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
int x = static_cast<int>(x_ratio * j + 0.5);
int y = static_cast<int>(y_ratio * i + 0.5);
int index = (y * width + x) * channels;
for (int c = 0; c < channels; c++) {
resizedImageData[offset++] = imageData[index];
index++;
}
}
}
// BYTE*转Mat,显示图像
cv::Mat outImg2 = bytePointerToMat(resizedImageData, W, H, 3);
//cv::imshow("Image", outImg2);
//cv::waitKey(0);
// 用于存放填充之后的图像
BYTE* img_letterbox = new BYTE[newWidth * newHeight * channels];
// 计算填充的范围
int d = (newWidth - W) / 2;
// letterbox进行填充
int offset1 = 0;
for (int h = 0; h < newHeight; h++) {
for (int w = 0; w < newWidth; w++) {
if ((w - d) < 0 || w > W + d - 1) {
for (int c = 0; c < channels; c++) {
img_letterbox[offset1++] = 114;
}
}
else {
int x = w - d;
int y = h;
int index = (y * W + x + 1) * channels - 1; // 同时进行BGR->RGB
for (int c = 0; c < channels; c++) {
img_letterbox[offset1++] = resizedImageData[index];
index--;
}
}
}
}
cv::Mat outImg4 = bytePointerToMat(img_letterbox, newWidth, newHeight, 3);
//cv::imshow("Image", outImg4);
//cv::waitKey(0);
// 将BYTE*转float,并进行标准化
int m = 0;
for (int i = 0; i < newWidth * newHeight * channels - 2; i = i + 3 ) {
out_data[m] = (static_cast<float>(img_letterbox[i]) - 128.0) / 256.0;
out_data[m + newWidth * newHeight] = (static_cast<float>(img_letterbox[i+1]) - 128.0) / 256.0;
out_data[m + 2 * (newWidth * newHeight)] = (static_cast<float>(img_letterbox[i+2]) - 128.0) / 256.0;
m++;
}
delete[] resizedImageData; // 释放内存
delete[] img_letterbox;
}
最后得到的float型直接送入到mobilenet模型里面进行推理。