OpenCv 大图中找小图

作者:shj 发布于:2014-1-22 17:03

为了实现大图中找小图,之前是考虑使用OpenCv中的模板匹配。。。但发现速度太慢了。

前几天实在受不了,自己动手写了个特定情况下使用的。

把代码贴上来有兴趣的看了。


    clock_t starttime = clock();
    clock_t endtime = 0;
    int found = 0;
     
    IplImage *source1 = cvLoadImage("G:\\vctest\\OpenCv_Test\\图片\\030.png");
    IplImage *source2 = cvLoadImage("G:\\vctest\\OpenCv_Test\\图片\\0311.png");
    cvShowImage("source1",source1);
    cvShowImage("source2",source2);
     
    //FILE *fp = fopen("G:\\vctest\\OpenCv_Test\\图片\\030.tes","wb");
    //FILE *fp1 = fopen("G:\\vctest\\OpenCv_Test\\图片\\031.tes","wb");
    //fwrite(source1->imageData,source1->imageSize,1,fp);
    //fwrite(source2->imageData,source2->imageSize,1,fp1);
    //fclose(fp);
    //fclose(fp1);
     
     
     
    char *source1_data = source1->imageData;
    char *source2_data = source2->imageData;
     
    for(int h=0;h<source1->height - source2->height-1;h++)
    {
    for (int w=0;w<source1->width-source2->width-1;w++)
    {
    if (1 == contrast((unsigned char *)&source1_data[h*source1->widthStep+w*source1->nChannels],(unsigned char *)&source2_data[source2->width*source2->nChannels],source2->width*source2->nChannels,1))
    {
    found = 1;
    for (int ih = 1;ih < source2->height-1;ih++)
    {
    if (0 == contrast((unsigned char *)&source1_data[(h+ih)*source1->widthStep+w*source1->nChannels],(unsigned char *)&source2_data[ih*source2->widthStep+source2->width*source2->nChannels],source2->width*source2->nChannels,1))
    {
    found = 0;
    continue;
    }
    }
    if (found == 0)
    {
    break;
    }
    AfxOutputDebugString("find 1 line at:%d %d\n",w,h);
    endtime = clock();
    AfxOutputDebugString("time:%d\n",endtime-starttime);
    break;
    }
     
     
    }
    // AfxOutputDebugString("h::%d\n",h);
    }
     
     
     
    cvReleaseImage(&source1);
    cvReleaseImage(&source2);
    endtime = clock();
    AfxOutputDebugString("time:%d\n",endtime-starttime);

    int Cimage_contrast_01Dlg::contrast(unsigned char *source1,unsigned char *source2,int len,int a)
    {
    int i = 0;
    for (i=0;i<len-1;i++)
    {
    //if(source1[i] <= source2[i] - a || source1[i] >= source2[i] + a)
    if((abs(source1[i] - source2[i]) >= a))
    {
    return 0;
    }
    }
     
    return 1;
    }

评论(23) 浏览(34598)

不得不吐下炉石传说,更新太慢了。

作者:shj 发布于:2014-1-22 17:01

炉石传说在线更新太慢了。。。。

解决方法如下:



打开【IE浏览器】-【Internet属性】-【连接】

确保未勾选 【为LAN使用代理服务器】
若【自动检测设置】已勾选则取消!!!!!!!!!
若【自动检测设置】未勾选则勾选!!!!!!!!!


取消以后不用关闭更新程序,下载速度自动提升了。

评论(20) 浏览(26637)

记念开始,今天一定要写什么东东

作者:shj 发布于:2014-1-22 16:56

随便写个。。。eve market.先随便上张截图。完整程序下载地址market.zip









主要用了几个控件,截图上看看就明白了。

在程序中用了sqllite的数据库,用于所有物品的基础信息。

下面贴点代码。

    #pragma comment(lib,"sqlite3.lib") //连接了sqlite3的数据库。
     
    open_sql("new_eve.db");//初始化时打开了new_eve.db这个数据库。
    //打开和关闭数据库
    int Cmarket_V01App::open_sql(char *dbname)
    {
    int rc = 0;
    rc = sqlite3_open(dbname,&db);
    if (rc != SQLITE_OK)
    {
    CString debug_out = sqlite3_errmsg(db);
     
    sqlite3_close(db);
     
    return -1;
    }
     
    return 0;
    }
     
    void Cmarket_V01App::close_sql()
    {
    if (db != NULL)
    {
    sqlite3_close(db);
    }
    }
初始化时build了整个树结构代码如下:



    void Cmarket_V01App::build_tree(void *p)
    {
    Cmarket_V01Dlg *p_wnd = (Cmarket_V01Dlg *)p;
     
    char * errmsg = NULL;
    char **dbResult[5]; //是 char ** 类型,两个*号
    int nRow[5], nColumn[5];
    char sql[5][1024];
    int result,i,j,x,y,z;
    CString out;
     
     
    p_wnd->m_tree.GetCapture();
    HTREEITEM hTreeItemParent=NULL;
    HTREEITEM hTreeItemRoot=NULL;
    HTREEITEM hTreeChild[5] = {NULL};
     
    if (db == NULL)
    {
    return ;
    }
    hTreeItemRoot = p_wnd->m_tree.GetRootItem();
    hTreeItemParent=p_wnd->m_tree.InsertItem("吉他行情");
     
    result = sqlite3_get_table( db, "select distinct classify1 from eve_data", &dbResult[0], &nRow[0], &nColumn[0], &errmsg );
    if (result != SQLITE_OK)
    {
    return ;
    }
     
    out.Format("查到%d条记录\n", nRow[0] );
    OutputDebugString(out);
     
    //for( i = 0; i < nRow[0] ; i++ )
    //{
    // out.Format(" “第 %d 条记录\n", i );
    // OutputDebugString(out);
    // out.Format( "字段名:%s 字段值:%s\n", dbResult[0][0], dbResult[0][i+1] );
    // OutputDebugString(out);
    // out.Format( "-------\n" );
    // OutputDebugString(out);
    //}
    SetWindowPos(AfxGetMainWnd()->m_hWnd, HWND_TOPMOST, -1, -1, -1, -1, SWP_NOMOVE | SWP_NOSIZE);
     
     
    for(i = 1;i < nRow[0];i++)
    {
    if (dbResult[0][i] == NULL)
    {
    continue;
    }
    out.Format("%s",dbResult[0][i]);
    hTreeChild[0] = p_wnd->m_tree.InsertItem(out,hTreeItemParent);
    sprintf(sql[0],"select distinct classify2 from eve_data where classify1 = '%s'",dbResult[0][i]);
    result = sqlite3_get_table( db, sql[0], &dbResult[1], &nRow[1], &nColumn[1], &errmsg );
    for( j = 1; j < nRow[1]+1 ; j++ )
    {
    if (dbResult[1][j] == NULL)
    {
    continue;
    }
    out.Format("查到%d条记录\n", nRow[1] );
    OutputDebugString(out);
    //out.Format(" “第 %d 条记录\n", j );
    //OutputDebugString(out);
    //out.Format( "字段名:%s 字段值:%s\n", dbResult[1][0], dbResult[1][j] );
    //OutputDebugString(out);
    //out.Format( "-------\n" );
    //OutputDebugString(out);
    out.Format("%s",dbResult[1][j]);
    hTreeChild[1] = p_wnd->m_tree.InsertItem(out,hTreeChild[0]);
    }
    sqlite3_free_table(dbResult[1]);
     
    }
    sqlite3_free_table(dbResult[0]);
     
     
    return ;
    }



因为数据库里面条数太多,整个EVE的数据库有5W多条。我们用的又是轻量级的数据库,在生成时查询所有数据并显示会导致程序启动需要初始化的时间过长。 这里我们启动时只加载前几个大项的数据,并没有加载所有数据。当选择具体的条目时在根据条目来进行添加。选择时的代码如下:

    void Cmarket_V01Dlg::OnNMClickTree1(NMHDR *pNMHDR, LRESULT *pResult)
    {
    // TODO: 在此添加控件通知处理程序代码
    CPoint pt;
    int i = 0;
     
    GetCursorPos(&pt);
     
    m_tree.ScreenToClient(&pt);
     
    CString classify_text[8]= {""};
    int classify_count = 0;
     
    UINT nFlag = 0;
    HTREEITEM hFather[8] = {NULL};
     
    HTREEITEM hChild = NULL;
     
    HTREEITEM hItem = m_tree.HitTest(pt, &nFlag);
     
    if( NULL != hItem )
     
    {
     
    m_tree.SelectItem(hItem);
     
    // CString strTemp=m_tree.GetItemText(hItem);
     
    DWORD nID=m_tree.GetItemData(hItem);
    if (nID != 0)
    {
    theApp.show_item((void *)this,nID);
     
    UpdateData(FALSE);
    *pResult = 0;
    return ;
     
    }
    hFather[6] = m_tree.GetParentItem(hItem);
    classify_text[0] = m_tree.GetItemText(hItem);
     
    // classify_text.Format("%d,%s",nID,strTemp);
    if (hFather[6] != NULL)
    {
    for (i = 5;i >= 0;i--)
    {
    hFather[i] = m_tree.GetParentItem(hFather[i+1]);
    if (hFather[i] == NULL)
    {
    break;
    }
     
    }
    classify_count = i;
    for (i;i <= 6 ;i++)
    {
    classify_text[6 - i + 1] = m_tree.GetItemText(hFather[i]);
    }
    }
     
    for (classify_count = 6 ;hFather[classify_count] != NULL;classify_count--)
    {
    ;
    }
    classify_count = 6 - classify_count;
     
    if (!m_tree.ItemHasChildren(hItem) )
    {
    printf("get");
    theApp.add_item((void *)this,classify_text,classify_count,hItem);
     
    }
     
     
    UpdateData(FALSE);
     
    }
    *pResult = 0;
    }

先写这些,数据库查询这些东东有需要的朋友留言我在贴吧。


评论(39) 浏览(32197)

开篇

作者:shj 发布于:2014-1-22 16:54

今天买了个阿里云服务器,花一下午的时间做域名的绑定。。。。结果晚上打电话才知道在国内的服务器没有备案之前是不能通过域名访问的。。。。。

评论(18) 浏览(23449)

Powered by emlog 蜀ICP备14001363号