次の図 は、QBIC 照会を作成して実行する C プログラムの一部を示したものです。 この図のコードでは、画像を平均色によって照会します。 ユーザーは、色またはイメージ・ファイルの名前の入力を求められます。 ユーザーは、照会によって戻される画像をサンプル・イメージとして使って、それ以後の照会を行うこともできます。 プログラムは、その指定された色か、その画像の色を平均色として使って、列の画像を照会します。
このプログラムの全体は、SAMPLES サブディレクトリーの QBICDEMO.C ファイルにあります。 このプログラムでは、ヒストグラム色か位置色で画像を照会することもできますし、平均色で照会することもできます。 プログラム全体を実行するには、ENABLE、POPULATE、QBCATDMO の各サンプル・プログラム (SAMPLES サブディレクトリーにあります) を実行する必要があります。 サンプル・プログラムの詳細については、付録 B, サンプル・プログラムとメディア・ファイルを参照してください。
サンプル・プログラム の次の点に注目してください。
(1) dmbqbapi ヘッダー・ファイルをインクルードする。
(2) データベース情報をユーザーに要求する。
(3) データベースに接続する。
(4) 照会オブジェクトを作成する。
(5) 照会オブジェクトにフィーチャーを追加する。
(6) 入力のタイプをユーザーに要求する (色名、イメージ・ファイル、または前に取り出した画像)。
(7) フィーチャーのデータ・ソースを指定する。 データ・ソースは、平均色の明示的な指定です。
(8) 照会を行う。 イメージ・エクステンダーは、その列全体の画像を探索します。 さらに、戻す画像の最大数として 10 を指定しています。
(9) 戻される画像群の中の次の画像を表示する。 画像の表示については、さらに フルサイズの画像やビデオ・フレームの表示を参照してください。
(10) 照会オブジェクトを削除する。
SAMPLES サブディレクトリーには、QBIC 照会を作成し使用する方法を示す別のプログラムが含まれています。 プログラム QbicQry.java は、QBIC 照会の探索基準を図によって指定する方法を示しています。 たとえば、このプログラムは平均色を選択するためのカラー・セレクターを示しています。 プログラムはこの選択を照会ストリングに変換します。
#include <sql.h> #include <sqlcli.h> #include <sqlcli1.h> #include <dmbqbqpi.h> (1) #include <stdio.h> #include <string.h> #include <malloc.h> #include <color.h> #include <ctype.h> #define MaxQueryReturns 10 #define MaxDatabaseNameLength SQL_SH_IDENT #define MaxUserIdLength SQL_SH_IDENT #define MaxPasswordLength SQL_SH_IDENT #define MaxTableNameLength SQL_LG_IDENT #define MaxColumnNameLength SQL_LG_IDENT static char databaseName[MaxDatabaseNameLength+1]; static char userid[MaxUserIdLength+1]; static char password[MaxPasswordLength+1]; static char tableName[MaxTableNameLength+1]; static char columnName[MaxColumnNameLength+1]; static char line[4000]; static QbResult results[MaxQueryReturns]; static long currentImage = -1; static long imageCount = 0; static char* tableAndColumn; static QbQueryHandle averageHandle = 0; static QbQueryHandle histogramHandle = 0; static QbQueryHandle drawHandle = 0; static QbQueryHandle lastHandle = 0; static SQLHENV henv; static SQLHDBC hdbc; static SQLHSTMT hstmt; static SQLRETURN rc; static char* listQueries = "SELECT NAME,DESCRIPTION FROM MMDBSYS.QBICQUERIES ORDER BY NAME"; static char* menu[] = { /* 12345678901234567890123456789012345678901234567890123456789012345678901234567890 */ "", "+-----------------------------------------------------------------------------+", "| AVERAGE COLOR colorname |", "| AVERAGE FILE filename format |", "| AVERAGE LAST |", "| Press Enter to display the next image in the series |", "+-----------------------------------------------------------------------------+", "", 0 }; static char* help[] = { "", "AVERAGE Execute an average color query", " COLOR Specifies the color to query for", " FILE Specifies the file to compute the average color from", " LAST Specifies the last displayed image be used to compute the color", " NEXT Displays the next image from the current query or nothing if", " all of the image have been displayed." "", ">>pause<<", 0 }; /******************************************************************************/ /* doNext() */ /******************************************************************************/ static void doNext(void) { int ret; if (currentImage < imageCount) currentImage++; if (currentImage < imageCount) ret = DBiBrowse("/usr/local/bin/xv %s", MMDB_PLAY_HANDLE, results[currentImage].imageHandle, MMDB_PLAY_NO_WAIT); (9) } /******************************************************************************/ /* doAverage() */ /******************************************************************************/ static void doAverage(void) { QbQueryHandle qohandle = 0; QbImageSource is; char* type; char* arg1; char* arg2; type = nextWord(0); if (abbrev(type, "color", 1)) { is.type = qbiSource_AverageColor; arg1 = nextWord(0); if (arg1 == 0) { printf("AVERAGE COLOR command requires a colorname argument.\n"); return; } if (getColor(arg1, &is.averageColor) == 0) { printf("The colorname entered was not recognized.\n"); return; } } else if (abbrev(type, "file", 1)) { is.type = qbiSource_ClientFile; arg1 = nextWord(0); if (arg1 == 0) { printf("AVERAGE FILE command requires a filename argument.\n"); return; } arg2 = nextWord(0); if (arg2 == 0) { printf("AVERAGE FILE command requires a file format argument.\n"); return; } strcpy(is.clientFile.fileName, arg1); strcpy(is.clientFile.format, arg2); } else if (abbrev(type, "last", 1)) { is.type = qbiSource_ImageHandle; if (0 <= currentImage &&; currentImage < imageCount) strcpy(is.imageHandle, results[currentImage]imageHandle); else { printf("No last image for AVERAGE LAST command\n"); return; } } else { printf("AVERAGE command only supports COLOR, FILE, and LAST types.\n"); return; } _QbQuerySetFeatureData(averageHandle, "QbColorFeatureClass", &is); (7) _QbQuerySearch(averageHandle, tableAndColumn, "IMAGE", MaxQueryReturns, 0, 0, &imageCount, results); (8) lastHandle = averageHandle; currentImage = -1; } /******************************************************************************/ /* commandLoop() */ /******************************************************************************/ void commandLoop(void) { int done = 0; while (!done) { (6) displayText(menu); printf("%d", currentImage + 1); if (0 <= currentImage &&; currentImage < imageCount) printf(" %8.6f", results[currentImage].score); printf("> "); gets(line); done = processCommand(line); } } /******************************************************************************/ /* main() */ /******************************************************************************/ void main(void) { char* inst; int i; printf("\n\n"); printf("Please enter: database_name [user_id] [password] "\n"); (2) gets(line); if (copyWord(line, databaseName, sizeof(databaseName)) == 0) exit(0); copyWord(0, userid, sizeof(userid)); copyWord(0, password, sizeof(password)); printf("\n"); if (SQLAllocEnv(&henv) != SQL_SUCCESS) sqlError(SQL_NULL_HSTMT); if (SQLAllocConnect(henv, &hdbc) != SQL_SUCCESS) sqlError(SQL_NULL_HSTMT); if (SQLConnect(hdbc, (3) (SQLCHAR*)databaseName, SQL_NTS, (SQLCHAR*)userid, SQL_NTS, (SQLCHAR*)password, SQL_NTS) != SQL_SUCCESS) sqlError(SQL_NULL_HSTMT); printf("Initializing . . .\n"); inst = getenv("DB2INSTANCE"); if (inst != 0 &&; strcmp(inst, "keeseyt") == 0) tableAndColumn = "KEESEY.TEST"; else tableAndColumn = "QBICDEMO.TEST"; _QbQueryCreate(&averageHandle); (4) _QbQueryAddFeature(averageHandle, "QbColorFeatureClass"); _QbQueryCreate(&histogramHandle); _QbQueryAddFeature(histogramHandle, "QbColorHistogramFeatureClass"); _QbQueryCreate(&drawHandle); _QbQueryAddFeature(drawHandle, "QbDrawFeatureClass"); (5) commandLoop(); _QbQueryDelete(drawHandle); _QbQueryDelete(histogramHandle); (10) _QbQueryDelete(averageHandle); if (SQLDisconnect(hdbc) != SQL_SUCCESS) sqlError(SQL_NULL_HSTMT); if (SQLFreeConnect(hdbc) != SQL_SUCCESS) sqlError(SQL_NULL_HSTMT); if (SQLFreeEnv(henv) != SQL_SUCCESS) sqlError(SQL_NULL_HSTMT); } |