下圖顯示在建立 QBIC 查詢中,以 C 撰寫的部份程式碼。 圖中的程式碼可按平均色來查詢影像。 它提示使用者輸入顏色或影像的名稱。 使用者也可以使用查詢傳回的影像,作為後續查詢的範 例影像。 然後,程式使用已命名的顏色或影像顏色, 作為查詢影像直欄的平均色。
您可以在 SAMPLES 次目錄中的 QBICDEMO.C 檔中找到完整程式。 完整程式可用來透過直方圖色或位置顏色,以及平均色來查詢影像。 若要執行完整程式,您必須執行 ENABLE、POPULATE 和 QBCATDMO 範例程式 (也位於 SAMPLES 次目錄中)。 有關範例程式的詳細資訊, 請參閱附錄 B, 範例程式和媒體檔案。
請注意中的下列要點:
(1)併入 dmbqbapi 標頭檔。
(2)提示使用者資料庫資訊。
(3)連接資料庫。
(4)建立查詢物件。
(5)將特性新增到查詢物件。
(6)提示使用者鍵入輸入類型 (顏色名稱、影像或先前擷取的影像)。
(7)指定特性的資料來源。 資料來源指平均色的明確規格。
(8)發出查詢。Image Extender 會搜尋整個影像直欄。 它也指定 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); } |