如何从图像的直方图中过滤出感兴趣的对象?
Posted
技术标签:
【中文标题】如何从图像的直方图中过滤出感兴趣的对象?【英文标题】:how to filter objects of interest from a histogram of an image? 【发布时间】:2012-05-05 08:38:32 【问题描述】:场景是这样的,我有一个图像,我只想从中提取文本以进行进一步的 OCR 处理,我试图通过腐蚀和膨胀来删除徽标,但是当卡片的背景中有图像时它会失败或卡片分为 2 种不同的颜色,所以我想计算卡片的直方图,然后过滤文本,因为它将在背景或任何其他非文本区域中具有最小的峰值我得到这个 opencv 代码来计算直方图图片
OpenCV 代码:
IplImage* trueColorImage = cvLoadImage("plastics.jpg");
TrueColorIplImg=[self CreateIplImageFromUIImage:trueColorImage];
IplImage* channel = cvCreateImage( cvGetSize(TrueColorIplImg), 8, 1);
IplImage *hist_img = cvCreateImage(cvSize(300,240), 8, 1);
cvSet( hist_img, cvScalarAll(255), 0 );
CvHistogram *hist_red;
CvHistogram *hist_green;
CvHistogram *hist_blue;
int hist_size = 256;
float range[]=0,256;
float* ranges[] = range ;
float max_value = 0.0;
float max = 0.0;
float w_scale = 0.0;
hist_red = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
hist_green = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
hist_blue = cvCreateHist(1, &hist_size, CV_HIST_ARRAY, ranges, 1);
cvSetImageCOI(TrueColorIplImg,3);
cvCopy(TrueColorIplImg,channel);
cvResetImageROI(TrueColorIplImg);
cvCalcHist( &channel, hist_red, 0, NULL );
cvSetImageCOI(TrueColorIplImg,2);
cvCopy(TrueColorIplImg,channel);
cvResetImageROI(TrueColorIplImg);
cvCalcHist( &channel, hist_green, 0, NULL );
cvSetImageCOI(TrueColorIplImg,1);
cvCopy(TrueColorIplImg,channel);
cvResetImageROI(TrueColorIplImg);
cvCalcHist( &channel, hist_blue, 0, NULL );
cvGetMinMaxHistValue( hist_red, 0, &max_value, 0, 0 );
cvGetMinMaxHistValue( hist_green, 0, &max, 0, 0 );
max_value = (max > max_value) ? max : max_value;
cvGetMinMaxHistValue( hist_blue, 0, &max, 0, 0 );
max_value = (max > max_value) ? max : max_value;
cvScale( hist_red->bins, hist_red->bins, ((float)hist_img->height)/max_value, 0 );
cvScale( hist_green->bins, hist_green->bins, ((float)hist_img->height)/max_value, 0 );
cvScale( hist_blue->bins, hist_blue->bins, ((float)hist_img->height)/max_value, 0 );
printf("Scale: %4.2f pixels per 100 units\n", max_value*100/((float)hist_img->height));
w_scale = ((float)hist_img->width)/hist_size;
for( int i = 0; i < hist_size; i++ )
cvRectangle( hist_img, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_red->bins,i))),
CV_RGB(255,0,0), -1, 8, 0 );
cvRectangle( hist_img, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_green->bins,i))),
CV_RGB(0,255,0), -1, 8, 0 );
cvRectangle( hist_img, cvPoint((int)i*w_scale , hist_img->height),
cvPoint((int)(i+1)*w_scale, hist_img->height - cvRound(cvGetReal1D(hist_blue->bins,i))),
CV_RGB(0,0,255), -1, 8, 0 );
【问题讨论】:
【参考方案1】:生成直方图很简单,OpenCV 的文档中提供了代码。所以你并没有真正给我们任何有用的东西。剩下的任务才是真正的挑战所在。我很想看到你尝试解决这个问题。
我从您的其他问题中注意到您对这个问题非常感兴趣。我需要说,解决它可能比你以前想象的要复杂一些。您可能希望稍微限制您尝试解决的问题的范围,因为为所有类型的名片开发通用检测系统将非常困难!
我做了一些研究,我将与你分享我发现的一些有趣的材料:
Text Detection in Images Locating Text within image Determining which are the text and graphic regions in an image document image processing Algorithm to detect presence of text on image Extracting part of a scanned document (personal ID) text detection【讨论】:
太棒了,所有武器都在一个页面中.. 一个很大的 +1。以上是关于如何从图像的直方图中过滤出感兴趣的对象?的主要内容,如果未能解决你的问题,请参考以下文章