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;
}
« ocr思考及测试
|
不得不吐下炉石传说,更新太慢了。»
评论:


2014-07-29 23:54