IBM Books

Image Extender、Audio Extender 和 Video Extender 管理和程序设计

QBIC 查询样本程序

下图 显示了用 C 语言编写的、用于构建并运行 QBIC 查询的程序的一部分。图中的代码按平均颜色查询图象。它提示用户输入颜色或图象文件的名称。用户还可以将查询返回的图象用作后续查询的示例图象。该程序将命名颜色或图象的颜色用作平均颜色来查询图象列。

可在 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 查询的 搜索标准。例如,该程序提供了一个颜色选择器以选择平均颜色。该程序将选择转换为查询字符串。

图 21. 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);
}

 


[ 页的顶部 | 上一页 | 下一页 | 目录 | 索引 ]