/* * ***************************************************************** * * * * * Copyright (c) Fast Corporation, 1997 * * * * * * All Rights Reserved. Unpublished rights reserved under * * * the copyright laws of the Japan. * * * * * * The software contained on this media is proprietary to * * * and embodies the confidential technology of Fast * * * Corporation. Possession, use, duplication or * * * dissemination of the software and media is authorized only * * * pursuant to a valid written license from Fast Corporation. * * * * * ***************************************************************** */ /* CSC90X グレイサーチによる処理ウインドアライメント後2値計測  GSMEAS.C */ /*[作成者]H.Yagi */ /* 目的: 関数: 履歴: Ver 1.0 97/11/14 Ver 2.0 99/05/12 エラーメッセージと処理時間表示が重なる不具合を修正 Ver 2.1 99/11/09 フリーランし忘れと、ヘルプメッセージの変更 Ver 3.0 00/05/16 2値計測をシステム構築対応に変更 注記:m_menu.hをインクルードして下さい。 m_menu.c及びm_note.cをリンクして下さい。 */ /* * Include compiler runtime library */ #include #include /* * Include CSC90X library */ #include "f_stdlib.h" #include "f_time.h" #include "f_gui.h" #include "f_stdio.h" #include "f_graph.h" #include "f_image.h" #include "f_pinf.h" #include "f_video.h" #include "f_system.h" #include "f_gray.h" #include "f_search.h" #include "f_meas.h" #include "f_exmeas.h" /* * Include CSC90X common local */ #include "m_menu.h" /* * プロトタイプ宣言 */ void main( void ); void main_menu_disp( void ); void disp_help( void ); void set( void ); void wind_set( int *, int *, int *, int * ); void ptn_reg( void ); void ptn_disp( void ); void gs_joken( void ); void meas_set( void ); void pdm_bin_level( int, int, PARADIGM * ); void pdm_wind_set( int, int, PARADIGM * ); int search_exec( void ); void meas_exec( void ); int d4i5_d( double ); void exec( void ); extern int message_note( void ); /* * メニュー項目 */ #define LANG_N 2 #define MAIN_MENU_N 4 static const char *str_main_menu[MAIN_MENU_N][LANG_N] = { { " SET ", " 設 定 " }, { " EXEC ", " 実 行 " }, { " PROC3 ", " 処理3 " }, { " PROC4 ", " 処理4 " } }; #define INIT_CUR_POS_X 255 #define INIT_CUR_POS_Y 239 static char *select[3] = { "通常", " 高", "超高" }; static char *select_color[2] = { "黒", "白" }; int pad_level_ms; static PVAL value_meas[6]; int sts; static char ss[100]; double Mas_x; double Mas_y; double zure_x; double zure_y; int name; /* サーチパタン名称 */ int gs_wxs; /* サーチ範囲始点X */ int gs_wys; /* サーチ範囲始点Y */ int gs_wxe; /* サーチ範囲終点X */ int gs_wye; /* サーチ範囲終点Y */ int seido; /* 精度 */ int complex; /* 複雑度 */ int mid_lower; /* 途中下限値 */ int last_lower; /* 最終下限値 */ int bin_level; /* 2値レベル */ int b_wxs; /* 計測範囲始点X */ int b_wys; /* 計測範囲始点Y */ int b_wxe; /* 計測範囲終点X */ int b_wye; /* 計測範囲終点Y */ int color; /* 対象色 */ int meas_edge; /* 接触塊オプション */ int l_limit; /* 面積下限値 */ int u_limit; /* 面積上限値 */ /* * メイン */ void main( void ) { int sts; int xpos; int ypos; int s_xpos; int s_ypos; /* 初期メッセージ表示 */ if( NORMAL_RETURN != message_note() ) return; /* パラメタ初期化 */ xpos = INIT_CUR_POS_X; ypos = INIT_CUR_POS_Y; s_xpos = INIT_CUR_POS_X; s_ypos = INIT_CUR_POS_Y; name = 0x30303030; /* 0000 */ gs_wxs = 0; gs_wys = 0; gs_wxe = 511; gs_wye = 479; seido = 1; complex = 1; mid_lower = 5000; last_lower = 7000; bin_level = 128; b_wxs = 0; b_wys = 0; b_wxe = 511; b_wye = 479; color = WHITE_COLOR; meas_edge = ON; l_limit = 10; u_limit = 245760; /* 入力ビデオ制御 */ Lib_input_video_control( GRAY_PLANE | BIN_PLANE ); /* ビデオ出力表示項目制御 */ Lib_display_control( GRAY_PLANE | LINE_PLANE | CHAR_PLANE ); /* ビデオ表示項目クリア */ Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); /* カーソル初期化 */ Lib_init_cursor(); /* メインメニューの表示 */ main_menu_disp(); /* マウスカーソルの表示 */ Lib_draw_cursor( INIT_CUR_POS_X, INIT_CUR_POS_Y ); /*GRAY_PTN.SYSを使用する*/ if ( NORMAL_RETURN == Lib_gs_open_data_file( CONTINUE_PTN_AREA ) ) { /* メニュー制御 */ for (;;) { sts = 0; /* マウス位置読みとり */ sts = Lib_see_current_position( &xpos, &ypos ); if ( s_xpos != xpos || s_ypos != ypos ) { /* マウス表示位置移動 */ Lib_move_cursor( xpos, ypos ); s_xpos = xpos; s_ypos = ypos; } /* 処理振り分け */ if( CURSOR_EXECUTE == sts ) { if ( xpos > MENU_1_XS && xpos < MENU_1_XE && ypos > MENU_1_YS && ypos < MENU_1_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); set(); /*設定*/ Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); main_menu_disp(); } else if ( xpos > MENU_2_XS && xpos < MENU_2_XE && ypos > MENU_2_YS && ypos < MENU_2_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); exec(); /*実行*/ main_menu_disp(); } else if ( xpos > MENU_H_XS && xpos < MENU_H_XE && ypos > MENU_H_YS && ypos < MENU_H_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); disp_help(); /* ヘルプ */ main_menu_disp(); } else if ( xpos > MENU_E_XS && xpos < MENU_E_XE && ypos > MENU_E_YS && ypos < MENU_E_YE ) { break; /* 終了 */ } } } Lib_gs_close_data_file(); } else Lib_display_message( 100, 200, "エラー", "パタンエリアは使用できません" ); } /* * メインメニュー表示 */ static void main_menu_disp( void ) { int iLanguage; /* 日本語/英語表示文字列切替情報取得 */ iLanguage = Lib_get_disp_language(); /* 整列キー表示 -> ( m_menu.c ) */ SUB_menu_disp4he( (char *)str_main_menu[0][iLanguage], (char *)str_main_menu[1][iLanguage], (char *)NULL, (char *)NULL ); } /* * ヘルプ表示 */ static void disp_help( void ) { Lib_chrdisp( 5, 6, "【機能\概要】 GSMEAS" ); Lib_chrdisp( 6, 8, "グレイサーチにより処理範囲をアライメント後" ); Lib_chrdisp( 6, 10, "2値計測を実行し重心位置を表\示します。" ); Lib_chrdisp( 6, 12, "2値計測はシステム構\築対応です。" ); } /************************************************************************/ /* 設定 */ /************************************************************************/ void set( void ) { int rtn; int pad_level; int status_flag; pad_level = Lib_view_open(); Lib_view_set_title( pad_level, "設定" ); Lib_view_set_comment( pad_level, 5, 5, "サーチ設定" , 0 ); Lib_view_set_null ( pad_level, 10, 35, "サーチ条件" , 1 ); Lib_view_set_null ( pad_level, 10, 65, "サーチ範囲" , 2 ); Lib_view_set_null ( pad_level, 10, 95, "パタン登録" , 3 ); Lib_view_set_null ( pad_level, 10, 125, "パタン表\示", 4 ); Lib_view_set_null ( pad_level, 5, 160, "2値設定" , 5 ); Lib_view_set_size( pad_level, 50, 80, 5, 5 ); Lib_draw_command( pad_level ); for ( status_flag = ON; OFF != status_flag; ) { if ( ERROR_RETURN != ( rtn = Lib_process_command( pad_level ) ) ) { switch( rtn ) { case 0: /* */ break; case 1: /* サーチ条件 */ gs_joken(); break; case 2: /* サーチ範囲 */ wind_set( &gs_wxs, &gs_wys, &gs_wxe, &gs_wye ); break; case 3: /* パタン登録 */ ptn_reg(); break; case 4: /* パタン表示 */ ptn_disp(); break; case 5: /* 2値設定 */ meas_set(); break; case 102: /* 終了 */ status_flag = OFF; break; } } } Lib_erase_command( pad_level ); Lib_view_close( pad_level ); } /************************************************************************/ /* サーチ条件設定 */ /************************************************************************/ void gs_joken( void ) { int pad_level_gs; PVAL value_gs[4]; int no; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); pad_level_gs = Lib_view_open(); Lib_view_set_title( pad_level_gs, "サーチ条件" ); Lib_view_set_select ( pad_level_gs, 5, 5, "精度" , seido - 1, 3 , select, 0 ); Lib_view_set_uniq_numeral( pad_level_gs, 5, 35, "複雑度" , complex, 1, 9, 1 ); Lib_view_set_uniq_numeral( pad_level_gs, 5, 65, "途中下限値", mid_lower , 1000, 9999 , 2 ); Lib_view_set_uniq_numeral( pad_level_gs, 5, 95, "最終下限値", last_lower, 1000, 9999 , 3 ); Lib_view_set_size( pad_level_gs, 100, 130, 35, 5 ); Lib_draw_menu( pad_level_gs ); if( ERROR_RETURN != ( no = Lib_process_menu( pad_level_gs, value_gs ) ) ) { switch( no ) { case 101: /*「実行」が選択された */ seido = value_gs[0].select_type + 1; complex = value_gs[1].value_type; mid_lower = value_gs[2].value_type; last_lower = value_gs[3].value_type; break; case 102: /*「取り消し」が選択された */ break; } } Lib_erase_menu( pad_level_gs ); Lib_view_close( pad_level_gs ); Lib_recover_plane( idc ); } /*********************************************************************/ /*  ウインド設定                          */ /*********************************************************************/ void wind_set( xs, ys, xe, ye ) int *xs, *ys, *xe, *ye; /* 入出力:ウインド位置 */ { int pad_level_wind; PVAL value_wind[2]; int no; int x_size,y_size; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); pad_level_wind = Lib_view_open(); Lib_view_set_title( pad_level_wind, "処理範囲" ); x_size = *xe - *xs + 1; y_size = *ye - *ys + 1; Lib_view_set_box( pad_level_wind, 5, 5, "始点" , *xs , *ys , 3, 1, 0 ); Lib_view_set_box( pad_level_wind, 5, 35, "サイズ", x_size, y_size, 3, 0, 1 ); Lib_view_set_size( pad_level_wind, 100, 130, 5, 5 ); Lib_draw_menu( pad_level_wind ); if( ERROR_RETURN != ( no = Lib_process_menu( pad_level_wind, value_wind ) ) ) { switch( no ) { case 101: /*「実行」が選択された */ *xs = value_wind[0].box_type.x; *ys = value_wind[0].box_type.y; *xe = value_wind[0].box_type.x + value_wind[1].box_type.x - 1; *ye = value_wind[0].box_type.y + value_wind[1].box_type.y - 1; break; case 102: /*「取り消し」が選択された */ break; } } Lib_erase_menu( pad_level_wind ); Lib_view_close( pad_level_wind ); Lib_recover_plane( idc ); } /************************************************************************/ /* パタン登録 */ /************************************************************************/ void ptn_reg( void ) { int out_inf[8]; int error; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); Lib_freeze( TRANSMIT ); if ( 0 == ( error = Lib_gs_defpat( name ) ) ) { /* アライメントマーク登録位置の取得 */ Lib_gs_infpat( name, out_inf ); Mas_x = (double)out_inf[4] + ( (double)out_inf[2] / 10.0 ); Mas_y = (double)out_inf[5] + ( (double)out_inf[3] / 10.0 ); } else if ( -1 == error ) Lib_display_message( 150, 200, "エラー", "既に200個登録済みです" ); else if ( -2 == error ) Lib_display_message( 150, 200, "エラー", "パタン格納領域不足です" ); Lib_freerun(); Lib_recover_plane( idc ); } /************************************************************************/ /* パタン表示 */ /************************************************************************/ void ptn_disp( void ) { int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); Lib_gs_dsppat( ); Lib_freerun(); Lib_recover_plane( idc ); } /************************************************************************/ /* 設定 */ /************************************************************************/ void meas_set( ) { int no; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); pad_level_ms = Lib_view_open(); Lib_view_set_title( pad_level_ms, "2値設定" ); Lib_view_set_uniq_numeral( pad_level_ms, 5, 5, "2値レベル", bin_level, 0, 255, 0 ); Lib_view_set_null ( pad_level_ms, 5, 35, "計測範囲" , 1 ); Lib_view_set_select ( pad_level_ms, 5, 65, "対象色" , color, 2, select_color, 2 ); Lib_view_set_uniq_alter ( pad_level_ms, 5, 95, "境界" , meas_edge, 3 ); Lib_view_set_uniq_numeral( pad_level_ms, 5, 125, "面積下限値", l_limit, 0, 245760, 4 ); Lib_view_set_uniq_numeral( pad_level_ms, 5, 155, "面積上限値", u_limit, 0, 245760, 5 ); Lib_set_paradigm( pad_level_ms, 0, WHOLE_TIMING, pdm_bin_level ); Lib_set_paradigm( pad_level_ms, 1, START_TIMING, pdm_wind_set ); Lib_view_set_size( pad_level_ms, 50, 50, 5, 5 ); Lib_draw_menu( pad_level_ms ); if ( ERROR_RETURN != ( no = Lib_process_menu( pad_level_ms, value_meas ) ) ) /* メニュー値の取得 */ { switch( no ) { case 101: /*「実行」が選択された */ bin_level = value_meas[0].value_type; color = value_meas[2].select_type; meas_edge = value_meas[3].on_off_type; l_limit = value_meas[4].value_type; u_limit = value_meas[5].value_type; break; case 102: /*「取り消し」が選択された */ break; } } Lib_erase_menu( pad_level_ms ); Lib_view_close( pad_level_ms ); Lib_recover_plane( idc ); } /*********************************************************************/ /*  2値レベル設定                         */ /*********************************************************************/ void pdm_bin_level( Timing, Numb, val ) int Timing; int Numb; PARADIGM val[]; { if ( Timing == START_TIMING ) { Lib_erase_menu( pad_level_ms ); Lib_input_video_control( BIN_PLANE ); Lib_display_control( BIN_PLANE | LINE_PLANE | CHAR_PLANE ); Lib_memory_clear( BIN_PLANE ); } else if ( Timing == END_TIMING ) { Lib_input_video_control( GRAY_PLANE | BIN_PLANE ); Lib_display_control( GRAY_PLANE | LINE_PLANE | CHAR_PLANE ); Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); Lib_draw_menu( pad_level_ms ); } Lib_video_bin_level( (int)val[0].long_type ); Lib_freerun(); } /*********************************************************************/ /*  ウインド設定制御                        */ /*********************************************************************/ void pdm_wind_set( Timing, Numb, val ) int Timing; int Numb; PARADIGM val[]; { int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); wind_set( &b_wxs, &b_wys, &b_wxe, &b_wye ); Lib_recover_plane( idc ); } /************************************************************************/ /* サーチ実行 */ /************************************************************************/ int search_exec( void ) { int Rslt_Buf[3]; int rtn; Lib_gs_window( NON_DISPLAY, gs_wxs, gs_wys, gs_wxe, gs_wye ); if ( 0 < ( rtn = Lib_gs_search( ON_DISPLAY, name, 1, seido, complex, mid_lower, last_lower, Rslt_Buf ) ) ) { zure_x = ( (double)Rslt_Buf[0] / 10.0 ) - Mas_x; zure_y = ( (double)Rslt_Buf[1] / 10.0 ) - Mas_y; } return( rtn ); } /***********************************************************************/ /* 入力数値(double)の4捨5入した数値(int)を返す */ /***********************************************************************/ int d4i5_d( in ) double in; /* 入力:浮動小数点数値 */ { int out; if ( in >= 0.0 ) out = (int)( in + 0.5 ); else out = (int)( in - 0.5 ); return( out ); } /************************************************************************/ /* 実行 */ /************************************************************************/ void exec( void ) { int ofsx, ofsy; int rtn; int xs,ys,xe,ye; int end_x,end_y; Lib_strtclk_count(); /*タイマーカウントモードのスタート*/ end_x = Lib_get_fx_size() - 1; end_y = Lib_get_fy_size() - 1; Lib_video_bin_level( bin_level ); Lib_freeze( TRANSMIT ); /* アライメント実行 */ if ( 0 < ( rtn = search_exec() ) ) { ofsx = d4i5_d( zure_x ); ofsy = d4i5_d( zure_y ); /* アライメント後の処理範囲の始点終点 */ xs = b_wxs + ofsx; ys = b_wys + ofsy; xe = b_wxe + ofsx; ye = b_wye + ofsy; /* 処理範囲が画像サイズをはみ出していないかチェック */ if ( xs < 0 ) xs = 0; if ( ys < 0 ) ys = 0; if ( end_x < xe ) xe = end_x; if ( end_y < ye ) ye = end_y; /* アライメントの結果から2値計測の処理範囲を補正 */ Lib_set_stage_window( xs, ys, xe, ye ); Lib_box( xs, ys, xe, ye, DASHED_LINE ); /*2値計測*/ meas_exec(); Lib_time_disp( 0, 452 ); Lib_display_keyinput( 460, 0, "確認" ); Lib_window( 0, 0, 511, 479 ); } else if ( 0 == rtn ) Lib_display_message( 100, 200, "エラー", "対象物がありません" ); else if ( -1 == rtn ) Lib_display_message( 100, 200, "エラー", "パタンが登録されていません" ); Lib_freerun(); Lib_cls( ( LINE_PLANE | CHAR_PLANE ), BLACK_COLOR ); } /*********************************************************************/ /* 2値計測実行                 */ /*********************************************************************/ void meas_exec( void ) { static struct MEASDATA *measp; static int *indextbl; unsigned int max_blob; char *error_string; int error_code; int i; int ct; int blobno; int center_x; int center_y; int area; int sumi,sumj; /* システムで設定されている「最大ブローブ数」の値を取得し、indextblに必要な容量の */ /* ワークメモリを確保する */ max_blob = Lib_get_meas_max_blob(); if ( ( int * )NULL != ( indextbl = ( int * )Lib_mlalloc( ( max_blob + 1 ) * 4 ) ) ) { /* システム構築対応1次特徴量計測処理オープン */ Lib_Open_meas_structure(); if ( NORMAL_RETURN == Lib_xmeasure( color, meas_edge, l_limit, &measp ) ) /* メジャー */ { /* システム構築対応1次特徴量アドレステーブル作成 */ Lib_Make_meas_structure ( (void *)measp ); if ( 0 < ( ct = Lib_xorderng( measp, 0, 0, color, u_limit, l_limit, indextbl ) ) ) { for ( i = 0; i < ct; i++ ) { blobno = indextbl[i]; /* 重心の計算 */ area = *( Meas_area + blobno ); sumi = *( Meas_sumi + blobno ); sumj = *( Meas_sumj + blobno ); center_x = sumi / area; /* 重心x */ center_y = sumj / area; /* 重心y */ Lib_drawline( center_x-2, center_y-2, center_x+2, center_y+2 ); Lib_drawline( center_x-2, center_y+2, center_x+2, center_y-2 ); } Lib_sprintf( ss, "個数:%d", ct ); Lib_chrdisp( 50, 30, ss ); } else Lib_chrdisp( 1, 30, "エラー:ブローブがありません。" ); } else { error_code = Lib_get_meas_error_code(); error_string = Lib_get_meas_error_string(); Lib_sprintf( ss, "2値計測エラー [%d] : %s", error_code, error_string ); Lib_chrdisp( 1, 30, ss ); } } else Lib_chrdisp( 1, 30, "エラー:indextbl用のワークメモリを確保できません。" ); }