From c049c2fdf2f5722e460d0dc5c7597bf819f2e2e5 Mon Sep 17 00:00:00 2001
From: LWQ <1806950222@qq.com>
Date: 星期四, 07 八月 2025 18:18:56 +0800
Subject: [PATCH] 添加缺陷原图保存功能
---
EdgeInspector_App/InterfaceManager.cpp | 325 +++++++++++++++++++++++++++++++++--------------------
1 files changed, 200 insertions(+), 125 deletions(-)
diff --git a/EdgeInspector_App/InterfaceManager.cpp b/EdgeInspector_App/InterfaceManager.cpp
index 20dd735..86bd20c 100644
--- a/EdgeInspector_App/InterfaceManager.cpp
+++ b/EdgeInspector_App/InterfaceManager.cpp
@@ -1,4 +1,4 @@
-#include "StdAfx.h"
+锘�#include "StdAfx.h"
#include "InterfaceManager.h"
#include "MulticamControl.h"
#include "RadientControl.h"
@@ -150,7 +150,7 @@
return 0;
}
-// End Signal阑 PLC俊辑 肋 给罐酒辑 Retry 持菌澜
+// End Signal闃� PLC淇婅緫 鑲� 缁欑綈閰掕緫 Retry 鎸佽弻婢�
void CInterfaceManager::PLCSignalRetry(SEND_MESSAGE_SECTION emSection,InspectToControlSignal emSendSignal,enInterface_Send_Control emControlSignal)
{
if(m_HardwareRecipe.m_nPLCSignalRetry <= 0)
@@ -505,7 +505,7 @@
m_PostProcess.SetView((void*) m_pView);
m_PostProcess.RunPostProcessing();
- // 馆靛矫 Scan End 脚龋 焊郴扁 傈俊 秦拎具茄促... Scan End 脚龋 焊尘锭 Loading捞 肯丰 救登搁 救焊郴瘤霸 阜囚乐澜..
+ // 棣嗛潧鐭� Scan End 鑴氶緥 鐒婇兇鎵� 鍌堜繆 绉︽嫀鍏疯寗淇�... Scan End 鑴氶緥 鐒婂皹閿� Loading鎹� 鑲赴 鏁戠櫥鎼� 鏁戠剨閮寸槫闇� 闃滃洑涔愭緶..
m_bGlassLoadingEnd = FALSE;
m_bPLCGlassLoading = FALSE;
@@ -589,35 +589,35 @@
pINSPECTFULLIMAGE_BUFFER pBuffer;
int nQuality = pHard->m_nSaveImageQuality;
- if(nQuality < 0 || nQuality > 100)
- nQuality = 30;
-
+ if (nQuality < 0 || nQuality > 100) {
+ nQuality = 30;
+ }
+
strFolder.Format(_T("%s"),m_PostProcess.GetResultPath(EM_RESULT_FULL));
CreateDirectory(strFolder,NULL);
strPanelID = g_pBase->m_strHPanelID;
- if(strPanelID.IsEmpty() == TRUE)
+ if (strPanelID.IsEmpty() == TRUE) {
strPanelID = _T("TestGlass");
+ }
- for(iCam=0;iCam<pHard->GetCameraCount();iCam++)
- {
- for(iScan=0;iScan<MAX_SCAN_COUNT;iScan++)
- {
+ for(iCam=0;iCam<pHard->GetCameraCount();iCam++) {
+ for(iScan=0;iScan<MAX_SCAN_COUNT;iScan++) {
pCamera = pHard->GetCameraSettings(iCam,iScan);
- if(pCamera == NULL)
+ if (pCamera == NULL) {
continue;
+ }
pBuffer = m_pInspect[iCam]->GetFullImgBuffer(iScan);
- if(pBuffer == NULL)
+ if (pBuffer == NULL) {
continue;
+ }
- g_pLog->DisplayMessage(_T("Sava %s iCam=%d, iScan=%d, szImage.cx=%ld, pBuffer->szImage.cy=%ld"),
- PANEL_SIDE[pCamera->m_eDimension], iCam, iScan, pBuffer->szImage.cx, pBuffer->szImage.cy);
+ g_pLog->DisplayMessage(_T("Sava %s iCam=%d, iScan=%d, szImage.cx=%ld, pBuffer->szImage.cy=%ld"), PANEL_SIDE[pCamera->m_eDimension], iCam, iScan, pBuffer->szImage.cx, pBuffer->szImage.cy);
- if(pBuffer->pImage != NULL && pBuffer->szImage.cx > 100 && pBuffer->szImage.cy > 100)
- {
+ if(pBuffer->pImage != NULL && pBuffer->szImage.cx > 100 && pBuffer->szImage.cy > 100) {
strFileJpg.Format(_T("%s\\%s_%s_%s.jpg"),strFolder, strPanelID, PANEL_SIDE[pCamera->m_eDimension],g_pBase->m_strLoadingTime);
- SaveFullImage2(strFileJpg, pBuffer->pImage, pBuffer->szImage.cx, pBuffer->szImage.cy, (int)pCamera->m_eDimension, 0, nQuality);
+ SaveFullImageModern(strFileJpg, pBuffer->pImage, pBuffer->szImage.cx, pBuffer->szImage.cy, (int)pCamera->m_eDimension, 0, nQuality);
}
}
}
@@ -1213,7 +1213,7 @@
if(!m_strReserveRecipe.IsEmpty())
{
g_pLog->DisplayMessage(_T("Apply reserve Recipe Loading - [%s]"), m_strReserveRecipe);
- if (LoadGlassRecipe(m_strReserveRecipe,TRUE)) // 鞍篮 Recipe扼档 利侩 抗距登绢 乐栏搁 促矫 肺爹茄促.
+ if (LoadGlassRecipe(m_strReserveRecipe,TRUE)) // 闉嶇 Recipe鎵兼。 鍒╀京 鎶楄窛鐧荤虎 涔愭爮鎼� 淇冪煫 鑲虹埞鑼勪績.
{
g_pLog->DisplayMessage(_T("Apply reserve Recipe Loading - [%s] : Success"), m_strReserveRecipe);
}
@@ -1227,7 +1227,7 @@
return FALSE;
}
- if (strRcp.CompareNoCase(m_GlassRecipe.GetRecipeName()) != 0) // strRecipeName 官差搁 肺爹茄促.
+ if (strRcp.CompareNoCase(m_GlassRecipe.GetRecipeName()) != 0) // strRecipeName 瀹樺樊鎼� 鑲虹埞鑼勪績.
{
if (LoadGlassRecipe(strRcp,TRUE) == FALSE)
{
@@ -1393,7 +1393,7 @@
BlVision_GetVisionRecipe()->setRecipeType(EI_VISION_RECIPE);
BlVision_GetVisionRecipe()->init();
- for (int i = 0; i < nCount; i++) { //设置分辨率
+ for (int i = 0; i < nCount; i++) { //璁剧疆鍒嗚鲸鐜�
DimensionDir eDir = (DimensionDir)(i);
CBlSideData *pSide = BlVision_GetVisionRecipe()->getSideData(eDir);
@@ -1443,7 +1443,7 @@
g_pLog->DisplayMessage( _T("CreateCamera Start!"));
//////////////////////////////////////////////////////////////////////////
- // Camera Handle 积己.
+ // Camera Handle 绉繁.
int iCam;
BOOL bRet = TRUE;
@@ -1540,7 +1540,7 @@
int nErrorCode;
int nBoardId = iCam;
- // B5 4龋扁 悸泼
+ // B5 4榫嬫墎 鎮告臣
g_pBase->m_strSBKLine = m_HardwareRecipe.GetLineID();
g_pBase->m_strSBKLine.Append(_T("#"));
@@ -1821,56 +1821,52 @@
{
g_pStatus->CheckDirectory(strPath);
- FIBITMAP *bitmap = NULL;
- BYTE *pBitBuffer;
+ CString strSavePath = strPath;
+ strSavePath += _T(".jpg");
- CString savePath = strPath;
- savePath += _T(".jpg");
-
- bitmap = FreeImage_Allocate(IMAGE_WIDTH, nLineCnt,8);
-
- pBitBuffer = FreeImage_GetBits(bitmap);
- if(pBitBuffer == NULL)
- {
- FreeImage_Unload(bitmap);
+ Lock();
+ FIBITMAP* bitmap = FreeImage_Allocate(IMAGE_WIDTH, nLineCnt, 8);
+ if (bitmap == NULL) {
+ Unlock();
return FALSE;
- }
+ }
- LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide,ptStart.y);
- if(lpImg == NULL)
+ BYTE* pBitBuffer = FreeImage_GetBits(bitmap);
+ if (pBitBuffer == NULL) {
+ FreeImage_Unload(bitmap);
+ Unlock();
return FALSE;
+ }
- CopyMemory(pBitBuffer,lpImg,IMAGE_WIDTH*nLineCnt);
+ LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide, ptStart.y);
+ if (lpImg == NULL) {
+ Unlock();
+ return FALSE;
+ }
+
+ CopyMemory(pBitBuffer, lpImg, IMAGE_WIDTH * nLineCnt);
USES_CONVERSION;
char str_filename[1024];
- sprintf_s(str_filename, "%s", W2A(savePath));
+ sprintf_s(str_filename, "%s", W2A(strSavePath));
FreeImage_FlipVertical(bitmap);
+ FreeImage_Save(FIF_JPEG, bitmap, str_filename, JPEG_FULLIMAGE_RATE);
+ FreeImage_Unload(bitmap);
- FreeImage_Save(FIF_JPEG, bitmap, str_filename,JPEG_FULLIMAGE_RATE);
-
- FreeImage_Unload(bitmap);
+ Unlock();
return TRUE;
}
-BOOL CInterfaceManager::SaveFullImage2(CString strPath,LPBYTE lpOrigin,int nImgWidth,int nImgHeight,int iSide,int nStartY,int nQuality /*= 80*/)
+BOOL CInterfaceManager::SaveFullImageModern(CString strPath,LPBYTE lpOrigin,int nImgWidth,int nImgHeight,int iSide,int nStartY,int nQuality /*= 80*/)
{
- if(lpOrigin == NULL)
- return FALSE;
-
- if(nImgHeight < 100)
- {
- g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), Height is 0"), strPath, PANEL_SIDE[iSide], nImgHeight);
+ if (!lpOrigin || nImgHeight < 100) {
+ g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), Invalid image or height"), strPath, PANEL_SIDE[iSide], nImgHeight);
return FALSE;
}
- //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 1"), strPath, PANEL_SIDE[iSide], nImgHeight);
-
g_pStatus->CheckDirectory(strPath);
-
- //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 2"), strPath, PANEL_SIDE[iSide], nImgHeight);
int p[3];
p[0] = CV_IMWRITE_JPEG_QUALITY;
@@ -1880,143 +1876,222 @@
double tmp = 70 / 100.0;
double dRatio = 1. - tmp;
dRatio = dRatio - 0.01 < 0.0 ? 1.0 : dRatio;
- IplImage *Img = cvCreateImageHeader(cvSize(nImgWidth,nImgHeight),8,1);
- IplImage *img2 = cvCreateImage(cvSize((int)(nImgWidth* dRatio),(int)(nImgHeight*dRatio)),8,1);
- //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 3"), strPath, PANEL_SIDE[iSide], nImgHeight);
+#if 0
+ IplImage* pImg = cvCreateImageHeader(cvSize(nImgWidth, nImgHeight), 8, 1);
+ IplImage* pImgNew = cvCreateImage(cvSize((int)(nImgWidth * dRatio), (int)(nImgHeight * dRatio)), 8, 1);
- if(Img == NULL || img2 == NULL)
- {
+ if (pImg == NULL || pImgNew == NULL) {
g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), img pointer is null"), strPath, PANEL_SIDE[iSide], nStartY);
return FALSE;
}
- g_pLog->DisplayMessage(_T("Save Full Image Start(%s, %s, %d, %d, %d)"), strPath, PANEL_SIDE[iSide], nStartY, nImgHeight, nQuality);
+ g_pLog->DisplayMessage(_T("Save Full Image Start(%s, %s, %d, %d, %d)"), strPath, PANEL_SIDE[iSide], nStartY, nImgHeight, nQuality);
- if(nStartY < 0 || iSide < 0 || iSide >= MAX_DIMENSION_COUNT)
- {
+ if (nStartY < 0 || iSide < 0 || iSide >= MAX_DIMENSION_COUNT) {
g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d)"), strPath, PANEL_SIDE[iSide], nStartY);
return FALSE;
}
- LPBYTE lpImg = lpOrigin+(nStartY*nImgWidth);
- if(lpImg == NULL)
- {
+ LPBYTE lpImg = lpOrigin + (nStartY * nImgWidth);
+ if (lpImg == NULL) {
g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), Buffer is NULL"), strPath, PANEL_SIDE[iSide], nStartY);
return FALSE;
}
- //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 4"), strPath, PANEL_SIDE[iSide], nImgHeight);
+ cvSetData(pImg, lpImg, nImgWidth);
- cvSetData(Img,lpImg,nImgWidth);
-
- cvResize(Img,img2);
+ cvResize(pImg, pImgNew);
USES_CONVERSION;
char str_filename[200];
sprintf_s(str_filename, "%s", W2A(strPath));
- cvSaveImage(str_filename,img2, p);
+ cvSaveImage(str_filename, pImgNew, p);
- //g_pLog->DisplayMessage(_T("Save Full Image (%s, %s, %d) 5"), strPath, PANEL_SIDE[iSide], nImgHeight);
+ cvReleaseImage(&pImgNew);
+ cvReleaseImageHeader(&pImg);
+#else
+ Lock();
+ // 鍘熷鍥惧儚鏁版嵁杞� cv::Mat
+ cv::Mat origin(nImgHeight, nImgWidth, CV_8UC1, lpOrigin + (nImgWidth * nStartY));
- cvReleaseImage(&img2);
- cvReleaseImageHeader(&Img);
+ // 缂╂斁鍥惧儚
+ cv::Size szNew(static_cast<int>(nImgWidth * dRatio), static_cast<int>(nImgHeight * dRatio));
+ cv::Mat resized;
+ cv::resize(origin, resized, szNew, 0, 0, cv::INTER_LINEAR);
+
+ // 璁剧疆 JPEG 鍘嬬缉鍙傛暟
+ std::vector<int> params = {
+ cv::IMWRITE_JPEG_QUALITY,
+ nQuality
+ };
+
+ USES_CONVERSION;
+ std::string strFilename = W2A(strPath);
+
+ if (!cv::imwrite(strFilename, resized, params)) {
+ Unlock();
+ g_pLog->DisplayMessage(_T("Save Full Image Fail(%s, %s, %d), imwrite failed"), strPath, PANEL_SIDE[iSide], nStartY);
+ return FALSE;
+ }
+
+ Unlock();
+#endif
g_pLog->DisplayMessage(_T("Save Full Image Success(%s, %s, %d, %d, %d)"), strPath, PANEL_SIDE[iSide], nStartY, nImgHeight, nQuality);
return TRUE;
}
-#define IMAGE_COMPRESSRATIO 50
+#define IMAGE_COMPRESSRATIO 50
int CInterfaceManager::LoadFullImage(CString strPath,int iSide,int nStartY, CGlass_Data *pGlassData,const bool bIsDebugImg)
{
- int nHeight = -1;
+ int nHeight = -1;
+ if (strPath.IsEmpty() || iSide < 0 || iSide > MAX_DIMENSION_COUNT) {
+ g_pLog->DisplayMessage(_T("Load Full Image Fail(%s, %d)"), strPath, iSide);
+ return nHeight;
+ }
+#if 0
USES_CONVERSION;
char str_filename[200];
sprintf_s(str_filename, "%s", W2A(strPath));
- IplImage *Iplimg = cvLoadImage(str_filename, CV_LOAD_IMAGE_GRAYSCALE);
- LPBYTE lpImg = NULL;
+ LPBYTE lpImg = NULL;
+ IplImage *pIplimg = cvLoadImage(str_filename, CV_LOAD_IMAGE_GRAYSCALE);
- if(Iplimg)
- {
- if(bIsDebugImg == true) //Debug image
- {
+ if(pIplimg) {
+ if(bIsDebugImg == true) {
+ //Debug image
UINT uiReadLine = 0;
- // lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY);
- // if(lpImg != NULL)
- // {
- // memcpy(lpImg, &Iplimg->imageData[0] , Iplimg->widthStep*Iplimg->height);
- // }
- //
- for(int i = 0; i < Iplimg->height; i++)
- {
+ lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY);
+ if (lpImg != NULL) {
+ memcpy(lpImg, &pIplimg->imageData[0], pIplimg->widthStep * pIplimg->height);
+ }
+
+ for(int i = 0; i < pIplimg->height; i++) {
lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY+i);
- if(lpImg == NULL)
+ if (lpImg == NULL) {
break;
- memcpy(lpImg, &Iplimg->imageData[uiReadLine] , Iplimg->widthStep);
- uiReadLine += Iplimg->widthStep;
+ }
+
+ memcpy(lpImg, &pIplimg->imageData[uiReadLine] , pIplimg->widthStep);
+ uiReadLine += pIplimg->widthStep;
}
- if(pGlassData != NULL)
- {
+ if(pGlassData != NULL) {
CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide);
- if(pSideData != NULL)
- {
+ if(pSideData != NULL) {
pSideData->m_nGlassStartLine = 0;
- pSideData->m_nGlassEndLine = Iplimg->height + nStartY;
+ pSideData->m_nGlassEndLine = pIplimg->height + nStartY;
}
}
- nHeight = Iplimg->height;
- cvReleaseImage(&Iplimg);
+ nHeight = pIplimg->height;
+ cvReleaseImage(&pIplimg);
}
- else //Full image
- {
+ else {
+ //Full image
const double dRatio = 1.0 - static_cast<double>(IMAGE_COMPRESSRATIO / 100.0);
- IplImage *resizeImage = cvCreateImage(cvSize((int)(Iplimg->width/dRatio), (int)(Iplimg->height/dRatio)), IPL_DEPTH_8U, 1);
+ IplImage *pResizeImage = cvCreateImage(cvSize((int)(pIplimg->width/dRatio), (int)(pIplimg->height/dRatio)), IPL_DEPTH_8U, 1);
- cvResize(Iplimg, resizeImage, CV_INTER_CUBIC);
-
+ cvResize(pIplimg, pResizeImage, CV_INTER_CUBIC);
lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY);
- if(lpImg != NULL)
- {
- memcpy(lpImg, &Iplimg->imageData[0] , resizeImage->widthStep*resizeImage->height);
+ if(lpImg != NULL) {
+ memcpy(lpImg, &pIplimg->imageData[0] , pResizeImage->widthStep * pResizeImage->height);
}
- // UINT uiReadLine = 0;
- // for(int i = 0; i < resizeImage->height; i++)
- // {
- // lpImg = GetGrabBuffer((DimensionDir)iSide,nStartY+i);
- // if(lpImg == NULL)
- // break;
- // memcpy(lpImg, &resizeImage->imageData[uiReadLine] , resizeImage->widthStep);
- // uiReadLine += resizeImage->widthStep;
- // }
+ UINT uiReadLine = 0;
+ for (int i = 0; i < pResizeImage->height; i++) {
+ lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY + i);
+ if (lpImg == NULL) {
+ break;
+ }
- if(pGlassData != NULL)
- {
+ memcpy(lpImg, &pResizeImage->imageData[uiReadLine], pResizeImage->widthStep);
+ uiReadLine += pResizeImage->widthStep;
+ }
+
+ if(pGlassData != NULL) {
CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide);
- if(pSideData != NULL)
- {
+ if(pSideData != NULL) {
pSideData->m_nGlassStartLine = 0;
- pSideData->m_nGlassEndLine = Iplimg->height + nStartY;
+ pSideData->m_nGlassEndLine = pIplimg->height + nStartY;
}
}
- nHeight = resizeImage->height;
- cvReleaseImage(&Iplimg);
- cvReleaseImage(&resizeImage);
+ nHeight = pResizeImage->height;
+ cvReleaseImage(&pIplimg);
+ cvReleaseImage(&pResizeImage);
}
strPath.Append(_T("_OK"));
}
- else
+ else {
strPath.Append(_T("_NG"));
+ }
+#else
+ // 鍔犺浇鐏板害鍥惧儚
+ std::string strFilename = CT2A(strPath);
+ cv::Mat img = cv::imread(strFilename, cv::IMREAD_GRAYSCALE);
+
+ if (!img.empty()) {
+ g_pLog->DisplayMessage(_T("Image Size: %d x %d"), img.cols, img.rows);
+
+ if (bIsDebugImg) {
+ for (int i = 0; i < img.rows; ++i) {
+ LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY + i);
+ if (lpImg == NULL) {
+ break;
+ }
+
+ memcpy(lpImg, img.ptr(i), img.cols);
+ }
+
+ if (pGlassData != NULL) {
+ CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide);
+ if (pSideData != NULL) {
+ pSideData->m_nGlassStartLine = 0;
+ pSideData->m_nGlassEndLine = img.rows + nStartY;
+ }
+ }
+
+ nHeight = img.rows;
+ }
+ else {
+ // 鎸夋瘮渚嬬缉鏀�
+ const double dRatio = 1.0 - static_cast<double>(IMAGE_COMPRESSRATIO) / 100.0;
+ int newWidth = static_cast<int>(img.cols / dRatio);
+ int newHeight = static_cast<int>(img.rows / dRatio);
+
+ cv::Size newSize(newWidth, newHeight);
+ cv::Mat resized;
+ cv::resize(img, resized, newSize, 0, 0, cv::INTER_CUBIC);
+
+ LPBYTE lpImg = GetGrabBuffer((DimensionDir)iSide, nStartY);
+ if (lpImg != NULL) {
+ memcpy(lpImg, resized.data, resized.cols * resized.rows);
+ }
+
+ if (pGlassData != NULL) {
+ CSide_Data* pSideData = pGlassData->GetSideData((DimensionDir)iSide);
+ if (pSideData != NULL) {
+ pSideData->m_nGlassStartLine = 0;
+ pSideData->m_nGlassEndLine = img.rows + nStartY;
+ }
+ }
+
+ nHeight = resized.rows;
+ }
+
+ strPath.Append(_T("_OK"));
+ }
+ else {
+ strPath.Append(_T("_NG"));
+ }
+#endif // 0
g_pLog->DisplayMessage(_T("%s"),(TCHAR*)(LPCTSTR)strPath);
-
SetSlashText(GLOBAL_DEFINE::emText, strPath, RGB(0,0,0));
return nHeight;
--
Gitblit v1.9.3