/* 合并多图像矩阵到一个大矩阵上 */
Mat mergeImgMats(vector<Mat> img_mats, int per_width, int per_height, int cols, int split_w) {
int count = img_mats.size();
CV_Assert(count > 0);
// 定义 行
int rows = count % cols == 0 ? count / cols : count / cols + 1;
//准备一个大矩阵
Mat bigMat(per_height * rows + split_w * (rows - 1), per_width*cols + split_w * (cols - 1), img_mats[0].type());
bigMat.setTo(Scalar::all(0));
Mat tmp;
for (size_t i = 0; i < count; i++)
{
resize(img_mats[i], tmp, Size(per_width, per_height));
int location_x = 0, location_y = 0;;
//copy to location
if (i < cols) { //第一行
location_x = i * (per_width + split_w);
location_y = 0;
}
else
{ // 第二行开始
location_x = i % cols * (per_width + split_w);
location_y = i / cols * (per_height + split_w);
}
Mat roi = bigMat(Rect(location_x, location_y, per_width, per_height));
tmp.copyTo(roi);
}
tmp.release();
return bigMat;
}
int BGR2YUV420P(Mat img, uint8_t *yuv_data, int w, int h) {
assert(w % 2 == 0 && h % 2 == 0);
Mat yuv;
cvtColor(img, yuv, CV_BGR2YUV_I420); //CV_BGR2YUV_I420
Mat Y = yuv(Rect(0, 0, w, h));
Mat U = yuv(Rect(0, h, w / 2, h / 2));
Mat V = yuv(Rect(w / 2, h, w / 2, h / 2));
memcpy_s(yuv_data, w * h, Y.data, h * w);
for (size_t i = 0; i < h / 2; i++)
{
for (size_t j = 0; j < w; j++)
{
int index = i * w + j;
if (j < 2 / w) yuv_data[w*h + index] = U.data[index];
else yuv_data[w*h + index] = V.data[index - w / 2];
}
}
yuv.release();
return 0;
}
# YUV --> AVI
# 只适用于YUV格式数据直接存储的视频文件
int YuvFile_to_AviFile(string &yuv_filename, int src_img_w, int src_img_h, string &avi_filenanme, int dst_img_w, int dst_img_h) {
int img_w = src_img_w, img_h = src_img_h;
int scale_w = dst_img_w, scale_h = dst_img_h;
int yuv_size = img_w * img_h * 3 / 2;
FILE *f = NULL;
uint8_t *yuv_data = new uint8_t[yuv_size];
auto code = fopen_s(&f, yuv_filename.c_str(), "rb");
assert(code == 0);
Mat img(img_h, img_w, CV_8UC3);
Mat yuvFrame(img_h * 3 / 2, img_w, CV_8UC1, yuv_data);
VideoWriter newcw;
auto flag = newcw.open(avi_filenanme, CV_FOURCC('M', 'P', '4', '2'), 25.0, Size(scale_w, scale_h));
assert(true == flag);
while (1)
{
auto _size = fread_s(yuv_data, yuv_size, 1, yuv_size, f);
if (_size < yuv_size) {
break;
}
cvtColor(yuvFrame, img, CV_YUV420p2RGB);
assert(img.empty() == false && img.cols > 0);
if (scale_w != img_w || scale_h != img_h) {
resize(img, img, Size(scale_w, scale_h));
}
newcw.write(img);
}
yuvFrame.release();
img.release();
delete[] yuv_data;
fclose(f);
newcw.release();
return 0;
}
# 编码
static unsigned char* img2Encode(Mat src, int& dataLength) {
vector<unsigned char> inImage;
imencode(".jpg", src, inImage);
size_t datalen = inImage.size();
unsigned char *outImage = new unsigned char[datalen];
for (int i = 0; i < datalen; i++)
{
outImage[i] = inImage[i];
}
inImage.clear();
dataLength = datalen;
return outImage;
}
# 解码
static Mat img2Decode(unsigned char *inImage, int dataLength) {
vector<unsigned char> buff;
for (int i = 0; i < dataLength; i++)
{
buff.push_back(inImage[i]);
}
Mat mat = imdecode(buff, CV_LOAD_IMAGE_COLOR);
buff.clear();
return mat;
}