调整uv拼接算法

This commit is contained in:
masayume 2021-08-24 18:17:32 +08:00
parent 317cfad828
commit 67d296fdc9
9 changed files with 212 additions and 131 deletions

View File

@ -263,7 +263,7 @@ void GScanO1003399::config_params(GScanCap& param)
cfg.g400params.pageSize = SupPaperTyps[ps]; cfg.g400params.pageSize = SupPaperTyps[ps];
CSize size; CSize size;
#ifdef G300 #ifdef G300
size = Device::PaperSize().GetPaperSize(TwSS::A4, 200.0f, gcap.paperAlign);//G300 最大支持A4幅面 size = Device::PaperSize().GetPaperSize(TwSS::A4, 200.0f, param.paperAlign);//G300 最大支持A4幅面
#else #else
size = Device::PaperSize().GetPaperSize(param.papertype, 200.0f, param.paperAlign); size = Device::PaperSize().GetPaperSize(param.papertype, 200.0f, param.paperAlign);
#endif// G300 #endif// G300

View File

@ -190,7 +190,7 @@ void GScanO400::open(int vid, int pid)
//m_usb->write_bulk(&status, sizeof(status)); //m_usb->write_bulk(&status, sizeof(status));
//m_usb->read_bulk(&status, sizeof(status)); //m_usb->read_bulk(&status, sizeof(status));
//std::this_thread::sleep_for(std::chrono::milliseconds(20)); //std::this_thread::sleep_for(std::chrono::milliseconds(20));
notifyscan(); //notifyscan();
GetFWVersion(); GetFWVersion();
} }
} }
@ -254,13 +254,15 @@ BOOL GScanO400::IsConnected()
std::string GScanO400::GetFWVersion() std::string GScanO400::GetFWVersion()
{ {
if (m_usb.get() && m_usb->is_connected()) { if (m_usb.get() && m_usb->is_connected()) {
lock_guard< mutex> lock(m_imgLocker); //lock_guard< mutex> lock(m_imgLocker);
if (fwVersion.empty()) { if (fwVersion.empty()) {
fwVersion.resize(10); fwVersion.resize(10);
USBCB cmd = { GET_FW_VERSION,fwVersion.size(),0, }; USBCB cmd = { GET_FW_VERSION,fwVersion.size(),0, };
m_usb->write_bulk(&cmd, sizeof(cmd)); m_usb->write_bulk(&cmd, sizeof(cmd));
std::this_thread::sleep_for(chrono::milliseconds(10));
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
m_usb->read_bulk(&fwVersion[0], fwVersion.size()); m_usb->read_bulk(&fwVersion[0], fwVersion.size());
std::this_thread::sleep_for(chrono::milliseconds(10));
} }
if (fwVersion.substr(0, 2) == "G3" || fwVersion.substr(0, 2) == "G4") if (fwVersion.substr(0, 2) == "G3" || fwVersion.substr(0, 2) == "G4")
is_AndroidOrLinux = false; is_AndroidOrLinux = false;
@ -827,3 +829,4 @@ void GScanO400::Pop_Image()
USBCB usbcb = { POP_IMAGE ,0,0 }; USBCB usbcb = { POP_IMAGE ,0,0 };
m_usb->write_bulk(&usbcb, sizeof(usbcb)); m_usb->write_bulk(&usbcb, sizeof(usbcb));
} }

View File

@ -506,12 +506,19 @@ void ImageMatQueue::proc()
{ {
Mat front = mat(Rect(0, 0, mat.cols / 2, mat.rows)); Mat front = mat(Rect(0, 0, mat.cols / 2, mat.rows));
Mat back = mat(Rect(mat.cols / 2, 0, mat.cols / 2, mat.rows)); Mat back = mat(Rect(mat.cols / 2, 0, mat.cols / 2, mat.rows));
#ifdef UV
mats.push_back(scanParam.is_switchfrontback ? front : back);
mats.push_back(scanParam.is_switchfrontback ? back : front);
#else
if (scanParam.imageRotateDegree != 0.0 && scanParam.imageRotateDegree != 180.0) { if (scanParam.imageRotateDegree != 0.0 && scanParam.imageRotateDegree != 180.0) {
cv::flip(front, front, 0); cv::flip(front, front, 0);
cv::flip(front, front, 1); cv::flip(front, front, 1);
} }
mats.push_back(scanParam.is_switchfrontback ? back : front); mats.push_back(back);
mats.push_back(scanParam.is_switchfrontback ? front : back); mats.push_back(front);
#endif
front.release();
back.release();
remove(info.path.c_str()); remove(info.path.c_str());
} }
else else
@ -528,8 +535,8 @@ void ImageMatQueue::proc()
{ {
Mat front = matuv(Rect(0, 0, mat.cols / 2, mat.rows)); Mat front = matuv(Rect(0, 0, mat.cols / 2, mat.rows));
Mat back = matuv(Rect(mat.cols / 2, 0, mat.cols / 2, mat.rows)); Mat back = matuv(Rect(mat.cols / 2, 0, mat.cols / 2, mat.rows));
uvmats.push_back(scanParam.is_switchfrontback ? back : front);
uvmats.push_back(scanParam.is_switchfrontback ? front : back); uvmats.push_back(scanParam.is_switchfrontback ? front : back);
uvmats.push_back(scanParam.is_switchfrontback ? back : front);
front.release(); front.release();
back.release(); back.release();
remove(uvinfo.path.c_str()); remove(uvinfo.path.c_str());
@ -564,7 +571,7 @@ void ImageMatQueue::proc()
mats[j].release(); mats[j].release();
break; break;
} }
cv::Mat mergeOrgin_UV = ImageApplyUV::Apply(mats[j], uvmats[j], rects[j], isDesaskew, angleResults.size() > 0 ? angleResults[j] : 0); cv::Mat mergeOrgin_UV = ImageApplyUV::Apply(mats[j], uvmats[j], rects[j], isDesaskew, angleResults.size() > 0 ? angleResults[j] : 0, scanParam.pixtype);
if (!mergeOrgin_UV.empty()) if (!mergeOrgin_UV.empty())
mats[j] = mergeOrgin_UV; mats[j] = mergeOrgin_UV;
} }
@ -581,9 +588,14 @@ void ImageMatQueue::proc()
} }
if (!mats[i].empty()) { if (!mats[i].empty()) {
IMat2Bmp idata; IMat2Bmp idata;
#ifdef UV
if (scanParam.pixtype == 1 && mats[i].channels() == 3)//gray
cv::cvtColor(mats[i], mats[i], COLOR_BGR2GRAY);
#else
if (scanParam.pixtype == 1 && scanParam.hsvcorrect) if (scanParam.pixtype == 1 && scanParam.hsvcorrect)
if (mats[i].channels() == 3) if (mats[i].channels() == 3)
cvtColor(mats[i], mats[i], cv::COLOR_BGR2GRAY); cvtColor(mats[i], mats[i], cv::COLOR_BGR2GRAY);
#endif
idata = (scanParam.pixtype == 0 || (((scanParam.automaticcolortype == 0) && (scanParam.automaticcolor == true)) && (mats[i].channels() == 1))) ? (IMat2Bmp)Mat2BmpBw(mats[i], scanParam.resolution_dst) : Mat2Bmp(mats[i], scanParam.resolution_dst); idata = (scanParam.pixtype == 0 || (((scanParam.automaticcolortype == 0) && (scanParam.automaticcolor == true)) && (mats[i].channels() == 1))) ? (IMat2Bmp)Mat2BmpBw(mats[i], scanParam.resolution_dst) : Mat2Bmp(mats[i], scanParam.resolution_dst);
if (!scanParam.multi_output_red) if (!scanParam.multi_output_red)
mats[i].release(); mats[i].release();

View File

@ -2,6 +2,7 @@
#include <thread> #include <thread>
#include <mutex> #include <mutex>
#include <atomic> #include <atomic>
#include <opencv2/opencv.hpp>
#include "ImageProcess/ImageApplyHeaders.h" #include "ImageProcess/ImageApplyHeaders.h"
#include "PublicFunc.h" #include "PublicFunc.h"
#include "BlockingQueue.h" #include "BlockingQueue.h"
@ -32,7 +33,7 @@ class Mat2Bmp:public IMat2Bmp {
public: public:
Mat2Bmp(const cv::Mat& mat, float res) { Mat2Bmp(const cv::Mat& mat, float res) {
//m_data =std::shared_ptr<std::vector<unsigned char>>(new std::vector<unsigned char>()); //m_data =std::shared_ptr<std::vector<unsigned char>>(new std::vector<unsigned char>());
m_data.reset(new std::vector<unsigned char>()); //m_data.reset(new std::vector<unsigned char>());
int headersize = mat.channels() == 3 ? 54 : 1078; int headersize = mat.channels() == 3 ? 54 : 1078;
int step = mat.step; int step = mat.step;
int bmpdatasize = (step + 3) / 4 * 4 * mat.rows; int bmpdatasize = (step + 3) / 4 * 4 * mat.rows;

View File

@ -170,9 +170,12 @@ void CImageApplyAutoCrop::apply(cv::Mat& pDib, int side)
void CImageApplyAutoCrop::apply(std::vector<cv::Mat>& mats, bool isTwoSide) void CImageApplyAutoCrop::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
{ {
m_rects.clear();
if (mats.empty()) return; if (mats.empty()) return;
if (!mats[0].empty()) { if (!mats[0].empty()) {
apply(mats[0], 0); apply(mats[0], 0);
m_rects.push_back(m_rect);
//cv::imwrite("1.bmp", mats[0]);
} }
if (isTwoSide && mats.size() > 1) if (isTwoSide && mats.size() > 1)
@ -182,6 +185,7 @@ void CImageApplyAutoCrop::apply(std::vector<cv::Mat>& mats, bool isTwoSide)
m_fixedSize = mats[0].size(); m_fixedSize = mats[0].size();
if (!mats[1].empty()) { if (!mats[1].empty()) {
apply(mats[1], 1); apply(mats[1], 1);
m_rects.push_back(m_rect);
} }
if (!mats[0].empty()) if (!mats[0].empty())
@ -243,3 +247,4 @@ uchar CImageApplyAutoCrop::getBackGroudChannelMean(const cv::Mat& gray, int tota
} }
return 255; return 255;
} }

View File

@ -109,3 +109,4 @@ private:
}; };
#endif // !IMAGE_APPLY_AUTO_CROP_H #endif // !IMAGE_APPLY_AUTO_CROP_H

View File

@ -32,7 +32,7 @@ void ImageApplyUV::Apply(cv::Mat& image, const cv::Mat& uv, int dpi, int thresh)
cv::resize(uv, uv_resize, cv::Size(uv.cols * SCA / dpi, uv.rows * SCA / dpi)); cv::resize(uv, uv_resize, cv::Size(uv.cols * SCA / dpi, uv.rows * SCA / dpi));
if (uv_resize.channels() == 3) if (uv_resize.channels() == 3)
cv::cvtColor(uv_resize, uv_resize, cv::COLOR_BGR2GRAY); cv::cvtColor(uv_resize, uv_resize, cv::COLOR_BGR2GRAY);
cv::threshold(uv_resize, uv_resize, 0, 255, THRESH_OTSU); cv::threshold(uv_resize, uv_resize, 150, 255, THRESH_BINARY);
cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(1500 / dpi, 1500 / dpi)); cv::Mat element = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(1500 / dpi, 1500 / dpi));
cv::dilate(uv_resize, uv_resize, element); cv::dilate(uv_resize, uv_resize, element);
std::vector<std::vector<cv::Point>> contours; std::vector<std::vector<cv::Point>> contours;
@ -52,10 +52,10 @@ void ImageApplyUV::Apply(cv::Mat& image, const cv::Mat& uv, int dpi, int thresh)
} }
} }
cv::Mat ImageApplyUV::Apply(const cv::Mat& image, const cv::Mat& uv, const cv::RotatedRect& uvRoi, bool isDesaskew, int angle) cv::Mat ImageApplyUV::Apply(const cv::Mat& image, const cv::Mat& uv, const cv::RotatedRect& uvRoi, bool isDesaskew, int angle, int pixtype)
{ {
static int svindex = 0;
if (uvRoi.size.width == 0) return cv::Mat(); if (uvRoi.size.width == 0) return cv::Mat();
cv::RotatedRect uvRoi_clone = uvRoi; cv::RotatedRect uvRoi_clone = uvRoi;
cv::Mat dst = cv::Mat::zeros(image.rows > image.cols ? image.rows : (image.rows * 2), image.cols > image.rows ? image.cols : (image.cols * 2), image.type()); cv::Mat dst = cv::Mat::zeros(image.rows > image.cols ? image.rows : (image.rows * 2), image.cols > image.rows ? image.cols : (image.cols * 2), image.type());
image.copyTo(dst(cv::Rect(0, 0, image.cols, image.rows))); image.copyTo(dst(cv::Rect(0, 0, image.cols, image.rows)));
@ -67,7 +67,6 @@ cv::Mat ImageApplyUV::Apply(const cv::Mat& image, const cv::Mat& uv, const cv::R
cv::Point2f dstTri[3]; cv::Point2f dstTri[3];
uvRoi_clone.points(srcTri); uvRoi_clone.points(srcTri);
if (angle == 90) if (angle == 90)
{ {
dstTri[0] = cv::Point2f(0, 0); dstTri[0] = cv::Point2f(0, 0);
@ -101,30 +100,88 @@ cv::Mat ImageApplyUV::Apply(const cv::Mat& image, const cv::Mat& uv, const cv::R
cv::cvtColor(uv_temp, dst_uv, cv::COLOR_GRAY2BGR); cv::cvtColor(uv_temp, dst_uv, cv::COLOR_GRAY2BGR);
} }
else else
cv::warpAffine(uv, dst_uv, warp_mat, dst_uv.size()); {
//imwrite("D:\\bfuv" + std::to_string(++svindex) + ".jpg", uv);
cv::Mat matuv;
if (image.channels() == 1 && uv.channels() == 3)
{
cvtColor(uv, matuv, COLOR_BGR2GRAY);
if (pixtype == 0)//¶þֵͼ
cv::threshold(matuv, matuv, 150, 255, THRESH_OTSU);
}
else
{
matuv = uv;
}
cv::warpAffine(matuv, dst_uv, warp_mat, dst_uv.size());
//dst_uv.copyTo(dst(cv::Rect(image.rows > image.cols ? image.cols : 0, image.rows > image.cols ? 0 : image.rows, dst_uv.cols, dst_uv.rows)));
//dst_uv.copyTo(dst(cv::Rect(image.rows > image.cols ? image.cols : 0, image.rows > image.cols ? 0 : image.rows, image.cols, image.rows)));
//imwrite("D:\\aftuv" + std::to_string(svindex) + ".jpg", dst_uv);
}
} }
else else
{ {
cv::Rect uvBoundingRect = uvRoi_clone.boundingRect(); cv::Rect uvBoundingRect = uvRoi_clone.boundingRect();
cv::Rect roi_dst_right; cv::Rect uvBoundingRect_temp;
roi_dst_right.x = dst_uv.cols > uvBoundingRect.width ? (dst_uv.cols - uvBoundingRect.width) / 2 : 0; if (angle == 90 || angle == 270)
roi_dst_right.width = cv::min(dst_uv.cols, uvBoundingRect.width); {
roi_dst_right.y = dst_uv.rows > uvBoundingRect.height ? (dst_uv.rows - uvBoundingRect.height) / 2 : 0; uvBoundingRect_temp.x = dst_uv.rows > uvBoundingRect.width ? uvBoundingRect.x : uvBoundingRect.x + (uvBoundingRect.width - dst_uv.rows) / 2;
roi_dst_right.height = cv::min(dst_uv.rows, uvBoundingRect.height); uvBoundingRect_temp.width = cv::min(dst_uv.rows, uvBoundingRect.width);
uvBoundingRect_temp.y = dst_uv.cols > uvBoundingRect.height ? uvBoundingRect.y : uvBoundingRect.y + (uvBoundingRect.height - dst_uv.cols) / 2;
cv::Rect roi_uv_BoundingRect((uvBoundingRect.width - roi_dst_right.width) / 2, uvBoundingRect_temp.height = cv::min(dst_uv.cols, uvBoundingRect.height);
(uvBoundingRect.height - roi_dst_right.height) / 2, roi_dst_right.width, roi_dst_right.height);
Mat uvCrop = (uv(uvBoundingRect))(roi_uv_BoundingRect);
if (uvCrop.channels() == 1 && dst_uv.channels() == 3)
cv::cvtColor(uvCrop, uvCrop, cv::COLOR_GRAY2BGR);
uvCrop.copyTo(dst_uv(roi_dst_right));
} }
else
{
uvBoundingRect_temp.x = dst_uv.cols > uvBoundingRect.width ? uvBoundingRect.x : uvBoundingRect.x + (uvBoundingRect.width - dst_uv.cols) / 2;
uvBoundingRect_temp.width = cv::min(dst_uv.cols, uvBoundingRect.width);
uvBoundingRect_temp.y = dst_uv.rows > uvBoundingRect.height ? uvBoundingRect.y : uvBoundingRect.y + (uvBoundingRect.height - dst_uv.rows) / 2;
uvBoundingRect_temp.height = cv::min(dst_uv.rows, uvBoundingRect.height);
}
int offset_left = 0, offset_right = 0, offset_top = 0, offset_bottom = 0;
if (uvBoundingRect_temp.x < 0)
{
offset_left = -uvBoundingRect_temp.x;
uvBoundingRect_temp.x = 0;
uvBoundingRect_temp.width -= offset_left;
}
if (uvBoundingRect_temp.y < 0)
{
offset_top = -uvBoundingRect_temp.y;
uvBoundingRect_temp.y = 0;
uvBoundingRect_temp.height -= offset_top;
}
if (uvBoundingRect_temp.br().x >= uv.cols)
{
offset_right = uvBoundingRect_temp.br().x - uv.cols + 1;
uvBoundingRect_temp.width -= offset_right;
}
if (uvBoundingRect_temp.br().y >= uv.rows)
{
offset_bottom = uvBoundingRect_temp.br().y - uv.rows + 1;
uvBoundingRect_temp.height -= offset_bottom;
}
cv::Mat uv_roi = uv(uvBoundingRect_temp).clone();
if (dst_uv.channels() == 1)//»Ò¶È
{
cv::cvtColor(uv_roi, uv_roi, cv::COLOR_BGR2GRAY);
if (pixtype == 0)//¶þֵͼ
cv::threshold(uv_roi, uv_roi, 150, 255, THRESH_OTSU);
}
uv_roi.copyTo(dst_uv(cv::Rect((dst_uv.cols - uv_roi.cols + offset_left) / 2, (dst_uv.rows - uv_roi.rows + offset_top) / 2, uv_roi.cols, uv_roi.rows)));
}
//imwrite("D:\\dst" + std::to_string(svindex) + ".jpg", dst);
return dst; return dst;
} }
void ImageApplyUV::purgeQR_kernal(cv::Mat& image, const cv::Rect& roi, std::map<int, cv::Scalar> map_color, int dpi, int threshold) void ImageApplyUV::purgeQR_kernal(cv::Mat& image, const cv::Rect& roi, std::map<int, cv::Scalar> map_color, int dpi, int threshold)
{ {
cv::Mat image_roi = image(roi); cv::Mat image_roi = image(roi);
@ -232,3 +289,4 @@ cv::Scalar ImageApplyUV::getColor(const cv::Mat& image, int col, int threshold)
return color; return color;
} }

