| | |
| | | CPoint pos2I = m_pGlassData->TransformToGlobal((DimensionDir)pDefect->m_DefectInfo.m_nSideIdx, pDefect->m_DefectInfo.m_ptDefectPos_pxl); |
| | | #endif // HALCON_VISION_KEY |
| | | |
| | | |
| | | |
| | | |
| | | int nMinSize_X = m_pRecipe->m_SideParam[pDefect->m_DefectInfo.m_nSideIdx].m_InspectPrm[(int)pDefect->m_DefectInfo.m_DefectLoc].m_nMinSize_Filter_X_um; |
| | | int nMinSize_Y = m_pRecipe->m_SideParam[pDefect->m_DefectInfo.m_nSideIdx].m_InspectPrm[(int)pDefect->m_DefectInfo.m_DefectLoc].m_nMinSize_Filter_Y_um;; |
| | | |
| | |
| | | |
| | | BOOL CPostProcess::SaveDefectImage(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile,DimensionDir emDir) |
| | | { |
| | | FIBITMAP *bitmap = NULL; |
| | | BYTE *pBitBuffer; |
| | | if (pImage == NULL || szImage.cx <= 0 || szImage.cy <= 0) { |
| | | g_pLog->DisplayMessage(_T("Invalid image data or size")); |
| | | return FALSE; |
| | | } |
| | | |
| | | bitmap = FreeImage_Allocate(szImage.cx, szImage.cy,8); |
| | | Lock(); |
| | | FIBITMAP* bitmap = FreeImage_Allocate(szImage.cx, szImage.cy, 8); |
| | | if (bitmap == NULL) { |
| | | g_pLog->DisplayMessage(_T("FreeImage_Allocate failed")); |
| | | Unlock(); |
| | | return FALSE; |
| | | } |
| | | |
| | | pBitBuffer = FreeImage_GetBits(bitmap); |
| | | if(pBitBuffer == NULL) |
| | | { |
| | | BYTE* pBitBuffer = FreeImage_GetBits(bitmap); |
| | | if(pBitBuffer == NULL) { |
| | | FreeImage_Unload(bitmap); |
| | | Unlock(); |
| | | return FALSE; |
| | | } |
| | | |
| | | CopyMemory(pBitBuffer,pImage,szImage.cx*szImage.cy); |
| | | |
| | | FIBITMAP *pBit24 = FreeImage_ConvertTo24Bits(bitmap); |
| | | if(pBit24 == NULL) |
| | | if (pBit24 == NULL) { |
| | | Unlock(); |
| | | return FALSE; |
| | | } |
| | | |
| | | char cFilePath[255]; |
| | | |
| | |
| | | #endif |
| | | |
| | | FreeImage_FlipVertical(pBit24); |
| | | |
| | | FreeImage_Save(FIF_JPEG, pBit24, cFilePath,JPEG_RATE); |
| | | |
| | | FreeImage_Unload(bitmap); |
| | | FreeImage_Unload(pBit24); |
| | | |
| | | Unlock(); |
| | | |
| | | return TRUE; |
| | | } |
| | | |
| | | BOOL CPostProcess::SaveDefectImage2(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile) |
| | | BOOL CPostProcess::SaveDefectImageModern(LPBYTE pImage,CSize szImage,CRect rectDefect,CString strFile) |
| | | { |
| | | if (!pImage || szImage.cx <= 0 || szImage.cy <= 0) { |
| | | return FALSE; |
| | | } |
| | | |
| | | #if 0 |
| | | IplImage *pImageTmp = cvCreateImage(cvSize(szImage.cx, szImage.cy), 8,1); |
| | | cvZero(pImageTmp); |
| | | |
| | | // add point to the image |
| | | for (int i = 0; i<szImage.cy; i++) |
| | | { |
| | | for (int j = 0; j<szImage.cx; j++) |
| | | { |
| | | for (int i = 0; i < szImage.cy; i++) { |
| | | for (int j = 0; j < szImage.cx; j++) { |
| | | pImageTmp->imageData[i*pImageTmp->widthStep+j] = pImage[i*szImage.cx+j]; |
| | | } |
| | | } |
| | |
| | | cvZero(pColorImg); |
| | | |
| | | cvCvtColor(pImageTmp, pColorImg, CV_GRAY2BGR); |
| | | |
| | | cvRectangle(pColorImg,CvPoint(rectDefect.left,rectDefect.top), CvPoint(rectDefect.left+rectDefect.Width(),rectDefect.top+rectDefect.Height()), cvScalar(0,0,255), 1); |
| | | |
| | | USES_CONVERSION; |
| | |
| | | |
| | | cvReleaseImage(&pColorImg); |
| | | cvReleaseImage(&pImageTmp); |
| | | #else |
| | | Lock(); |
| | | cv::Mat grayImg(szImage.cy, szImage.cx, CV_8UC1, pImage); |
| | | cv::Mat colorImg; |
| | | cv::cvtColor(grayImg, colorImg, cv::COLOR_GRAY2BGR); |
| | | |
| | | cv::rectangle( |
| | | colorImg, |
| | | cv::Rect(rectDefect.left, rectDefect.top, rectDefect.Width(), rectDefect.Height()), |
| | | cv::Scalar(0, 0, 255), |
| | | 1 |
| | | ); |
| | | |
| | | USES_CONVERSION; |
| | | std::string strFilename = W2A(strFile); |
| | | |
| | | if (!cv::imwrite(strFilename, colorImg)) { |
| | | Unlock(); |
| | | g_pLog->DisplayMessage(_T("Failed to save image to %s"), strFile); |
| | | return FALSE; |
| | | } |
| | | Unlock(); |
| | | #endif // 0 |
| | | |
| | | return TRUE; |
| | | } |
| | | |
| | | BOOL CPostProcess::SaveDefectImage_with_Title(CDefect* pDefect, CSize szImage,CRect rectDefect,CString strFile, CvScalar color) |
| | | { |
| | | Lock(); |
| | | if (pDefect == NULL || pDefect->m_Image == NULL) { |
| | | g_pLog->DisplayMessage(_T("Invalid defect data or image")); |
| | | return FALSE; |
| | | } |
| | | |
| | | if (szImage.cx <= 0 || szImage.cy <= 0) { |
| | | g_pLog->DisplayMessage(_T("Invalid image size: %d x %d"), szImage.cx, szImage.cy); |
| | | return FALSE; |
| | | } |
| | | |
| | | BOOL bSuccess = TRUE; |
| | | |
| | | Lock(); |
| | | // 标题文本 |
| | | CString strTitle; |
| | | int nSideIdx = (0 <= pDefect->m_DefectInfo.m_nSideIdx && pDefect->m_DefectInfo.m_nSideIdx < MAX_SIDE_COUNT) ? pDefect->m_DefectInfo.m_nSideIdx : MAX_SIDE_COUNT; |
| | |
| | | } |
| | | Unlock(); |
| | | |
| | | // 成功标志 |
| | | BOOL bSuccess = TRUE; |
| | | |
| | | try { |
| | | // 将 CDefect 的 m_Image 缓冲区复制到 cv::Mat |
| | | Lock(); |
| | | cv::Mat imgTmp(szImage.cy, szImage.cx, CV_8UC1, pDefect->m_Image); |
| | | |
| | | // 创建彩色图像 |
| | | cv::Mat colorImg(szImage.cy, szImage.cx, CV_8UC3); |
| | | cv::cvtColor(imgTmp, colorImg, cv::COLOR_GRAY2BGR); |
| | | |
| | | // 在图像上画矩形 |
| | | cv::rectangle(colorImg, cv::Point(rectDefect.left, rectDefect.top), |
| | | cv::Point(rectDefect.right, rectDefect.bottom), color, 1); |
| | | cv::rectangle(colorImg, cv::Point(rectDefect.left, rectDefect.top), cv::Point(rectDefect.right, rectDefect.bottom), color, 1); |
| | | cv::rectangle(colorImg, cv::Point(0, szImage.cy - 40), cv::Point(szImage.cx, szImage.cy), cv::Scalar(0, 0, 0), cv::FILLED); |
| | | |
| | | // 添加标题 |
| | | cv::rectangle(colorImg, cv::Point(0, szImage.cy - 40), |
| | | cv::Point(szImage.cx, szImage.cy), cv::Scalar(0, 0, 0), cv::FILLED); |
| | | |
| | | // 设置字体 |
| | | int fontFace = cv::FONT_HERSHEY_TRIPLEX; |
| | | double fontScale = 0.7; |
| | | int thickness = 1; |
| | | |
| | | // 将标题文本放到图像上 |
| | | CT2CA pszConvertedAnsiTitle(strTitle); |
| | | cv::String strCVTitle(pszConvertedAnsiTitle); |
| | | cv::putText(colorImg, strCVTitle, cv::Point(10, szImage.cy - 10), |
| | | fontFace, fontScale, CV_RGB(255, 255, 255), thickness); |
| | | cv::putText(colorImg, strCVTitle, cv::Point(10, szImage.cy - 10), fontFace, fontScale, CV_RGB(255, 255, 255), thickness); |
| | | |
| | | // 将图像保存到文件 |
| | | CT2CA pszConvertedAnsiFile(strFile); |
| | | cv::String strStdFile(pszConvertedAnsiFile); |
| | | cv::imwrite(strStdFile, colorImg); |
| | | // cv::imwrite("D:\\Inspection\\Data\\IMG_DEFECT\\20250409\\Manual\\Manual_A_TOP_Chip_0.bmp", colorImg); |
| | | Unlock(); |
| | | } |
| | | catch (cv::Exception& e) { |
| | | // OpenCV异常处理 |
| | | bSuccess = FALSE; |
| | | Unlock(); |
| | | g_pLog->DisplayMessage(_T("OpenCV Exception: %s"), e.what()); |
| | | } |
| | | return bSuccess; |
| | | |