rk3399_arm_lvds/capimage/CImageMerge.cpp

302 lines
13 KiB
C++

#include "CImageMerge.h"
#include <vector>
CImageMerge::CImageMerge()
{
}
CImageMerge::~CImageMerge()
{
}
cv::Mat CImageMerge::MergeImage(cv::Mat &srcMat, int dstwidth, int dstheight)
{
cv::Mat retMat(dstheight, dstwidth, CV_8UC3);
if (!srcMat.empty())
{
std::vector<cv::Mat> ch_mats;
int blockcnt = 12;
int spitWidth = srcMat.cols / blockcnt;
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + j), 0, spitWidth, dstheight)));
}
cv::merge(ch_mats, retMat(cv::Rect(spitWidth * i, 0, spitWidth, dstheight)));
ch_mats.clear();
}
return retMat;
}
}
cv::Mat CImageMerge::MergeImage(bool iscolor, cv::Mat &srcMat, int dstwidth, int dstheight, unsigned int fgpaversion)
{
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);
}
if (fgpaversion == 0x00090002)
{
int blockcnt = iscolor ? 18 : 6;
int spitWidth = srcMat.cols / blockcnt / 2; // 一面的灰度图像宽度
int abortwidth = spitWidth == 1296 ? 432 : (spitWidth == 648 ? 216 : 144);
cv::Mat dst(dstheight, dstwidth - 2 * abortwidth, CV_8UC(iscolor ? 3 : 1));
if (!iscolor)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 6; j++)
{
int index = i == 0 ? 5 : 11;
int bandindex = (i * 6 + j);
if (bandindex == 0 || bandindex == 11)
{
if (bandindex == 0)
srcMat(cv::Rect(bandindex * spitWidth, 0, spitWidth - abortwidth, srcMat.rows)).copyTo(dst(cv::Rect(spitWidth * (index - j), 0, spitWidth - abortwidth, srcMat.rows)));
else
srcMat(cv::Rect(bandindex * spitWidth + abortwidth, 0, spitWidth - abortwidth, srcMat.rows)).copyTo(dst(cv::Rect(spitWidth * (index - j) - abortwidth, 0, spitWidth - abortwidth, srcMat.rows)));
}
else
{
if (bandindex >= 6)
srcMat(cv::Rect(bandindex * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(dst(cv::Rect(spitWidth * (index - j) - 2 * abortwidth, 0, spitWidth, srcMat.rows)));
else
srcMat(cv::Rect(bandindex * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(dst(cv::Rect(spitWidth * (index - j), 0, spitWidth, srcMat.rows)));
}
}
}
srcMat.release();
}
else
{
std::vector<cv::Mat> m_splits = {
cv::Mat(dstheight, dstwidth - 2 * abortwidth, CV_8UC1),
cv::Mat(dstheight, dstwidth - 2 * abortwidth, CV_8UC1),
cv::Mat(dstheight, dstwidth - 2 * abortwidth, CV_8UC1)};
int item_index[3][4] = {
{11, 2, 35, 26}, // R
{17, 8, 29, 20}, // B
{14, 5, 32, 23} // G
};
for (size_t j = 0; j < 3; j++)
{
// Front
srcMat(cv::Rect((item_index[j][0] - 0) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 0, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][0] - 1) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 1, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][0] - 2) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 2, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][1] - 0) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 3, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][1] - 1) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 4, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][1] - 2) * spitWidth, 0, spitWidth - abortwidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 5, 0, spitWidth - abortwidth, srcMat.rows)));
// Back
srcMat(cv::Rect((item_index[j][2] - 0) * spitWidth + abortwidth, 0, spitWidth - abortwidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 6 - abortwidth, 0, spitWidth - abortwidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][2] - 1) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 7 - 2 * abortwidth, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][2] - 2) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 8 - 2 * abortwidth, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][3] - 0) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 9 - 2 * abortwidth, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][3] - 1) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 10 - 2 * abortwidth, 0, spitWidth, srcMat.rows)));
srcMat(cv::Rect((item_index[j][3] - 2) * spitWidth, 0, spitWidth, srcMat.rows)).copyTo(m_splits[j](cv::Rect(spitWidth * 11 - 2 * abortwidth, 0, spitWidth, srcMat.rows)));
}
cv::merge(m_splits, dst);
m_splits.clear();
}
srcMat.release();
// static int index=0;
// cv::imwrite(std::to_string(++index)+".bmp",dst);
return dst;
}
else if (fgpaversion == 0x00090001)
{
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();
}
else
{
std::vector<cv::Mat> m_splits;
std::vector<int> m_index = {0, 3, 8, 11, 2, 5, 6, 9, 1, 4, 7, 10};
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;
}
else if (fgpaversion == 0x0009000a)
{
auto switchblock = [](std::vector<std::pair<int, int>> v, cv::Mat &src, cv::Mat &dst, int block_width, int block_height, int abortwidth)
{
int copyindex = 0;
for (int i = 0; i < v.size(); i++)
{
if (v[i].second == 5 || v[i].second == 6)
{
src(cv::Rect(v[i].second == 6 ? block_width * v[i].first + abortwidth : block_width * v[i].first, 0, block_width - abortwidth, block_height)).copyTo(dst(cv::Rect(copyindex, 0, block_width - abortwidth, block_height)));
copyindex += block_width - abortwidth;
}
else
{
src(cv::Rect(block_width * v[i].first, 0, block_width, block_height)).copyTo(dst(cv::Rect(copyindex, 0, block_width, block_height)));
copyindex += block_width;
}
}
};
std::vector<std::pair<int, int>> v{{3, 0}, {5, 1}, {4, 2}, {1, 3}, {0, 4}, {2, 5}, {10, 6}, {9, 7}, {11, 8}, {6, 9}, {8, 10}, {7, 11}};
if (!iscolor)
{
int abortwidth = srcMat.cols / 12 == 1296 ? 432 : (srcMat.cols / 12 == 648 ? 216 : 144);
cv::Mat dst(srcMat.rows, srcMat.cols - abortwidth * 2, CV_8UC1);
switchblock(v, srcMat, dst, srcMat.cols / 12, srcMat.rows, abortwidth);
return dst;
}
else
{
cv::Mat retMat(srcMat.rows, srcMat.cols / 3, CV_8UC3);
int spitWidth = srcMat.cols / 12;
for (int i = 0; i < 4; i++)
{
// 1 2 0
std::vector<cv::Mat> ch_mats;
if (i < 2)
{
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)));
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 1), 0, spitWidth, srcMat.rows)));
}
else
{
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)));
ch_mats.push_back(srcMat(cv::Rect(spitWidth * (i * 3 + 1), 0, spitWidth, srcMat.rows)));
}
cv::merge(ch_mats, retMat(cv::Rect(spitWidth * i, 0, spitWidth, srcMat.rows)));
ch_mats.clear();
}
int abortwidth = retMat.cols / 12 == 1296 ? 432 : (retMat.cols / 12 == 648 ? 216 : 144);
cv::Mat dst(retMat.rows, retMat.cols - abortwidth * 2, CV_8UC3);
switchblock(v, retMat, dst, retMat.cols / 12, retMat.rows, abortwidth);
printf(" dst.cols = %d dst.rows = %d dst.channels = %d \n", dst.cols, dst.rows, dst.channels());
return dst;
}
}
else if (fgpaversion == 0x0009000b)
{
int copy_size = 0;
int ratio = iscolor ? 3 : 1;
int spitWidth = srcMat.cols / 12;
int abortwidth = spitWidth == 1296 ? 432 : (spitWidth == 648 ? 216 : 144);
if (iscolor)
abortwidth = spitWidth == 3888 ? 432 : (spitWidth == 1944 ? 216 : 144);
spitWidth /= ratio;
cv::Mat dst(srcMat.rows, srcMat.cols / (iscolor ? 3 : 1) - abortwidth * 2, CV_8UC(iscolor ? 3 : 1));
std::vector<std::pair<int, int>> v = {{spitWidth * (3 * ratio + 1), spitWidth}, {spitWidth * (3 * ratio), spitWidth}, {spitWidth * (3 * ratio + 2), spitWidth}, {spitWidth * 0, spitWidth}, {spitWidth * 2, spitWidth}, {spitWidth * 1, spitWidth - abortwidth}, {spitWidth * (9 * ratio + 1) + abortwidth, spitWidth - abortwidth}, {spitWidth * (9 * ratio), spitWidth}, {spitWidth * (9 * ratio + 2), spitWidth}, {spitWidth * (6 * ratio), spitWidth}, {spitWidth * (6 * ratio + 2), spitWidth}, {spitWidth * (6 * ratio + 1), spitWidth}};
if (!iscolor)
{
for (int i = 0; i < v.size(); i++)
{
srcMat(cv::Rect(v[i].first, 0, v[i].second, srcMat.rows)).copyTo(dst(cv::Rect(copy_size, 0, v[i].second, srcMat.rows)));
copy_size += v[i].second;
}
}
else
{
std::vector<cv::Mat> m_splits;
std::vector<int> m_index_f = {0, 2, 1};
std::vector<int> m_index_b = {2, 0, 1};
for (int j = 0; j < 3; j++)
{
cv::Mat tmp_dst(srcMat.rows, srcMat.cols / (iscolor ? 3 : 1) - abortwidth * 2, CV_8UC1);
for (int i = 0; i < v.size(); i++)
{
if (i > 5)
srcMat(cv::Rect(v[i].first + m_index_b[j] * 3 * spitWidth, 0, v[i].second, srcMat.rows)).copyTo(tmp_dst(cv::Rect(copy_size, 0, v[i].second, srcMat.rows)));
else
srcMat(cv::Rect(v[i].first + m_index_f[j] * 3 * spitWidth, 0, v[i].second, srcMat.rows)).copyTo(tmp_dst(cv::Rect(copy_size, 0, v[i].second, srcMat.rows)));
copy_size += v[i].second;
}
copy_size = 0;
m_splits.push_back(tmp_dst);
}
cv::merge(m_splits, dst);
m_splits.clear();
}
srcMat.release();
return dst;
}
else if (fgpaversion == 0x0009000c)
{
// cv::imwrite("src.bmp",srcMat);
cv::Mat dst(dstheight, dstwidth - abortwidth * 2, CV_8UC(iscolor ? 3 : 1));
int copy_size = 0;
int ratio = iscolor ? 3 : 1;
spitWidth /= ratio;
std::vector<std::pair<int, int>> v = {{spitWidth * (ratio * 3 + 2), spitWidth}, {spitWidth * (ratio * 3 + 1), spitWidth}, {spitWidth * (ratio * 3), spitWidth}, {spitWidth * 2, spitWidth}, {spitWidth, spitWidth}, {0, spitWidth - abortwidth}, {spitWidth * (ratio * 9 + 2) + abortwidth, spitWidth - abortwidth}, {spitWidth * (ratio * 9 + 1), spitWidth}, {spitWidth * (ratio * 9), spitWidth}, {spitWidth * (ratio * 6 + 2), spitWidth}, {spitWidth * (ratio * 6 + 1), spitWidth}, {spitWidth * (ratio * 6), spitWidth}};
if (!iscolor)
{
for (int i = 0; i < v.size(); i++)
{
srcMat(cv::Rect(v[i].first, 0, v[i].second, srcMat.rows)).copyTo(dst(cv::Rect(copy_size, 0, v[i].second, srcMat.rows)));
copy_size += v[i].second;
}
}
else
{
std::vector<cv::Mat> m_splits;
std::vector<int> m_index_f = {0, 2, 1};
std::vector<int> m_index_b = {2, 0, 1};
for (int j = 0; j < 3; j++)
{
cv::Mat tmp_dst(dstheight, dstwidth - abortwidth * 2, CV_8UC1);
for (int i = 0; i < v.size(); i++)
{
if (i > 5)
srcMat(cv::Rect(v[i].first + m_index_b[j] * 3 * spitWidth, 0, v[i].second, srcMat.rows)).copyTo(tmp_dst(cv::Rect(copy_size, 0, v[i].second, srcMat.rows)));
else
srcMat(cv::Rect(v[i].first + m_index_f[j] * 3 * spitWidth, 0, v[i].second, srcMat.rows)).copyTo(tmp_dst(cv::Rect(copy_size, 0, v[i].second, srcMat.rows)));
copy_size += v[i].second;
}
copy_size = 0;
m_splits.push_back(tmp_dst);
}
cv::merge(m_splits, dst);
m_splits.clear();
}
srcMat.release();
return dst;
}
return srcMat;
}