/* * ***************************************************************** * * * * * 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値計測(パッドを自作する例) MEASBSC.C */ /*[作成者]H.Yagi */ /* 目的: 関数: 履歴: Ver 1.0 97/08/27 Ver 2.0 99/05/12 試行時に設定したパラメータが有効にならない不具合を修正 注記: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_file.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 save_file( void ); int load_file( void ); void meas_set( void ); void pdm_bin_level( int, int, PARADIGM * ); void pdm_wind_set( int, int, PARADIGM * ); void wind_set( int *, int *, int *, int * ); 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 ", " 2値設定 " }, { " EXEC ", " 実 行 " }, { " SAVE ", " セーブ " }, { " PROC4 ", " 処理4 " } }; #define INIT_CUR_POS_X 255 #define INIT_CUR_POS_Y 239 static char *select_color[2] = { "黒", "白" }; int pad_level_ms; static char ss[70]; int sts; int time; int no; int bin_level; /*2値レベル */ int wxs, wys, wxe, wye; /*計測範囲 */ int color; /*対象色 */ int meas_edge; /*接触塊オプション */ int l_limit; /*面積下限値 */ int u_limit; /*面積上限値 */ typedef struct { int bin_level; /*2値レベル */ int wxs; /*計測範囲 */ int wys; int wxe; int wye; int color; /*対象色 */ int meas_edge; /*接触塊オプション */ int l_limit; /*面積下限値 */ int u_limit; /*面積上限値 */ } DATA; /* * メイン */ 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; if ( ERROR_RETURN == load_file() ) { bin_level = 128; wxs = 0; wys = 0; wxe = 511; wye = 479; color = BLACK_COLOR; meas_edge = ON; l_limit = 10; u_limit = 245760; } /* 入力ビデオ制御 */ Lib_input_video_control( GRAY_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 ); /* メニュー制御 */ 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 ); meas_set(); /*2値計測設定*/ 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(); /*実行*/ Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); main_menu_disp(); } else if ( xpos > MENU_3_XS && xpos < MENU_3_XE && ypos > MENU_3_YS && ypos < MENU_3_YE ) { save_file(); /*セーブ*/ 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; /* 終了 */ } } } } /* * メインメニュー表示 */ 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 *)str_main_menu[2][iLanguage], (char *)NULL ); } /* * ヘルプ表示 */ static void disp_help( void ) { Lib_chrdisp( 10, 8, "【機能\概要】 MEASBSC" ); Lib_chrdisp( 11, 10, "2値計測を行い各物体の重心、面積と物体の個数、" ); Lib_chrdisp( 11, 12, "フェレ径を表\示します。" ); Lib_chrdisp( 11, 14, "計測のパラメータのパッドを自作する例です。" ); Lib_chrdisp( 11, 16, "2値計測はシステム構\築対応です。" ); } /***** 設定データのセーブ *****/ void save_file( void ) { DATA *ap; Lib_cls( ( LINE_PLANE | CHAR_PLANE ), BLACK_COLOR ); Lib_set_cursor_shape( HOURGLASS_TYPE ); if ( ( DATA * )NULL != ( ap = ( DATA * )Lib_mlalloc( sizeof( DATA ) ) ) ) { ap->bin_level = bin_level; /*2値レベル */ ap->wxs = wxs; /*計測範囲 */ ap->wys = wys; ap->wxe = wxe; ap->wye = wye; ap->color = color; /*対象色 */ ap->meas_edge = meas_edge; /*接触塊オプション */ ap->l_limit = l_limit; /*面積下限値 */ ap->u_limit = u_limit; /*面積上限値 */ /* 設定ファイルのセーブ */ Lib_display_comment( 144, 228, "設定ファイルをセーブ中です" ); if ( ( unsigned long )NULL == Lib_fsave( "\\FS0\\MEASBSC.DAT", ( char * )ap, sizeof( DATA ) ) ) Lib_display_message( 120, 200, "エラー", "設定ファイルをセーブできません" ); Lib_lfree( ( char * )ap ); } else Lib_display_message( 120, 200, "エラー", "メモリーが確保できません" ); Lib_cls( ( LINE_PLANE | CHAR_PLANE ), BLACK_COLOR ); Lib_set_cursor_shape( ARROW_TYPE ); } /***** 設定ファイルのロード *****/ int load_file( void ) { DATA *ap; int error_status; if ( ( DATA * )NULL != ( ap = ( DATA * )Lib_mlalloc( sizeof( DATA ) ) ) ) { /* 設定ファイルのロード */ if ( ( unsigned long )NULL != Lib_fload( "\\FS0\\MEASBSC.DAT", ( char * )ap, sizeof( DATA ) ) ) { bin_level = ap->bin_level; /*2値レベル */ wxs = ap->wxs; /*計測範囲 */ wys = ap->wys; wxe = ap->wxe; wye = ap->wye; color = ap->color; /*対象色 */ meas_edge = ap->meas_edge; /*接触塊オプション */ l_limit = ap->l_limit; /*面積下限値 */ u_limit = ap->u_limit; /*面積上限値 */ error_status = NORMAL_RETURN; } else error_status = ERROR_RETURN; Lib_lfree( ( char * )ap ); } else { Lib_display_message( 120, 200, "エラー", "メモリーが確保できません" ); error_status = ERROR_RETURN; } return( error_status ); } /************************************************************************/ /* 設定 */ /************************************************************************/ void meas_set( ) { PVAL value_meas[6]; int no; pad_level_ms = Lib_view_open(); Lib_view_set_title( pad_level_ms, "設定" ); 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 ); } /*********************************************************************/ /*  2値レベル設定                         */ /*********************************************************************/ void pdm_bin_level( Timing, Numb, val ) int Timing; int Numb; PARADIGM val[]; { if ( START_TIMING == 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 ( END_TIMING == Timing ) { Lib_input_video_control( GRAY_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 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 = wxe - wxs + 1; y_size = wye - wys + 1; Lib_view_set_box( pad_level_wind, 5, 5, "始点" , wxs , wys , 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: /*「実行」が選択された */ wxs = value_wind[0].box_type.x; wys = value_wind[0].box_type.y; wxe = value_wind[0].box_type.x + value_wind[1].box_type.x - 1; wye = 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 exec( void ) { static struct MEASDATA *measp; static int *indextbl; unsigned int max_blob; char *error_string; int error_code; int i; int fxs, fys, fxe, fye; int ct; int blobno; int center_x; int center_y; int area; int sumi,sumj; Lib_input_video_control( BIN_PLANE ); Lib_display_control( BIN_PLANE | LINE_PLANE | CHAR_PLANE ); /* システムで設定されている「最大ブローブ数」の値を取得し、indextblに必要な容量の */ /* ワークメモリを確保する */ max_blob = Lib_get_meas_max_blob(); if ( ( int * )NULL != ( indextbl = ( int * )Lib_mlalloc( ( max_blob + 1 ) * 4 ) ) ) { Lib_video_bin_level( bin_level ); Lib_strtclk_count(); /*タイマーカウントモードのスタート*/ Lib_freeze( TRANSMIT ); Lib_window( wxs, wys, wxe, wye ); /* システム構築対応1次特徴量計測処理オープン */ Lib_Open_meas_structure(); if ( NORMAL_RETURN == Lib_xmeasure( color, meas_edge, l_limit, &measp ) ) /* メジャー */ { if ( 0 < ( ct = Lib_xorderng( measp, 0, 0, color, u_limit, l_limit, indextbl ) ) ) { /* システム構築対応1次特徴量アドレステーブル作成 */ Lib_Make_meas_structure ( (void *)measp ); 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 */ /* フェレ経 */ fxs = *( Meas_imin + blobno ); fxe = *( Meas_imax + blobno ); fys = *( Meas_jmin + blobno ); fye = *( Meas_jmax + blobno ); Lib_box( fxs, fys, fxe, fye, SOLID_LINE ); 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", area ); Lib_kanjishift( 0, 0, 0, center_x+5, center_y-5, ss ); } Lib_sprintf( ss, "個数:%d", ct ); Lib_chrdisp( 50, 30, ss ); Lib_time_disp( 0, 452 ); } 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 ); } Lib_lfree( ( char * )indextbl ); } else Lib_chrdisp( 1, 30, "エラー:indextbl用のワークメモリを確保できません。" ); Lib_display_keyinput( 430, 0, " 確認 " ); Lib_input_video_control( GRAY_PLANE ); Lib_cls( ( BIN_PLANE | LINE_PLANE | CHAR_PLANE ), BLACK_COLOR ); Lib_display_control( GRAY_PLANE | LINE_PLANE | CHAR_PLANE ); Lib_window( 0, 0, 511, 479 ); Lib_freerun(); }