C/C++小功能的函数模板

C++

获取当前时间并返回字符串

#include <iostream>
#include <unistd.h>
#include <chrono>
#include <iomanip>
#include <string>

// 获取当前时间,返回字符串
std::string CurrentTimeString()
{
	// 获取当前时间
	auto now = std::chrono::system_clock::now();

	// 将时间转换为time_t
	std::time_t currentTime = std::chrono::system_clock::to_time_t(now);

	// 格式化时间为字符串
	std::stringstream ss;
	ss << std::put_time(std::localtime(&currentTime), "%Y-%m-%d %H:%M:%S");

	std::string formattedTime = ss.str();
	return formattedTime;
}

C

在YUV420图像上绘制包围盒

int DrawGreenRectOnYuv420p( uint8_t *yuv, int width, int height, int x, int y, int w, int h) {
    uint8_t* yuv_y = yuv;
    uint8_t* yuv_u = yuv_y + width * height;
    uint8_t* yuv_v = yuv_u + width * height / 4;

    int y_x = x, y_y = y;
    int y_w = w, y_h = h;
    int uv_x = x /2, uv_y = y / 2;
    int uv_w = w /2, uv_h = h / 2;

    // 处理Y
    for (int i = y_y; i < y_y + y_h; i++) {
        uint8_t *ptr = yuv_y + i * width;
        ptr[y_x] = ptr[y_x + y_w] = 128;
    }
    for (int j = y_x; j < y_x + y_w; j++) {
        uint8_t *ptr = yuv_y + y_y * width;
        ptr[j] = 128;
        ptr          = yuv_y + (y_y + y_h) * width;
        ptr[j] = 128;
    }

    // 处理U
    for (int i = uv_y; i < uv_y + uv_h; i++) {
        uint8_t *ptr = yuv_u + i * width / 2;
        ptr[uv_x] = ptr[uv_x + uv_w] = 0;
    }
    for (int j = uv_x; j < uv_x + uv_w; j++) {
        uint8_t *ptr = yuv_u + uv_y * width / 2;
        ptr[j] = 0;
        ptr          = yuv_u + (uv_y + uv_h) * width / 2;
        ptr[j] = 0;
    }

    // 处理V
    for (int i = uv_y; i < uv_y + uv_h; i++) {
        uint8_t *ptr = yuv_v + i * width / 2;
        ptr[uv_x] = ptr[uv_x + uv_w] = 0;
    }
    for (int j = uv_x; j < uv_x + uv_w; j++) {
        uint8_t *ptr = yuv_v + uv_y * width / 2;
        ptr[j] = 0;
        ptr          = yuv_v + (uv_y + uv_h) * width / 2;
        ptr[j] = 0;
    }

    return 0;
}

在YUV420SP图像上绘制包围盒和实心矩形

static void DrawGreenRectOnYUV420SP(uint8_t *yuv, int width, int height, int x, int y, int w, int h) {
  // 处理Y
  for (int i = y; i < y + h; i++) {
    uint8_t *ptr = yuv + i * width;
    ptr[x] = ptr[x + w] = 128;
  }
  for (int j = x; j < x + w; j++) {
    uint8_t *ptr = yuv + y * width;
    ptr[j] = 128;
    ptr          = yuv + (y + h) * width;
    ptr[j] = 128;
  }

  // UV plane (packed UV)
  int uvBaseIndex = width * height;
  int uvWidth = width / 2;  // UV components have half the width
  int uvHeight = height / 2;  // UV components have half the height

  for (int i = y / 2; i < (y + h) / 2; i++) {
    yuv[uvBaseIndex + 2 * i * uvWidth + x] = 0;  // U component
    yuv[uvBaseIndex + 2 * i * uvWidth + x + 1] = 0;  // V component
    yuv[uvBaseIndex + 2 * i * uvWidth + x + w] = 0;  // U component
    yuv[uvBaseIndex + 2 * i * uvWidth + x + w + 1] = 0;  // V component
  }
  for (int j = x / 2; j < (x + w) / 2; j++) {
    yuv[uvBaseIndex + (y) * uvWidth + 2 * j] = 0;  // U component
    yuv[uvBaseIndex + (y) * uvWidth + 2 * j + 1] = 0;  // V component
    yuv[uvBaseIndex + (y + h) * uvWidth + 2 * j] = 0;  // U component
    yuv[uvBaseIndex + (y + h) * uvWidth + 2 * j + 1] = 0;  // V component
  }
}
static void DrawMaskYUV420SP(uint8_t *yuv, int width, int height, int x, int y, int rectWidth, int rectHeight) {
  // Y plane
  for (int i = y; i < y + rectHeight; i++) {
    for (int j = x; j < x + rectWidth; j++) {
      yuv[i * width + j] = 99;  // Y component
    }
  }

  // UV plane (packed UV)
  int uvBaseIndex = width * height;
  int uvWidth = width / 2;  // UV components have half the width
  int uvHeight = height / 2;  // UV components have half the height

  for (int i = y / 2; i < (y + rectHeight) / 2; i++) {
    for (int j = x / 2; j < (x + rectWidth) / 2; j++) {
      yuv[uvBaseIndex + 2 * i * uvWidth + 2 * j] = 128;  // U component
      yuv[uvBaseIndex + 2 * i * uvWidth + 2 * j + 1] = 128;  // V component
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值