问题描述
我想使用opencv BoundingRect在白色背景上得到填充的黑色圆圈的边框。我使用的示例代码来自,但未能获取边框的属性并将其绘制到图像中。应该是一个简单的问题,我想,但仍然我不能这样做...
I want to get the bounding box of a filled black circle on a white background using opencv BoundingRect. I used the sample code from http://cgi.cse.unsw.edu.au/~cs4411/wiki/index.php?title=OpenCV_Guide#Finding_bounding_boxes_around_regions_of_a_binary_image but failed to get the properties of the bounding box and draw it into the image. Should be a simple problem, I think, but still I fail doing it...
如果你能写下一些示例代码,会很好。
Would be nice, if you could write down some sample code.
感谢。
我目前使用的图片是
我的源代码是:
#include <iostream>
#include <vector>
#include <cv.h>
#include <highgui.h>
using namespace std;
int main(int argc, char** argv){
IplImage* img_in = cvLoadImage("Schwarzer_Kreis.png",1);
IplImage* img_working = cvCreateImage(cvGetSize(img_in), 8, 1);
cvCvtColor(img_in, img_working, CV_BGR2GRAY);
CvSeq* seq;
vector<CvRect> boxes;
CvMemStorage* storage = cvCreateMemStorage(0);
cvClearMemStorage(storage);
cvFindContours(img_working, storage, &seq, sizeof(CvContour), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cvPoint(600,200));
CvRect boundbox ;
for(; seq; seq = seq->h_next) {
boundbox = cvBoundingRect(seq);
boxes.push_back(boundbox);
}
for (int ii=0; ii<boxes.size(); ii++) {
cout << boxes[ii].x << endl;
cout << boxes[ii].y << endl;
cout << boxes[ii].width << endl;
cout << boxes[ii].height << endl;
}
cvWaitKey(0);
return 0;
}
从程序输出的是:
The output that I geht from the program is:
601
201
1390
1038
推荐答案
我自己的代码用于计算边界框:
I made my own code for calculating the bounding box:
int* get_boundingbox(
IplImage* img_input){
int width = img_input->width;
int height = img_input->height;
int* Output = NULL;
Output = new int[4];
//----- Top boundary -----
for(int ii = 0 ; ii < height ; ii++ ){
int value;
for(int jj = 0 ; jj < width ; jj++ ) {
value = static_cast<int>(cvGetReal2D(img_input,1039-ii,jj));
if (value == 0) {
Output[1] = 1039-ii; // upper left corner, y-value
break;
}
if (value == 0) break;
}
}
//----- Left boundary -----
for(int ii = 0 ; ii < width ; ii++ ){
int value;
for(int jj = 0 ; jj < height ; jj++ ) {
value = static_cast<int>(cvGetReal2D(img_input,jj,1391-ii));
if (value == 0) {
Output[0] = 1391-ii; //upper left corner. x-value
break;
}
if (value == 0) break;
}
}
//----- Right boundary -----
for(int ii = 0 ; ii < width ; ii++ ){
int value;
for(int jj = 0 ; jj < height ; jj++ ) {
value = static_cast<int>(cvGetReal2D(img_input,jj,ii));
if (value == 0) {
Output[2] = ii+1; // lower right corner, x-value
break;
}
if (value == 0) break;
}
}
//----- Bottom boundary -----
for(int ii = 0 ; ii < height ; ii++ ){
int value;
for(int jj = 0 ; jj < width ; jj++ ) {
value = static_cast<int>(cvGetReal2D(img_input,ii,jj));
if (value == 0) {
Output[3] = ii+1; // lower right corner, y-value
break;
}
if (value == 0) break;
}
}
Output[2] = Output[2] - Output[0] + 1; // width
Output[3] = Output[3] - Output[1] + 1; // height
return Output;
delete []Output;
}
这篇关于使用OpenCV的边界框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!