bool CDetectLine::RunThreshold() { try { threshold(m_Image, &m_Region, m_minGray, m_maxGray); if (HDevWindowStack::IsOpen()) { clear_window(HDevWindowStack::GetActive()); set_color(HDevWindowStack::GetActive(),"red"); disp_obj(m_Region, HDevWindowStack::GetActive()); connection(m_Region, &m_ConnectedRegions); //为了设定参数方便,显示面积在m_MinCirleArea/10~m_MaxCirleArea*10(Pixel)的区域面积值 Hobject selectRegions; HTuple RegionCount, Area, Row, Column; select_shape(m_ConnectedRegions, &selectRegions, "area", "and", m_minArea/10, m_maxArea*10); count_obj(selectRegions, &RegionCount); area_center(selectRegions, &Area, &Row, &Column); set_color(HDevWindowStack::GetActive(),"blue"); for (int i=1; i<=RegionCount; i+=1) { set_tposition(HDevWindowStack::GetActive(), HTuple(Row[i-1]), HTuple(Column[i-1])); write_string(HDevWindowStack::GetActive(), Area[i-1].D()); } } } catch(...) { AfxMessageBox("二值化失败!"); return false; } return true; }
void panTaskLogo::OnButtonClick( wxCommandEvent& event ) { Hobject region_loc, Image, TemplateImage, ModelContours; Hobject roi_loc; HTuple WindowHandle, ModelId, ModelRow, ModelColumn; HTuple ModelAngle, ModelScore, MatchingObjIdx, HomMat; roi_loc = c_pgvTask->gvTask_GetROI()->gvROI_GetROI(); Image = c_pgvVisionImage->gvIMG_GetImage(); if(c_pgvVisionImage->_insp_loc(Image,®ion_loc)) { set_system("border_shape_models", "false"); reduce_domain(Image, roi_loc, &TemplateImage); create_shape_model(TemplateImage, 4, HTuple(0).Rad(), HTuple(360).Rad(), HTuple(0.6824).Rad(), (HTuple("point_reduction_high").Append("no_pregeneration")), "use_polarity", ((HTuple(15).Append(19)).Append(6)), 4, &ModelId); get_shape_model_contours(&ModelContours, ModelId, 1); c_pgvTask->gvTask_SetModeId(ModelId); } else { wxMessageBox(wxT("未检测到产品,请调整产品位置后重新学习!")); } }
bool CDetectLine::RunSelectTarget() { if(RunThreshold()) { try { select_shape(m_ConnectedRegions, &m_SelectedRegions, (((((HTuple("area").Append("roundness")).Append("row")).Append("column")).Append("ra")).Append("rb")), "and", (((((HTuple(m_minArea).Append(m_minRectangularity)).Append(m_minRow)).Append(m_minColumn)).Append(m_minRA)).Append(m_minRB)), (((((HTuple(m_maxArea).Append(m_maxRectangularity)).Append(m_maxRow)).Append(m_maxColumn)).Append(m_maxRA)).Append(m_maxRB))); fill_up(m_SelectedRegions, &m_RegionFillUp); gen_contour_region_xld(m_RegionFillUp, &m_RectangleContours, "border"); distance_pc(m_RectangleContours, m_ImageHeight/2, m_ImageWidth/2, &m_DistanceMin, &m_DistanceMax); if (HDevWindowStack::IsOpen()) { clear_window(HDevWindowStack::GetActive()); set_color(HDevWindowStack::GetActive(),"red"); disp_obj(m_RectangleContours, HDevWindowStack::GetActive()); set_color(HDevWindowStack::GetActive(),"green"); disp_cross(HDevWindowStack::GetActive(), m_ImageHeight/2, m_ImageWidth/2, 20, 0); //disp_cross(HDevWindowStack::GetActive(), m_ImageHeight/2 + m_DistanceMin, m_ImageWidth/2, 20, 0); } return true; } catch(...) { AfxMessageBox("检测直线失败!"); } } return false; }
bool gvVisionTask_black::gvTask_inspect( Hobject image_src, Hobject *outRegion_defects ) { // Local iconic variables Hobject TMP_Region, Region1, RegionErosion1,ImageReduced; Hobject Image_meanSmall, Image_meanLarge, RegionDynThresh; Hobject ConnectedRegions, RegionFillUp, RegionErosion, ConnectedRegions1; Hobject SelectedRegions, RegionDilation1, RegionDilation; Hobject RegionDifference, obj_selected; // Local control variables HTuple Meancenter, Deviation, Meanouter, Deviation1; HTuple Index, Number; // reduce_domain(image_src, gvTask_GetROI()->gvROI_GetROI(), &ImageReduced); reduce_domain(image_src, gvTask_GetROI()->gvROI_GetROI(), &ImageReduced); threshold(ImageReduced, &Region1, 100, 255); fill_up(Region1, &Region1); erosion_circle(Region1, &RegionErosion1, 20); mean_image(ImageReduced, &Image_meanSmall, 2, 2); mean_image(ImageReduced, &Image_meanLarge, 20, 20); dyn_threshold(Image_meanSmall, Image_meanLarge, &RegionDynThresh, 5, "dark"); connection(RegionDynThresh, &ConnectedRegions); fill_up(ConnectedRegions, &RegionFillUp); erosion_circle(RegionFillUp, &RegionErosion, 1.5); connection(RegionErosion, &ConnectedRegions1); select_shape(ConnectedRegions1, &SelectedRegions, "area", "and", m_minBlackSpotArea, 5000); dilation_circle(SelectedRegions, &RegionDilation1, 2.5); intensity(RegionDilation1, Image_meanSmall, &Meancenter, &Deviation); dilation_circle(SelectedRegions, &RegionDilation, 5); difference(RegionDilation, SelectedRegions, &RegionDifference); intensity(RegionDifference, Image_meanSmall, &Meanouter, &Deviation1); gen_empty_obj(&(*outRegion_defects)); for (Index=0; Index<=(Meancenter.Num())-1; Index+=1) { select_obj(SelectedRegions, &obj_selected, Index+1); if (0 != ((HTuple(Meancenter[Index])-HTuple(Meanouter[Index]))<-30)) { concat_obj((*outRegion_defects), obj_selected, &(*outRegion_defects)); } } count_obj((*outRegion_defects), &Number); if(Number>m_maxBlackSpotCnt||Number<m_minBlackSpotCnt ) { return FALSE; } return TRUE; }
void action() { // Local iconic variables HObject ho_Rim, ho_Edges, ho_Holes, ho_Hole; // Local control variables HTuple hv_Width, hv_Height, hv_WindowID, hv_Row; HTuple hv_Column, hv_Radius, hv_Number, hv_i, hv_DistanceMin; HTuple hv_DistanceMax, hv_SVal, hv_Ascent, hv_Descent; HTuple hv_Width1, hv_Height1; //Example program for distance_pc //Determine the minimal and maximal radius of drill holes // //if (HDevWindowStack::IsOpen()) // CloseWindow(HDevWindowStack::Pop()); ReadImage(&ho_Rim, "rim"); GetImageSize(ho_Rim, &hv_Width, &hv_Height); SetWindowAttr("background_color","black"); OpenWindow(0,0,hv_Width,hv_Height,0,"","",&hv_WindowID); HDevWindowStack::Push(hv_WindowID); if (HDevWindowStack::IsOpen()) DispObj(ho_Rim, HDevWindowStack::GetActive()); //set_display_font(hv_WindowID, 14, "mono", "false", "false"); //Segment the circular holes //The alpha parameter was choosen different than the default value //to ensure stronger smoothing and thusmore connected edge components EdgesSubPix(ho_Rim, &ho_Edges, "canny", 4, 20, 40); SelectShapeXld(ho_Edges, &ho_Holes, "circularity", "and", 0.7, 1.0); SortContoursXld(ho_Holes, &ho_Holes, "upper_left", "true", "row"); //Determine the midpoints SmallestCircleXld(ho_Holes, &hv_Row, &hv_Column, &hv_Radius); CountObj(ho_Holes, &hv_Number); if (HDevWindowStack::IsOpen()) SetColor(HDevWindowStack::GetActive(),"yellow"); //For all boles ... { HTuple end_val20 = hv_Number; HTuple step_val20 = 1; for (hv_i=1; hv_i.Continue(end_val20, step_val20); hv_i += step_val20) { SelectObj(ho_Holes, &ho_Hole, hv_i); if (HDevWindowStack::IsOpen()) DispObj(ho_Rim, HDevWindowStack::GetActive()); if (HDevWindowStack::IsOpen()) DispObj(ho_Hole, HDevWindowStack::GetActive()); //Compute the minimal and maximal radius of the holes by computing the //distance of the midpoint to the contour DistancePc(ho_Hole, HTuple(hv_Row[hv_i-1]), HTuple(hv_Column[hv_i-1]), &hv_DistanceMin, &hv_DistanceMax); //And visualize the results (minimal and maximal radius) DispCross(hv_WindowID, HTuple(hv_Row[hv_i-1]), HTuple(hv_Column[hv_i-1]), 6, 0); TupleString(hv_DistanceMin, ".3f", &hv_SVal); GetStringExtents(hv_WindowID, "min: "+hv_SVal, &hv_Ascent, &hv_Descent, &hv_Width1, &hv_Height1); //disp_message(hv_WindowID, "min: "+hv_SVal, "image", HTuple(hv_Row[hv_i-1])-(2*hv_Ascent), // ((HTuple(hv_Column[hv_i-1])-HTuple(hv_Radius[hv_i-1]))-hv_Width1)-10, "black", // "true"); //disp_message(hv_WindowID, "max: "+(hv_DistanceMax.TupleString(".3f")), "image", // HTuple(hv_Row[hv_i-1]), ((HTuple(hv_Column[hv_i-1])-HTuple(hv_Radius[hv_i-1]))-hv_Width1)-10, // "black", "true"); //disp_continue_message(hv_WindowID, "black", "true"); // stop(); only in hdevelop } } //if (HDevWindowStack::IsOpen()) // CloseWindow(HDevWindowStack::Pop()); }
void CDoProcess::DoProcess(CMData* pData,SubTestResult *testItem) { // Local iconic variables Hobject Image=pData->m_Image; Hlong m_contrast_low1=m_Parameters.m_contrast_low; Hlong m_contrast_high1=m_Parameters.m_contrast_high; Hlong m_thred; ///////// if (m_contrast_low1>m_contrast_high1) { m_thred=m_contrast_low1; m_contrast_low1=m_contrast_high1; m_contrast_high1=m_thred; } if (m_Parameters.radius_inner>m_Parameters.radius_ext) { m_thred=m_Parameters.radius_inner; m_Parameters.radius_inner=m_Parameters.radius_ext; m_Parameters.radius_ext=m_thred; } Hobject Circle5, Circle6, RegionDifference_median,ModelContours; Hobject ImageReduced_median, Region_median, ConnectedRegions_median; Hobject SelectedRegions_median, RegionUnion_median, ImageReduced_median_extract; Hobject ModelImages, ModelRegions; // Local control variables HTuple Mean_median, Deviation_median; HTuple Area_baoChiJia, Row_baoChiJia, Column_baoChiJia; HTuple ModelID, Row2, Column2, Angle2, Score2; // Local control variables HTuple Row=pData->m_center_y; HTuple Column= pData->m_center_x; // dev_update_off(...); only in hdevelop try { ///Row_model_center = 237.659; //Column_model_center = 339.212; gen_circle(&Circle5, Row, Column,m_Parameters.radius_ext); gen_circle(&Circle6, Row, Column,m_Parameters.radius_inner); difference(Circle5, Circle6, &RegionDifference_median); reduce_domain(Image, RegionDifference_median, &ImageReduced_median); intensity(RegionDifference_median, Image, &Mean_median, &Deviation_median); fast_threshold(ImageReduced_median, &Region_median, m_Parameters.radius_inner_thred, 255, 1);//Mean_median*1.2 connection(Region_median, &ConnectedRegions_median); select_shape(ConnectedRegions_median, &SelectedRegions_median, "area", "and", m_Parameters.radius_ext_extract_size, 99999); union1(SelectedRegions_median, &RegionUnion_median); area_center(RegionUnion_median, &Area_baoChiJia, &Row_baoChiJia, &Column_baoChiJia); //创建模板时 保存区域和区域图片。在提取区域时,因为正常运行时每次都要用,不能使用 /*if (!_access(m_Parameters.CenterOcrModelRegion.c_str(),0)) { wstring msg = _T("Set Region already Exit.OverWrite or Not?"); if (MessageBox(NULL,msg.c_str(),_T("ReadFile Error"),MB_OK||MB_CANCELTRYCONTINUE)==IDOK) { write_region(RegionUnion_median,m_Parameters.CenterOcrModelRegion.c_str()); } } else*/ write_region(RegionUnion_median,m_Parameters.CenterOcrModelRegion.c_str()); reduce_domain(Image, RegionUnion_median, &ImageReduced_median_extract); //write_image(ImageReduced_median_extract, "bmp", 0, "D:/SLACTST/CH1.101/模板字符区域图片.bmp"); // /*if (!_access(m_Parameters.CenterOcrModelPicture.c_str(),0)) { wstring msg = _T("Set Picture already Exit.OverWrite or Not?"); if (MessageBox(NULL,msg.c_str(),_T("ReadFile Error"),MB_OK||MB_CANCELTRYCONTINUE)==IDOK) { write_image(ImageReduced_median_extract, "bmp", 0,m_Parameters.CenterOcrModelPicture.c_str()); } } else write_image(ImageReduced_median_extract, "bmp", 0,m_Parameters.CenterOcrModelPicture.c_str()); */ //匹配数据 inspect_shape_model(ImageReduced_median_extract, &ModelImages, &ModelRegions, 1, 25); create_shape_model(ImageReduced_median_extract, 4, 0, HTuple(360).Rad(), "auto", "auto", "use_polarity", m_contrast_high1, m_contrast_low1, &ModelID); find_shape_model(Image, ModelID, 0, HTuple(360).Rad(), 0.5, 1, 0.5, "least_squares", 0, 0.9, &Row2, &Column2, &Angle2, &Score2); /*if (!_access(m_Parameters.CenterOcrModelName.c_str(),0)) { wstring msg = _T("Set Model already Exit.OverWrite or Not?"); if (MessageBox(NULL,msg.c_str(),_T("ReadFile Error"),MB_OK||MB_CANCELTRYCONTINUE)==IDOK) { write_shape_model(ModelID,m_Parameters.CenterOcrModelName.c_str()); } } else*/ write_shape_model(ModelID,m_Parameters.CenterOcrModelName.c_str()); //*查看模板提取效果 set_shape_model_origin(ModelID, -Row_baoChiJia, -Column_baoChiJia); get_shape_model_contours(&ModelContours, ModelID, 1); /////////////////////////////////////////////////*/ // Number_QiPao=1; if (1) { //pData->m_isFail= false; //testItem->m_bFailSubTest = false; pData->m_center_y2=Row_baoChiJia[0].D(); pData->m_center_x2=Column_baoChiJia[0].D(); } else { //testItem->m_ErrorMsg = "Error No Find Center"; //testItem->m_ErrorY = 250; //testItem->m_ErrorX = 350; //testItem->m_ErrorR = 55; //set_color(pData->m_ShowHWindow,"red"); //disp_region(SelectedRegions_qiPao_extract,pData->m_ShowHWindow); //disp_circle(pData->m_ShowHWindow,Row_center,Column_center,85); } set_color(pData->m_ShowHWindow,"blue"); disp_obj(ModelContours,pData->m_ShowHWindow); set_color(pData->m_ShowHWindow,"green"); disp_region(RegionUnion_median,pData->m_ShowHWindow); disp_region(Circle5,pData->m_ShowHWindow); disp_region(Circle6,pData->m_ShowHWindow); set_color(pData->m_ShowHWindow,"red"); /////// } catch (HException &except) { pData->m_isFail = true; testItem->m_bFailSubTest = true; pData->m_orientationPhi=0; //testItem->m_ErrorMsg = except.file; //testItem->m_ErrorMsg += " "; //testItem->m_ErrorMsg += except.line; //testItem->m_ErrorMsg += " "; //testItem->m_ErrorMsg += except.message; testItem->m_ErrorY = 1; testItem->m_ErrorX = 1; testItem->m_ErrorR = 4; } }
void CDoProcess::DoProcess(CMData* pData,SubTestResult *testItem) { //适当添加查找模板的匹配参数以及匹配度参数 //float m_white_para=m_Parameters.thred_high/100.0; Hobject Image=pData->m_Image; Hobject Circle7, ImageReduced3; // Local control variables HTuple Row_define, Column_define,Angle_define, Score_define; //************** HTuple Row_center=pData->m_center_y; HTuple Column_center= pData->m_center_x; //HTuple Row_model_center ;//=pData->m_center_y2 //HTuple Column_model_center ;//=pData->m_center_x2 //HTuple m_model_area; try { //参数是否正确!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!修改模板提取函数,添加中心点保存(参数不能保存?) //area_center(m_Parameters.RegionDilation_model,&m_model_area,&Row_model_center,&Column_model_center); //缩小区域 匹配图案 gen_circle(&Circle7, Row_center, Column_center, m_Parameters.radius_detect); reduce_domain(Image, Circle7, &ImageReduced3); find_shape_model(ImageReduced3, m_Parameters.ModelID, 0, HTuple(360).Rad(), m_Parameters.Minscore, 1, 0.5, "least_squares", 4, m_Parameters.Match_Greediness, &Row_define, &Column_define, &Angle_define, &Score_define); // Hlong length; tuple_length(Angle_define,&length); if(0==length) { pData->m_isFail =true; testItem->m_bFailSubTest = true; #ifdef _ENGLISH set_color(pData->m_ShowHWindow,"red"); HTuple msg = HTuple("Matching Fail"); set_tposition( pData->m_ShowHWindow, 55, 200); write_string(pData->m_ShowHWindow,msg); #else set_color(pData->m_ShowHWindow,"red"); HTuple msg = HTuple("匹配失败"); set_tposition( pData->m_ShowHWindow, 55, 200); write_string(pData->m_ShowHWindow,msg); #endif return; } //添加判断,满足条件时,再检测//图案中心点不一定是瓶盖中心点 //if (Score_define>0.65) //{ //set_color(pData->m_ShowHWindow,"green"); // disp_obj(ImageEmphasize_no_ocr, pData->m_ShowHWindow); // set_color(pData->m_ShowHWindow,"red"); // disp_obj(SelectedRegions_detect, pData->m_ShowHWindow); // 匹配位置 pData->m_center_x3=Column_define[0].D(); pData->m_center_y3=Row_define[0].D(); pData->m_orientationPhi3=Angle_define[0].D(); // // } // else // { // pData->m_isFail = true; // testItem->m_bFailSubTest = true; //} if (ShowObject) { set_color(pData->m_ShowHWindow,"green"); disp_obj(Circle7, pData->m_ShowHWindow); set_color(pData->m_ShowHWindow,"red"); } } catch (HException &except) { pData->m_isFail = true; testItem->m_bFailSubTest = true; /*//testItem->m_ErrorMsg = except.file; //testItem->m_ErrorMsg += " "; //testItem->m_ErrorMsg += except.line; //testItem->m_ErrorMsg += " "; //testItem->m_ErrorMsg += except.message; testItem->m_ErrorY = 1; testItem->m_ErrorX = 1; testItem->m_ErrorR = 4;*/ if (pData->m_isDebug) { set_tposition( pData->m_ShowHWindow, 55, 230); #ifdef _ENGLISH write_string(pData->m_ShowHWindow,"MatchingParaDefineDll parameter values error,please re-adjust"); #else write_string(pData->m_ShowHWindow,"MatchingParaDefineDll程序参数设置出错!"); #endif } } }