Skip to content

Instantly share code, notes, and snippets.

@lix19937
Last active November 15, 2024 02:04
Show Gist options
  • Save lix19937/d6d5261619626017ba8ba219efbf7c1b to your computer and use it in GitHub Desktop.
Save lix19937/d6d5261619626017ba8ba219efbf7c1b to your computer and use it in GitHub Desktop.
opencv hwc2chw and crop_resize
#include "opencv2/opencv.hpp"
void hwc2chw(const cv::Mat &hwc, int h, int w, int c, cv::Mat &chw /* fp32 */)
{
std::vector<cv::Mat> channels;
cv::split(hwc, channels);
int nbytes = h * w;
int size4[4] = {1, c, h, w};
cv::Mat chw_tmp(4, size4, CV_8UC1, cv::Scalar::all(0));
for (int i = 0; i < c; ++i)
{
memcpy((uint8_t *)chw_tmp.data + i * nbytes, channels[i].data, nbytes);
}
chw_tmp.convertTo(chw, CV_32F, 1, 0);
}
void bgra_crop_resize_cv(const cv::Mat &img_bgra /* uint8 */, cv::Mat &chw_fp32)
{
cv::Mat img_bgr;
cv::cvtColor(img_bgra, img_bgr, cv::COLOR_BGRA2BGR);
int roi_x = SOD_ROI_X, roi_y = SOD_ROI_Y, roi_x2 = SOD_ROI_X2, roi_y2 = SOD_ROI_Y2;
int dst_h = SOD_ROI_H, dst_w = SOD_ROI_W;
int rz_h = SOD_NET_IN_H, rz_w = SOD_NET_IN_W, c = SOD_NET_IN_C;
/// do crop
auto roi = cv::Rect(roi_x, roi_y, dst_w /* w */, dst_h /* h */);
cv::Mat img_bgr_crop = img_bgr(roi);
/// do resize
cv::Mat img_bgr_hwc;
cv::resize(img_bgr_crop, img_bgr_hwc, cv::Size(rz_w, rz_h), 0, 0, cv::INTER_LINEAR);
/// debug
/// hwc-uint8 to chw-float32
hwc2chw(img_bgr_hwc, rz_h, rz_w, c, chw_fp32);
}
void bgra_crop_resize_cv(const void *src /* uint8 */, void *dst /* float */, cudaStream_t stream)
{
int h = SOD_PREPROCESS_IN_H, w = SOD_PREPROCESS_IN_W;
int roi_x = SOD_ROI_X, roi_y = SOD_ROI_Y, roi_x2 = SOD_ROI_X2, roi_y2 = SOD_ROI_Y2;
int dst_h = SOD_ROI_H, dst_w = SOD_ROI_W;
int rz_h = SOD_NET_IN_H, rz_w = SOD_NET_IN_W, c = SOD_NET_IN_C, batch = SOD_PREPROCESS_IN_BATCH;
int src_nbytes = h * w * 4 * sizeof(uint8_t); // bgra-uint8
int rz_nbytes = rz_h * rz_w * c * sizeof(float); // bgr-float32
for (int i = 0; i < batch; ++i)
{
cv::Mat img_bgra(h, w, CV_8UC4, cv::Scalar::all(0));
cv::Mat chw_fp32;
cudaMemcpyAsync(img_bgra.data, (uint8_t *)src + i * src_nbytes, src_nbytes, ::cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
bgra_crop_resize_cv(img_bgra /* uint8 */, chw_fp32);
// cv::FileStorage fs((std::to_string(i) + ".xml").c_str(), cv::FileStorage::WRITE);
// fs << "img" << chw_fp32;
// fs.release();
cudaMemcpyAsync((uint8_t *)dst + i * rz_nbytes, chw_fp32.data, rz_nbytes, ::cudaMemcpyHostToDevice, stream);
cudaStreamSynchronize(stream);
}
}
/// for show
void debug_out(const float *src, cudaStream_t stream)
{
#define SOD_NET_OUT_ACCUMU (4 * 1 * 5040 * 26)
std::vector<float> h_out(SOD_NET_OUT_ACCUMU);
cudaMemcpyAsync(h_out.data(), (uint8_t *)src, SOD_NET_OUT_ACCUMU * sizeof(float), ::cudaMemcpyDeviceToHost, stream);
cudaStreamSynchronize(stream);
for (int i = 0; i < 4; ++i)
{
for (int j = 0; j < 10; j++)
{
printf("%f ", h_out[i * 5040 * 26 + j]);
}
printf("\n ----- \n\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment