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;
    }

评论:

邓柑名兔
2014-07-29 23:54
还没崇拜过谁呢,满足一下愿望吧,谢谢!
卢斤店后
2014-07-28 19:25
晕。。。。怎么没早看到呢。。。。。。。。。
向平勾柱
2014-07-28 19:19
震撼!
王宜心侄
2014-06-26 19:43
我顶啊。接着顶
范去卉言
2014-06-18 11:14
不错,支持下
陈昭步拉
2014-06-14 23:31
呵呵 我可不敢~~~~
秦侑岩昀
2014-06-14 12:57
哪个正常的人能崇拜一只蟑螂呢??
宋吾久邑
2014-05-12 15:31
吖...
雷丞底天
2014-05-07 04:06
终于看完了~~~
陆幻互努
2014-05-04 04:20
哈哈,顶你了哦.
罗帅胃奈
2014-05-01 18:04
人生伟业的建立 ,不在能知,乃在能行。?
沈冷昨冷
2014-05-01 17:56
很好!很强大!
汪栅凸政
2014-04-28 11:44
风物长宜放眼量
何明肖玟
2014-04-27 21:49
呵呵 大家好奇嘛 来观看下~~~~?
莫欣些丰
2014-04-26 03:34
真是练的
Powered by emlog 蜀ICP备14001363号