View File

@ -10,7 +10,7 @@ public:
~ImageApplyUV(); ~ImageApplyUV();
void Apply(cv::Mat& image, const cv::Mat& uv, int dpi = 200, int thresh = 100); void Apply(cv::Mat& image, const cv::Mat& uv, int dpi = 200, int thresh = 100);
static cv::Mat Apply(const cv::Mat& image, const cv::Mat& uv, const cv::RotatedRect& uvRoi, bool isDesaskew, int angle); static cv::Mat Apply(const cv::Mat& image, const cv::Mat& uv, const cv::RotatedRect& uvRoi, bool isDesaskew, int angle, int pixtype);
private: private:
void purgeQR_kernal(cv::Mat& image, const cv::Rect& roi, std::map<int, cv::Scalar> map_color, int dpi, int threshold); void purgeQR_kernal(cv::Mat& image, const cv::Rect& roi, std::map<int, cv::Scalar> map_color, int dpi, int threshold);
void findContours1(const cv::Mat& src, std::vector<std::vector<cv::Point>>& contours, std::vector<cv::Vec4i>& hierarchy, void findContours1(const cv::Mat& src, std::vector<std::vector<cv::Point>>& contours, std::vector<cv::Vec4i>& hierarchy,
@ -20,3 +20,4 @@ private:
cv::Mat lut; cv::Mat lut;
}; };

Binary file not shown.