如何获取javacv中提取的对象的x,y坐标?
发布时间:2020-07-09 00:28:40 所属栏目:Java 来源:互联网
导读:目前我正在开发图像处理项目,我正在使用 javacv开发图像处理组件.我能够提取图像的一些有趣的部分,现在我需要读取这些对象的x和y坐标. 这是我提出的形象 我需要识别这些对象,并绘制这些对象的方块.我经历了一些教程,并尝试使用以下代码识别对象. IplImage img
|
目前我正在开发图像处理项目,我正在使用
javacv开发图像处理组件.我能够提取图像的一些有趣的部分,现在我需要读取这些对象的x和y坐标.
我需要识别这些对象,并绘制这些对象的方块.我经历了一些教程,并尝试使用以下代码识别对象. IplImage img="sourceimage";
CvSize sz = cvSize(img.width(),img.height());
IplImage gry=cvCreateImage(sz,img.depth(),1);
cvCvtColor(img,gry,CV_BGR2GRAY);
cvThreshold(gry,200,250,CV_THRESH_BINARY);
CvMemStorage mem = CvMemStorage.create();
CvSeq contours = new CvSeq();
CvSeq ptr = new CvSeq();
cvFindContours(gry,mem,contours,Loader.sizeof(CvContour.class),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE,cvPoint(0,0));
CvRect boundbox;
for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
boundbox = cvBoundingRect(ptr,0);
if(boundbox.height()>10||boundbox.height()>10){
cvRectangle( gry,cvPoint( boundbox.x(),boundbox.y() ),cvPoint( boundbox.x() + boundbox.width(),boundbox.y() + boundbox.height()),CvScalar.BLUE,0 );
System.out.println(boundbox.x()+","+boundbox.y());
}
}
cvShowImage("contures",gry);
cvWaitKey(0);
但是它不会在对象周围绘制为矩形.我想知道是否可以使用cvFindContours方法来识别这些对象?请问有人可以解释如何使用javacv / opencv归档我的目标? 解决方法尝试通过以下代码,它将为您的问题提供答案.IplImage img=cvLoadImage("pathtosourceimage");
CvSize cvSize = cvSize(img.width(),img.height());
IplImage gry=cvCreateImage(cvSize,255,CV_THRESH_BINARY);
cvAdaptiveThreshold(gry,CV_ADAPTIVE_THRESH_MEAN_C,CV_THRESH_BINARY_INV,11,5);
CvMemStorage storage = CvMemStorage.create();
CvSeq contours = new CvContour(null);
int noOfContors = cvFindContours(gry,storage,CV_CHAIN_APPROX_NONE,new CvPoint(0,0));
CvSeq ptr = new CvSeq();
int count =1;
CvPoint p1 = new CvPoint(0,0),p2 = new CvPoint(0,0);
for (ptr = contours; ptr != null; ptr = ptr.h_next()) {
CvScalar color = CvScalar.BLUE;
CvRect sq = cvBoundingRect(ptr,0);
System.out.println("Contour No ="+count);
System.out.println("X ="+ sq.x()+" Y="+ sq.y());
System.out.println("Height ="+sq.height()+" Width ="+sq.width());
System.out.println("");
p1.x(sq.x());
p2.x(sq.x()+sq.width());
p1.y(sq.y());
p2.y(sq.y()+sq.height());
cvRectangle(img,p1,p2,CV_RGB(255,2,8,0);
cvDrawContours(img,ptr,color,CV_RGB(0,-1,CV_FILLED,0));
count++;
}
cvShowImage("contures",img);
cvWaitKey(0);
这是我给出的给定图像的输出. (编辑:鄂州站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
