From 67d296fdc9f5612d3cdf5616739c99222e8bf12a Mon Sep 17 00:00:00 2001 From: masayume <1936714878@qq.com> Date: Tue, 24 Aug 2021 18:17:32 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4uv=E6=8B=BC=E6=8E=A5=E7=AE=97?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- huagao/Device/GScanO1003399.cpp | 2 +- huagao/Device/GScanO400.cpp | 69 +++++++------ huagao/Device/ImageMatQueue.cpp | 114 ++++++++++++--------- huagao/Device/ImageMatQueue.h | 35 ++++--- huagao/ImageProcess/ImageApplyAutoCrop.cpp | 5 + huagao/ImageProcess/ImageApplyAutoCrop.h | 1 + huagao/ImageProcess/ImageApplyUV.cpp | 114 ++++++++++++++++----- huagao/ImageProcess/ImageApplyUV.h | 3 +- huagao/stdafx.h | Bin 12360 -> 12356 bytes 9 files changed, 212 insertions(+), 131 deletions(-) diff --git a/huagao/Device/GScanO1003399.cpp b/huagao/Device/GScanO1003399.cpp index 74e83e09..30a67081 100644 --- a/huagao/Device/GScanO1003399.cpp +++ b/huagao/Device/GScanO1003399.cpp @@ -263,7 +263,7 @@ void GScanO1003399::config_params(GScanCap& param) cfg.g400params.pageSize = SupPaperTyps[ps]; CSize size; #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 size = Device::PaperSize().GetPaperSize(param.papertype, 200.0f, param.paperAlign); #endif// G300 diff --git a/huagao/Device/GScanO400.cpp b/huagao/Device/GScanO400.cpp index bab9393c..ba4b3588 100644 --- a/huagao/Device/GScanO400.cpp +++ b/huagao/Device/GScanO400.cpp @@ -133,7 +133,7 @@ typedef enum tagUsbKeyWords : UINT32 //设置纵向畸变系数 SET_JUST_COF_V = 0x41, //读取纵向畸变系数 - GET_JUST_COF_V=0x42, + GET_JUST_COF_V = 0x42, //设置扫描仪编码 GET_CODE_G400 = 0x59, //读取扫描仪编码 @@ -151,9 +151,9 @@ GScanO400::GScanO400() : m_bread_fixed_ratio_fromDSP(false) { m_pImages.reset(new ImageMatQueue()); - auto getimgnum = [&](bool isadd,int num) + auto getimgnum = [&](bool isadd, int num) { - isadd ? image_num+=num : image_num-=num; + isadd ? image_num += num : image_num -= num; }; m_pImages->SetGetimgnumcall(getimgnum); } @@ -190,7 +190,7 @@ void GScanO400::open(int vid, int pid) //m_usb->write_bulk(&status, sizeof(status)); //m_usb->read_bulk(&status, sizeof(status)); //std::this_thread::sleep_for(std::chrono::milliseconds(20)); - notifyscan(); + //notifyscan(); GetFWVersion(); } } @@ -217,7 +217,7 @@ int GScanO400::aquire_bmpdata(std::vector& bmpdata) devState = DEV_STOP; m_threadUsb->join(); m_threadUsb.reset(); - FileTools::writelog(log_ERROR,"aquire_bmpdata m_threadUsb.reset()"); + FileTools::writelog(log_ERROR, "aquire_bmpdata m_threadUsb.reset()"); } Stop_scan();//停止扫描 ResetScanner(); @@ -254,13 +254,15 @@ BOOL GScanO400::IsConnected() std::string GScanO400::GetFWVersion() { if (m_usb.get() && m_usb->is_connected()) { - lock_guard< mutex> lock(m_imgLocker); + //lock_guard< mutex> lock(m_imgLocker); if (fwVersion.empty()) { fwVersion.resize(10); USBCB cmd = { GET_FW_VERSION,fwVersion.size(),0, }; m_usb->write_bulk(&cmd, sizeof(cmd)); - if(m_usb.get()&&m_usb->is_connected()) + std::this_thread::sleep_for(chrono::milliseconds(10)); + if (m_usb.get() && m_usb->is_connected()) 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") is_AndroidOrLinux = false; @@ -281,7 +283,7 @@ std::string GScanO400::GetSerialNum() SerialNum.resize(14); USBCB usbcb = { GET_SERIAL,SerialNum.size(),0 }; m_usb->write_bulk(&usbcb, sizeof(usbcb)); - if(m_usb.get()&&m_usb->is_connected()) + if (m_usb.get() && m_usb->is_connected()) m_usb->read_bulk(&SerialNum[0], SerialNum.size()); } return SerialNum; @@ -331,7 +333,7 @@ BOOL GScanO400::Get_Scanner_PaperOn() Set_ErrorCode(USB_DISCONNECTED); return true; } - //return false; + //return false; USBCB usbcb = { GET_PAPER_STATUS ,2,0 }; std::lock_guard lck(m_imgLocker); m_usb->write_bulk(&usbcb, sizeof(usbcb)); @@ -343,7 +345,7 @@ BOOL GScanO400::Get_Scanner_PaperOn() return true; } } - else{ + else { Set_ErrorCode(USB_DISCONNECTED); return true; } @@ -374,7 +376,7 @@ void GScanO400::config_params(GScanCap& params) gcap = params; UINT32 cfgdata = cfg.GetData(); USBCB usbcb = { CONFIGURED_DATA,cfgdata,0 }; - FileTools::writelog(log_INFO, "config hardware param"+to_string(cfgdata)); + FileTools::writelog(log_INFO, "config hardware param" + to_string(cfgdata)); m_usb->write_bulk(&usbcb, sizeof(USBCB)); this_thread::sleep_for(std::chrono::milliseconds(200)); m_pImages->setparam(params); @@ -390,7 +392,7 @@ void GScanO400::Scanner_StartScan(UINT16 count) devState = DEV_STOP; m_threadUsb->join(); } - USBCB status = { GET_DSP_STATUS ,0,0 }; + USBCB status = { GET_DSP_STATUS ,0,0 }; if (m_usb.get() && m_usb->is_connected()) m_usb->write_bulk(&status, sizeof(status)); if (m_usb.get() && m_usb->is_connected()) @@ -422,7 +424,7 @@ void GScanO400::Scanner_StartScan(UINT16 count) if (m_usb.get() && m_usb->is_connected()) m_usb->write_bulk(&paperstatus, sizeof(paperstatus)); if (m_usb.get() && m_usb->is_connected()) - readlenght= m_usb->read_bulk(&paperstatus, sizeof(paperstatus)); + readlenght = m_usb->read_bulk(&paperstatus, sizeof(paperstatus)); if (paperstatus.u32_Data == 0) { m_pImages->setscanflags(false); devState = DEV_WRONG; @@ -434,7 +436,7 @@ void GScanO400::Scanner_StartScan(UINT16 count) dev_callback(errorcode, huagods); return; } - m_pImages->reset_DogEar(); + m_pImages->reset_DogEar(); if (gcap.is_duplex) count = count == 65535 ? 65535 : count / 2; USBCB usbcb = { START_COMMAND,(UINT32)count ,0 }; @@ -540,7 +542,7 @@ void GScanO400::clear_hwerror() void GScanO400::set_sleep_time(int mode) { - return ; + return; } bool GScanO400::set_scannercode(std::string str) @@ -646,37 +648,37 @@ void GScanO400::usbmain() m_usb->set_timeout(200); if (!imgData->size()) { Stop_scan(); - FileTools::writelog(log_ERROR,"imgData->size() error"); + FileTools::writelog(log_ERROR, "imgData->size() error"); break; } FileTools::writelog(log_INFO, " get image data size " + to_string(totalNum)); if (imgData->size() != totalNum) { - FileTools::writelog(log_ERROR," get image data size error totalnum " + to_string(totalNum) + " imgdata size " + to_string(imgData->size())); + FileTools::writelog(log_ERROR, " get image data size error totalnum " + to_string(totalNum) + " imgdata size " + to_string(imgData->size())); } - if(!m_pImages->get_isDogEar()) + if (!m_pImages->get_isDogEar()) m_pImages->pushMat(std::shared_ptr(new G400Decode(imgData))); #ifdef UV - if(gcap.hardwarecaps.en_uv) - { - if(indeximg%2==0) - { - UpdateScanInfo(countNReaded(), get_imgTransfered()); - } - } - else + if (gcap.hardwarecaps.en_uv) + { + if (indeximg % 2 == 0) { UpdateScanInfo(countNReaded(), get_imgTransfered()); } - indeximg++; + } + else + { + UpdateScanInfo(countNReaded(), get_imgTransfered()); + } + indeximg++; #else UpdateScanInfo(countNReaded(), get_imgTransfered()); #endif - if(!is_AndroidOrLinux) + if (!is_AndroidOrLinux) Pop_Image(); - FileTools::writelog(log_INFO, "从扫描仪接收" + to_string(get_imgnReaded()) + "份文件。耗时 "+to_string(sw.elapsed_ms())); + FileTools::writelog(log_INFO, "从扫描仪接收" + to_string(get_imgnReaded()) + "份文件。耗时 " + to_string(sw.elapsed_ms())); sw.reset(); break; } @@ -703,8 +705,8 @@ void GScanO400::usbmain() { //if (is_AndroidOrLinux) //{ - haveError = true; - Set_ErrorCode(usbcb.u32_Data); + haveError = true; + Set_ErrorCode(usbcb.u32_Data); //} //else //{ @@ -733,7 +735,7 @@ void GScanO400::usbmain() catch (const std::exception& e) { //writelog(e.what()); - FileTools::writelog(log_ERROR,e.what()); + FileTools::writelog(log_ERROR, e.what()); } ////FileTools::write_log("D:\\1.txt", "thread usb exit"); @@ -808,7 +810,7 @@ std::shared_ptr> GScanO400::Get_Img_Data(int bufferSize) } if (sw.elapsed_ms() > 5000) { - FileTools::writelog(log_ERROR,"Usb read data timeout\n"); + FileTools::writelog(log_ERROR, "Usb read data timeout\n"); } return imData; } @@ -827,3 +829,4 @@ void GScanO400::Pop_Image() USBCB usbcb = { POP_IMAGE ,0,0 }; m_usb->write_bulk(&usbcb, sizeof(usbcb)); } + diff --git a/huagao/Device/ImageMatQueue.cpp b/huagao/Device/ImageMatQueue.cpp index 56ecee1e..3fbfbe43 100644 --- a/huagao/Device/ImageMatQueue.cpp +++ b/huagao/Device/ImageMatQueue.cpp @@ -16,15 +16,15 @@ using namespace std; ImageMatQueue::ImageMatQueue(void) : bRun(false) , is_scanning(false) - ,fx(1.001) - ,fy(1.007) - ,DogEar_index(0) - ,is_DogEar(false) - ,benablecache(false) + , fx(1.001) + , fy(1.007) + , DogEar_index(0) + , is_DogEar(false) + , benablecache(false) { - + atm_orgin_image_remains = 0; - m_dogear.reset(new CImageApplyDogEarDetection(40,1.0,200)); + m_dogear.reset(new CImageApplyDogEarDetection(40, 1.0, 200)); m_snowflake.init(1, 1); } @@ -89,11 +89,11 @@ void ImageMatQueue::updatefixratio(float& hratio, float& vratio) } else { - FileTools::writelog(log_ERROR,"error horizental or vertical ratio"); + FileTools::writelog(log_ERROR, "error horizental or vertical ratio"); } } -void ImageMatQueue::SetGetimgnumcall(std::function getimgnumcall) +void ImageMatQueue::SetGetimgnumcall(std::function getimgnumcall) { m_Getimagenumber = getimgnumcall; } @@ -107,10 +107,10 @@ void ImageMatQueue::SetDogEarCallback(std::function dogearcall) static int paperIndex = 0; void ImageMatQueue::pushMat(std::shared_ptr data) { - if(m_Getimagenumber) + if (m_Getimagenumber) { #ifdef UV - if(scanParam.hardwarecaps.en_uv) + if (scanParam.hardwarecaps.en_uv) m_Getimagenumber(true, 2); else m_Getimagenumber(true, 1); @@ -173,13 +173,13 @@ void ImageMatQueue::setparam(const GScanCap& param) #else // REAL300DPI fixedSize = papersize.GetPaperSize(param.papertype, 200.0f, param.paperAlign); #endif - bool normalCrop = ((param.autodescrew) || (islongcustomcrop ? islongcustomcrop : param.is_autocrop)||(param.fillbackground)) ? false : param.normalCrop; + bool normalCrop = ((param.autodescrew) || (islongcustomcrop ? islongcustomcrop : param.is_autocrop) || (param.fillbackground)) ? false : param.normalCrop; m_iaList.push_back(shared_ptr(new CImageApplyAutoCrop(islongcustomcrop ? islongcustomcrop : param.is_autocrop, - param.autodescrew, param.fillbackground, cv::Size(fixedSize.cx, fixedSize.cy), param.is_convex,false,param.AutoCrop_threshold,param.noise,param.indent,normalCrop))); + param.autodescrew, param.fillbackground, cv::Size(fixedSize.cx, fixedSize.cy), param.is_convex, false, param.AutoCrop_threshold, param.noise, param.indent, normalCrop))); - /* m_iaList.push_back(shared_ptr(new CImageApplyAutoCrop(true, param.autodescrew, param.fillbackground, cv::Size(fixedSize.cx, fixedSize.cy), param.is_convex, false, param.AutoCrop_threshold, param.noise, param.indent))); - if(!(islongcustomcrop ? islongcustomcrop : param.is_autocrop)) - m_iaList.push_back(shared_ptr(new CImageApplyResize(CImageApplyResize::ResizeType::DSIZE, cv::Size(fixedSize.cx, fixedSize.cy), 1.0, 1.0)));*/ + /* m_iaList.push_back(shared_ptr(new CImageApplyAutoCrop(true, param.autodescrew, param.fillbackground, cv::Size(fixedSize.cx, fixedSize.cy), param.is_convex, false, param.AutoCrop_threshold, param.noise, param.indent))); + if(!(islongcustomcrop ? islongcustomcrop : param.is_autocrop)) + m_iaList.push_back(shared_ptr(new CImageApplyResize(CImageApplyResize::ResizeType::DSIZE, cv::Size(fixedSize.cx, fixedSize.cy), 1.0, 1.0)));*/ } if (param.is_autodiscradblank_normal || param.is_autodiscradblank_vince) { //m_iaList.push_back(shared_ptr(new CImageApplyDiscardBlank())); @@ -239,8 +239,8 @@ void ImageMatQueue::setparam(const GScanCap& param) if (param.brightness != 0 || param.contrast != 0 || param.gamma != 1.0) { double aa = (254 / 2000.0) * param.brightness + 0.5; int bright = ceil(aa);//[-127,128] 0.128=256.0/2000.0 - int contrast=0.0; //= (int)(param.contrast * 0.036);;//[-36,36] 0.036=72.0/2000.0; - if(param.contrast<0.0) // 暂不修改对比度,彩色文稿色偏 + int contrast = 0.0; //= (int)(param.contrast * 0.036);;//[-36,36] 0.036=72.0/2000.0; + if (param.contrast < 0.0) // 暂不修改对比度,彩色文稿色偏 contrast = (int)(param.contrast * 0.036); else contrast = (int)(param.contrast * 0.018); @@ -258,7 +258,7 @@ void ImageMatQueue::setparam(const GScanCap& param) } //自动颜色识别 if (param.automaticcolor) - m_iaList.push_back(shared_ptr(new CImageApplyColorRecognition(param.automaticcolortype==1? CImageApplyColorRecognition::ColorRecognitionMode::Color_Gray:CImageApplyColorRecognition::ColorRecognitionMode::Color_Mono))); + m_iaList.push_back(shared_ptr(new CImageApplyColorRecognition(param.automaticcolortype == 1 ? CImageApplyColorRecognition::ColorRecognitionMode::Color_Gray : CImageApplyColorRecognition::ColorRecognitionMode::Color_Mono))); //缩放 if (param.resolution_dst != param.resolution_native) { @@ -280,16 +280,16 @@ void ImageMatQueue::setparam(const GScanCap& param) if (param.pixtype == 0) //threshold m_iaList.push_back(shared_ptr(new CImageApplyBWBinaray(CImageApplyBWBinaray::ThresholdType::THRESH_BINARY))); //黑白降噪优化 - if (scanParam.detachnoise.is_detachnoise&&scanParam.pixtype==0) + if (scanParam.detachnoise.is_detachnoise && scanParam.pixtype == 0) { m_iaList.push_back(shared_ptr(new CImageApplyDetachNoise(scanParam.detachnoise.detachnoise))); } - + if (scanParam.en_fold) { - m_iaList.push_back(shared_ptr(new CImageApplyConcatenation(CImageApplyConcatenation::horizontal,cv::Scalar(255,255,255)))); + m_iaList.push_back(shared_ptr(new CImageApplyConcatenation(CImageApplyConcatenation::horizontal, cv::Scalar(255, 255, 255)))); } if (param.imageRotateDegree != 0.0 || param.is_backrotate180 || param.is_autotext) @@ -329,26 +329,26 @@ void ImageMatQueue::EnqueueBmpBuffer(std::shared_ptr> } void ImageMatQueue::PaniusCount(int count) { - atm_orgin_image_remains-=count; + atm_orgin_image_remains -= count; #ifdef UV - if(scanParam.hardwarecaps.en_uv) + if (scanParam.hardwarecaps.en_uv) { - if(m_Getimagenumber) + if (m_Getimagenumber) m_Getimagenumber(false, 2); } else - if(m_Getimagenumber) + if (m_Getimagenumber) m_Getimagenumber(false, 1); #else if (m_Getimagenumber) - m_Getimagenumber(false,1); + m_Getimagenumber(false, 1); #endif } void ImageMatQueue::init_cachethread() { - if(!m_threadcache.get()) + if (!m_threadcache.get()) { benablecache = true; m_threadcache.reset(new thread(&ImageMatQueue::cache_run, this)); @@ -359,7 +359,7 @@ static int index = 0; void ImageMatQueue::cache_run() { std::ios::sync_with_stdio(false); - while(benablecache) + while (benablecache) { if (m_rawBuffs.Size() == 0) { @@ -382,7 +382,7 @@ void ImageMatQueue::cache_run() if (!access(path.c_str(), 0)) { remove(path.c_str()); - FileTools::writelog(log_ERROR, "exist file "+ path+" maybe lost previous session scanned image"); + FileTools::writelog(log_ERROR, "exist file " + path + " maybe lost previous session scanned image"); } CFile frb; @@ -425,7 +425,7 @@ void ImageMatQueue::clear_cachefiles() bool ImageMatQueue::empty() { - return m_imgCacheinfo.Size()==0&&(m_imagedata.Size()==0 )&&(m_rawBuffs.Size()==0)&& !is_scanning&& atm_orgin_image_remains==0; + return m_imgCacheinfo.Size() == 0 && (m_imagedata.Size() == 0) && (m_rawBuffs.Size() == 0) && !is_scanning && atm_orgin_image_remains == 0; } bool ImageMatQueue::queuesempty() @@ -441,7 +441,7 @@ void ImageMatQueue::proc() { string msg; auto info = m_imgCacheinfo.Take(); - if(info.path.length()==0 || !isFileExist(info.path)) + if (info.path.length() == 0 || !isFileExist(info.path)) { msg = "error while checking file :" + info.path + " ,file lost"; FileTools::writelog(log_ERROR, msg); @@ -451,22 +451,22 @@ void ImageMatQueue::proc() ImreadModes rmc; if (scanParam.filter != 3 || scanParam.enhance_color || scanParam.hsvcorrect) rmc = IMREAD_COLOR; - else + else rmc = scanParam.pixtype == 2 ? IMREAD_COLOR : IMREAD_GRAYSCALE; std::vector mats; std::vector uvmats; StopWatch sw; - if(info.scannerType==ScannerSerial::G200Serial) + if (info.scannerType == ScannerSerial::G200Serial) { - auto back= m_imgCacheinfo.Take(); - if(!isFileExist(back.path)) + auto back = m_imgCacheinfo.Take(); + if (!isFileExist(back.path)) { msg = "error while reading g200 back image " + back.path + " ,file not exist"; - FileTools::writelog(log_ERROR,msg); + FileTools::writelog(log_ERROR, msg); } sw.reset(); - auto imgfront= imread(info.path, rmc); + auto imgfront = imread(info.path, rmc); msg = "reading image front time elapsed_ms:" + std::to_string(sw.elapsed_ms()); FileTools::writelog(log_ERROR, msg); sw.reset(); @@ -474,23 +474,23 @@ void ImageMatQueue::proc() msg = "reading image back time elapsed_ms:" + std::to_string(sw.elapsed_ms()); FileTools::writelog(log_ERROR, msg); sw.reset(); - if(!imgfront.empty()&&!imgback.empty()) + if (!imgfront.empty() && !imgback.empty()) { - mats.push_back(scanParam.is_switchfrontback? imgback:imgfront); - mats.push_back(scanParam.is_switchfrontback? imgfront:imgback); + mats.push_back(scanParam.is_switchfrontback ? imgback : imgfront); + mats.push_back(scanParam.is_switchfrontback ? imgfront : imgback); remove(info.path.c_str()); remove(back.path.c_str()); } else { - msg = "get empty mat! empty "; + msg = "get empty mat! empty "; msg += (imgfront.empty() ? " front image" : " back image"); FileTools::writelog(log_ERROR, msg); } for (size_t i = 0; i < mats.size(); i++) { - if(!mats[i].empty()) + if (!mats[i].empty()) cv::resize(mats[i], mats[i], cv::Size(), fx, fy);//用于修正与佳能机器幅面大小不匹配问题 此系数请勿轻易动 } @@ -501,17 +501,24 @@ void ImageMatQueue::proc() } else { - auto mat= imread(info.path, rmc); - if(!mat.empty()) + auto mat = imread(info.path, rmc); + if (!mat.empty()) { Mat front = mat(Rect(0, 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) { cv::flip(front, front, 0); cv::flip(front, front, 1); } - mats.push_back(scanParam.is_switchfrontback ? back : front); - mats.push_back(scanParam.is_switchfrontback ? front : back); + mats.push_back(back); + mats.push_back(front); +#endif + front.release(); + back.release(); remove(info.path.c_str()); } else @@ -520,16 +527,16 @@ void ImageMatQueue::proc() FileTools::writelog(log_ERROR, msg); } #ifdef UV - if(scanParam.hardwarecaps.en_uv)//EN UV + if (scanParam.hardwarecaps.en_uv)//EN UV { auto uvinfo = m_imgCacheinfo.Take(); auto matuv = imread(uvinfo.path, IMREAD_COLOR); - if(!matuv.empty()) + if (!matuv.empty()) { Mat front = matuv(Rect(0, 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 ? back : front); front.release(); back.release(); remove(uvinfo.path.c_str()); @@ -564,7 +571,7 @@ void ImageMatQueue::proc() mats[j].release(); 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()) mats[j] = mergeOrgin_UV; } @@ -581,9 +588,14 @@ void ImageMatQueue::proc() } if (!mats[i].empty()) { 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 (mats[i].channels() == 3) 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); if (!scanParam.multi_output_red) mats[i].release(); diff --git a/huagao/Device/ImageMatQueue.h b/huagao/Device/ImageMatQueue.h index 5002b426..122e783b 100644 --- a/huagao/Device/ImageMatQueue.h +++ b/huagao/Device/ImageMatQueue.h @@ -2,6 +2,7 @@ #include #include #include +#include #include "ImageProcess/ImageApplyHeaders.h" #include "PublicFunc.h" #include "BlockingQueue.h" @@ -28,11 +29,11 @@ protected: std::shared_ptr> m_data; }; -class Mat2Bmp:public IMat2Bmp { +class Mat2Bmp :public IMat2Bmp { public: - Mat2Bmp(const cv::Mat& mat,float res) { + Mat2Bmp(const cv::Mat& mat, float res) { //m_data =std::shared_ptr>(new std::vector()); - m_data.reset(new std::vector()); + //m_data.reset(new std::vector()); int headersize = mat.channels() == 3 ? 54 : 1078; int step = mat.step; int bmpdatasize = (step + 3) / 4 * 4 * mat.rows; @@ -44,7 +45,7 @@ public: { uchar colortable[256 * 4]{ 0 }; for (int i = 0; i < 256; i++) { - colortable[i*4] = colortable[i*4 + 1] = colortable[i*4 + 2] = i; + colortable[i * 4] = colortable[i * 4 + 1] = colortable[i * 4 + 2] = i; } memcpy(m_data->data() + 54, colortable, 256 * 4); } @@ -87,7 +88,7 @@ private: class Mat2BmpBw :public IMat2Bmp { public: - Mat2BmpBw(const cv::Mat& mat,float res) { + Mat2BmpBw(const cv::Mat& mat, float res) { //static int indeximg = 0; //cv::imwrite("D:\\" + to_string(++indeximg) + ".jpg", mat); m_data = std::shared_ptr>(new std::vector()); @@ -97,12 +98,12 @@ public: int bmpdatasize = (width + 31) / 32 * 4 * height; m_data->resize(headsize + bmpdatasize); - unsigned char* binary = m_data->data()+62;//file header(14)+info header(40)+RGBQUAD(8) - setBmpFileHeader(mat,bmpdatasize); + unsigned char* binary = m_data->data() + 62;//file header(14)+info header(40)+RGBQUAD(8) + setBmpFileHeader(mat, bmpdatasize); setBmpInfoHeader(mat, bmpdatasize, res); memcpy(m_data->data(), &m_fileHeader, sizeof(BITMAPFILEHEADER)); memcpy(m_data->data() + sizeof(BITMAPFILEHEADER), &m_infoHeader, sizeof(BITMAPINFOHEADER)); - RGBQUAD* quad = (RGBQUAD*)(m_data->data() + sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)); + RGBQUAD* quad = (RGBQUAD*)(m_data->data() + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER)); quad->rgbBlue = quad->rgbGreen = quad->rgbRed = quad->rgbReserved = 0; quad++; quad->rgbBlue = quad->rgbGreen = quad->rgbRed = 255; @@ -131,7 +132,7 @@ public: } } private: - void setBmpFileHeader(const cv::Mat& mat,const int bmpDataLen) { + void setBmpFileHeader(const cv::Mat& mat, const int bmpDataLen) { memset(&m_fileHeader, 0, sizeof(BITMAPFILEHEADER)); m_fileHeader.bfType = ((unsigned short)('M' << 8) | 'B'); m_fileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + (sizeof(RGBQUAD) * 2); @@ -140,7 +141,7 @@ private: m_fileHeader.bfReserved1 = m_fileHeader.bfReserved2 = 0; } - void setBmpInfoHeader(const cv::Mat& mat,const int bmpDataLen, const long res){ + void setBmpInfoHeader(const cv::Mat& mat, const int bmpDataLen, const long res) { memset(&m_infoHeader, 0, sizeof(BITMAPINFOHEADER)); m_infoHeader.biSize = sizeof(BITMAPINFOHEADER); m_infoHeader.biWidth = mat.cols; @@ -152,7 +153,7 @@ private: m_infoHeader.biXPelsPerMeter = m_infoHeader.biYPelsPerMeter = static_cast(res * 39.37F + 0.5); m_infoHeader.biClrUsed = m_infoHeader.biClrImportant = 2; } - + private: BITMAPFILEHEADER m_fileHeader; BITMAPINFOHEADER m_infoHeader; @@ -168,12 +169,12 @@ protected: std::vector>> m_buffs; }; -class G200Decode : public IDecode{ +class G200Decode : public IDecode { public: G200Decode(std::shared_ptr> buff); virtual ~G200Decode() { - + } }; @@ -218,13 +219,13 @@ public: int orginimgcount(); void setscanflags(const bool flags) { is_scanning = flags; } void updatefixratio(float& hratio, float& vratio); - void SetGetimgnumcall(std::function getimgnumcall); + void SetGetimgnumcall(std::function getimgnumcall); void SetDogEarCallback(std::function dogearcall); private: void proc(); void EnqueueBmpBuffer(std::shared_ptr>); - void PaniusCount(int count=1); + void PaniusCount(int count = 1); void init_cachethread(); void cache_run(); //娓呴櫎缂撳瓨鏂囦欢 @@ -242,12 +243,12 @@ private: atomic_bool benablecache; atomic_int atm_orgin_image_remains; atomic_bool is_scanning; - GScanCap scanParam; + GScanCap scanParam; Device::PaperSize papersize; std::shared_ptr m_dogear; std::vector> m_iaList; //脥录脧帽麓娄脌铆潞炉脢媒陆脫驴脷 float fx, fy; std::function m_DogEarDetection_callback; - std::function m_Getimagenumber; + std::function m_Getimagenumber; snowflake_t m_snowflake; }; \ No newline at end of file diff --git a/huagao/ImageProcess/ImageApplyAutoCrop.cpp b/huagao/ImageProcess/ImageApplyAutoCrop.cpp index 9394a219..b87a60ba 100644 --- a/huagao/ImageProcess/ImageApplyAutoCrop.cpp +++ b/huagao/ImageProcess/ImageApplyAutoCrop.cpp @@ -170,9 +170,12 @@ void CImageApplyAutoCrop::apply(cv::Mat& pDib, int side) void CImageApplyAutoCrop::apply(std::vector& mats, bool isTwoSide) { + m_rects.clear(); if (mats.empty()) return; if (!mats[0].empty()) { apply(mats[0], 0); + m_rects.push_back(m_rect); + //cv::imwrite("1.bmp", mats[0]); } if (isTwoSide && mats.size() > 1) @@ -182,6 +185,7 @@ void CImageApplyAutoCrop::apply(std::vector& mats, bool isTwoSide) m_fixedSize = mats[0].size(); if (!mats[1].empty()) { apply(mats[1], 1); + m_rects.push_back(m_rect); } if (!mats[0].empty()) @@ -243,3 +247,4 @@ uchar CImageApplyAutoCrop::getBackGroudChannelMean(const cv::Mat& gray, int tota } return 255; } + diff --git a/huagao/ImageProcess/ImageApplyAutoCrop.h b/huagao/ImageProcess/ImageApplyAutoCrop.h index 7c8b80bf..eaf84f89 100644 --- a/huagao/ImageProcess/ImageApplyAutoCrop.h +++ b/huagao/ImageProcess/ImageApplyAutoCrop.h @@ -109,3 +109,4 @@ private: }; #endif // !IMAGE_APPLY_AUTO_CROP_H + diff --git a/huagao/ImageProcess/ImageApplyUV.cpp b/huagao/ImageProcess/ImageApplyUV.cpp index e2f5c13d..779d633b 100644 --- a/huagao/ImageProcess/ImageApplyUV.cpp +++ b/huagao/ImageProcess/ImageApplyUV.cpp @@ -2,7 +2,7 @@ using namespace cv; #define SCA 50 -ImageApplyUV::ImageApplyUV(): lut(1, 256, CV_8UC1) +ImageApplyUV::ImageApplyUV() : lut(1, 256, CV_8UC1) { } @@ -13,15 +13,15 @@ ImageApplyUV::~ImageApplyUV() void ImageApplyUV::update_lutData(int contrast) { unsigned char* ptr = lut.data; - int m_contrast = cv::max(-127, cv::min(contrast, 127)); - for (int i = 0; i < 256; i++) - { - //update contrast - if (i < 128) - ptr[i] = static_cast(cv::max(0, cv::min(i - m_contrast, 127))); - else - ptr[i] = static_cast(cv::max(127, cv::min(i + m_contrast, 255))); - } + int m_contrast = cv::max(-127, cv::min(contrast, 127)); + for (int i = 0; i < 256; i++) + { + //update contrast + if (i < 128) + ptr[i] = static_cast(cv::max(0, cv::min(i - m_contrast, 127))); + else + ptr[i] = static_cast(cv::max(127, cv::min(i + m_contrast, 255))); + } } void ImageApplyUV::Apply(cv::Mat& image, const cv::Mat& uv, int dpi, int thresh) @@ -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)); if (uv_resize.channels() == 3) 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::dilate(uv_resize, uv_resize, element); std::vector> 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(); - 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()); 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]; uvRoi_clone.points(srcTri); - if (angle == 90) { 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); } 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 { cv::Rect uvBoundingRect = uvRoi_clone.boundingRect(); - cv::Rect roi_dst_right; - roi_dst_right.x = dst_uv.cols > uvBoundingRect.width ? (dst_uv.cols - uvBoundingRect.width) / 2 : 0; - 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; - roi_dst_right.height = cv::min(dst_uv.rows, uvBoundingRect.height); + cv::Rect uvBoundingRect_temp; + if (angle == 90 || angle == 270) + { + uvBoundingRect_temp.x = dst_uv.rows > uvBoundingRect.width ? uvBoundingRect.x : uvBoundingRect.x + (uvBoundingRect.width - dst_uv.rows) / 2; + 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; + uvBoundingRect_temp.height = cv::min(dst_uv.cols, uvBoundingRect.height); + } + 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); + } - cv::Rect roi_uv_BoundingRect((uvBoundingRect.width - roi_dst_right.width) / 2, - (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)); + 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; } - void ImageApplyUV::purgeQR_kernal(cv::Mat& image, const cv::Rect& roi, std::map map_color, int dpi, int threshold) { cv::Mat image_roi = image(roi); @@ -232,3 +289,4 @@ cv::Scalar ImageApplyUV::getColor(const cv::Mat& image, int col, int threshold) return color; } + diff --git a/huagao/ImageProcess/ImageApplyUV.h b/huagao/ImageProcess/ImageApplyUV.h index 05a8e29a..bf1684ec 100644 --- a/huagao/ImageProcess/ImageApplyUV.h +++ b/huagao/ImageProcess/ImageApplyUV.h @@ -10,7 +10,7 @@ public: ~ImageApplyUV(); 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: void purgeQR_kernal(cv::Mat& image, const cv::Rect& roi, std::map map_color, int dpi, int threshold); void findContours1(const cv::Mat& src, std::vector>& contours, std::vector& hierarchy, @@ -20,3 +20,4 @@ private: cv::Mat lut; }; + diff --git a/huagao/stdafx.h b/huagao/stdafx.h index 44f46cf474ab0bfc9fcc18aa1cc0f45ed45824ff..9cdef02fed0e305b53455365599fc5e0fbe172dc 100644 GIT binary patch delta 30 mcmX?+a3o