173 lines
8.1 KiB
C++
173 lines
8.1 KiB
C++
#include "CImageMerge.h"
|
|
#include <sstream>
|
|
|
|
CImageMerge::CImageMerge()
|
|
{
|
|
}
|
|
CImageMerge::~CImageMerge()
|
|
{
|
|
}
|
|
|
|
cv::Mat CImageMerge::MergeImage(cv::Mat &srcMat, int dstwidth, int dstheight, int mode, bool b7010)
|
|
{
|
|
if (b7010)
|
|
{
|
|
cv::Mat matdst;
|
|
if (!mode) // gray mode
|
|
{
|
|
matdst = srcMat;
|
|
}
|
|
else
|
|
{
|
|
// To do: merge color image, notice image rgb channels order,
|
|
// opencv Mat default channels order is B G R
|
|
|
|
/*TEMP MERGE ALGORITHM*/
|
|
int width = srcMat.cols / 3;
|
|
int height = srcMat.rows;
|
|
cv::Mat m_dst(height, width, CV_8UC(3));
|
|
std::vector<cv::Mat> m_items;
|
|
std::vector<int> index = {0, 2, 1};
|
|
for (size_t i = 0; i < 3; i++)
|
|
{
|
|
cv::Mat t_item(srcMat(cv::Rect(width * index[i], 0, width, height)));
|
|
m_items.push_back(t_item);
|
|
}
|
|
cv::merge(m_items, m_dst);
|
|
matdst = m_dst;
|
|
}
|
|
return matdst;
|
|
}
|
|
else
|
|
{
|
|
cv::Mat dst(srcMat.rows, srcMat.cols / (mode == 0 ? 1 : 3), CV_8UC(mode == 0 ? 1 : 3));
|
|
auto graymerge = [](cv::Mat &src, cv::Mat dst) -> cv::Mat
|
|
{
|
|
int width_block = src.cols / 12;
|
|
int heigh_block = src.rows;
|
|
|
|
// 20 vsp 拼接算法
|
|
// src(cv::Rect(width_block,0,width_block*2,heigh_block)).copyTo(dst(cv::Rect(0,0,width_block*2,heigh_block)));
|
|
// src(cv::Rect(0,0,width_block,heigh_block)).copyTo(dst(cv::Rect(width_block*2,0,width_block,heigh_block)));
|
|
// src(cv::Rect(width_block*3,0,width_block*2,heigh_block)).copyTo(dst(cv::Rect(width_block*4,0,width_block*2,heigh_block)));
|
|
// src(cv::Rect(width_block*5,0,width_block,heigh_block)).copyTo(dst(cv::Rect(width_block*3,0,width_block,heigh_block)));
|
|
|
|
// src(cv::Rect(width_block*7,0,width_block*2,heigh_block)).copyTo(dst(cv::Rect(width_block*6,0,width_block*2,heigh_block)));
|
|
// src(cv::Rect(width_block*6,0,width_block,heigh_block)).copyTo(dst(cv::Rect(width_block*8,0,width_block,heigh_block)));
|
|
// src(cv::Rect(width_block*9,0,width_block*2,heigh_block)).copyTo(dst(cv::Rect(width_block*10,0,width_block*2,heigh_block)));
|
|
// src(cv::Rect(width_block*11,0,width_block,heigh_block)).copyTo(dst(cv::Rect(width_block*9,0,width_block,heigh_block)));
|
|
// return dst;
|
|
|
|
// 45 vsp 拼接算法
|
|
src(cv::Rect(0, 0, width_block * 2, heigh_block)).copyTo(dst(cv::Rect(width_block, 0, width_block * 2, heigh_block)));
|
|
src(cv::Rect(width_block * 2, 0, width_block, heigh_block)).copyTo(dst(cv::Rect(0, 0, width_block, heigh_block)));
|
|
src(cv::Rect(width_block * 3, 0, width_block, heigh_block)).copyTo(dst(cv::Rect(width_block * 5, 0, width_block, heigh_block)));
|
|
src(cv::Rect(width_block * 4, 0, width_block * 2, heigh_block)).copyTo(dst(cv::Rect(width_block * 3, 0, width_block * 2, heigh_block)));
|
|
|
|
src(cv::Rect(width_block * 6, 0, width_block * 2, heigh_block)).copyTo(dst(cv::Rect(width_block * 7, 0, width_block * 2, heigh_block)));
|
|
src(cv::Rect(width_block * 8, 0, width_block, heigh_block)).copyTo(dst(cv::Rect(width_block * 6, 0, width_block, heigh_block)));
|
|
src(cv::Rect(width_block * 9, 0, width_block, heigh_block)).copyTo(dst(cv::Rect(width_block * 11, 0, width_block, heigh_block)));
|
|
src(cv::Rect(width_block * 10, 0, width_block * 2, heigh_block)).copyTo(dst(cv::Rect(width_block * 9, 0, width_block * 2, heigh_block)));
|
|
return dst;
|
|
};
|
|
// cv::imwrite("org.jpg",srcMat);
|
|
if (!srcMat.empty())
|
|
{
|
|
if (mode == 0) // 灰度模式
|
|
return graymerge(srcMat, dst);
|
|
|
|
std::vector<cv::Mat> ch_mats;
|
|
int blockcnt = 12;
|
|
int spitWidth = srcMat.cols / blockcnt;
|
|
cv::Mat retMat(srcMat.rows, srcMat.cols / 3, CV_8UC3);
|
|
for (int i = 0; i < 4; i++)
|
|
{
|
|
if (i < 2)
|
|
{
|
|
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 1), 0, spitWidth, srcMat.rows)));
|
|
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 2), 0, spitWidth, srcMat.rows)));
|
|
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 0), 0, spitWidth, srcMat.rows)));
|
|
}
|
|
else
|
|
{
|
|
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 1), 0, spitWidth, srcMat.rows)));
|
|
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 0), 0, spitWidth, srcMat.rows)));
|
|
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 2), 0, spitWidth, srcMat.rows)));
|
|
}
|
|
cv::merge(ch_mats, retMat(cv::Rect(spitWidth * i, 0, spitWidth, srcMat.rows)));
|
|
ch_mats.clear();
|
|
}
|
|
return graymerge(retMat, dst);
|
|
}
|
|
return srcMat;
|
|
}
|
|
}
|
|
|
|
// cv::Mat CImageMerge::MergeImage(bool iscolor, cv::Mat &srcMat, int dstwidth, int dstheight)
|
|
// {
|
|
// {
|
|
// int blockcnt = 12;
|
|
// int spitWidth = srcMat.cols / blockcnt;
|
|
// int abortwidth; // = spitWidth == 3888 ? 432 : (spitWidth == 2592 ? 216 : 144);
|
|
// if (!iscolor) // 灰度
|
|
// {
|
|
// abortwidth = spitWidth == 1296 ? 432 : (spitWidth == 648 ? 216 : 144);
|
|
// }
|
|
// else
|
|
// {
|
|
// abortwidth = spitWidth == 3888 ? 432 : (spitWidth == 1944 ? 216 : 144);
|
|
// }
|
|
|
|
// cv::Mat dst(dstheight, dstwidth - abortwidth * 2, CV_8UC(iscolor ? 3 : 1));
|
|
// if (!iscolor)
|
|
// {
|
|
// for (int i = 0; i < 2; i++)
|
|
// {
|
|
// srcMat(cv::Rect((dstwidth / 2 + abortwidth) * i, 0, dstwidth / 2 - abortwidth, dstheight)).copyTo(dst(cv::Rect(dst.cols / 2 * i, 0, dst.cols / 2, dstheight)));
|
|
// }
|
|
// srcMat.release();
|
|
// return dst;
|
|
// }
|
|
// else
|
|
// {
|
|
// std::vector<cv::Mat> m_splits;
|
|
// std::vector<int> m_index = {1, 4, 7, 10, 2, 5, 6, 9, 0, 3, 8, 11};
|
|
|
|
// for (int i = 0; i < 3; i++)
|
|
// {
|
|
// int startindex = i == 0 ? 0 : (i == 1 ? 4 : 8);
|
|
// cv::Mat t_mat(dstheight, dstwidth - abortwidth * 2, CV_8UC1);
|
|
// srcMat(cv::Rect(spitWidth * m_index[startindex + 0], 0, spitWidth, dstheight)).copyTo(t_mat(cv::Rect(0, 0, spitWidth, dstheight)));
|
|
// srcMat(cv::Rect(spitWidth * m_index[startindex + 1], 0, spitWidth - abortwidth, dstheight)).copyTo(t_mat(cv::Rect(spitWidth, 0, spitWidth - abortwidth, dstheight)));
|
|
// srcMat(cv::Rect(spitWidth * m_index[startindex + 2] + abortwidth, 0, spitWidth - abortwidth, dstheight)).copyTo(t_mat(cv::Rect(spitWidth * 2 - abortwidth, 0, spitWidth - abortwidth, dstheight)));
|
|
// srcMat(cv::Rect(spitWidth * m_index[startindex + 3], 0, spitWidth, dstheight)).copyTo(t_mat(cv::Rect(spitWidth * 3 - abortwidth * 2, 0, spitWidth, dstheight)));
|
|
// m_splits.push_back(t_mat);
|
|
// }
|
|
// cv::merge(m_splits, dst);
|
|
// m_splits.clear();
|
|
// }
|
|
// srcMat.release();
|
|
// return dst;
|
|
// }
|
|
// }
|
|
|
|
cv::Mat CImageMerge::MergeFrames(std::vector<cv::Mat> frames, int type, int dpi)
|
|
{
|
|
if (frames.empty())
|
|
return cv::Mat();
|
|
|
|
int size = frames.size();
|
|
auto it = frames.begin();
|
|
int dstH = it->rows * size;
|
|
int abortwidth = dpi == 0x02 || dpi == 0x01 ? 432 : 864; // 216*2 432*2
|
|
int dstwidth = dpi == 0x02 || dpi == 0x01 ? 7344 : 14688;
|
|
cv::Mat matdst(dstH, dstwidth, CV_8UC(type == 1 ? 3 : 1));
|
|
for (int i = 0; i < size; i++)
|
|
{
|
|
// auto itemimg = frames[i];
|
|
// itemimg = MergeImage(type, itemimg, dstwidth + abortwidth, itemimg.rows);
|
|
// itemimg.copyTo(matdst(cv::Rect(0, itemimg.rows * i, itemimg.cols, itemimg.rows)));
|
|
// itemimg.release();
|
|
}
|
|
return matdst;
|
|
} |