我正在尝试使用OpenCV在Java中进行一些简单的行人检测。 这是我的代码:
SimpleBlobDetector blobDetector;
blobDetector.detect(mask, matOfKeyPoints);
org.opencv.core.Scalar cores = new org.opencv.core.Scalar(255, 0, 0);
org.opencv.features2d.Features2d.drawKeypoints(frame, matOfKeyPoints, frame, cores, Features2d.DRAW_RICH_KEYPOINTS);
这是一项非常可靠的工作,但我在行人周围的边界框实际上是圆圈,我需要矩形。
答案 0 :(得分:0)
不幸的是,圆圈是内置的,并且没有默认标志来绘制矩形。请参阅DRAW_RICH_KEYPOINTS
documentation。
但是,由于您已经拥有了关键点位置,因此您可以编写自己的绘图功能。 drawKeypoints
函数的c ++版本为here。以下是绘制矩形的新函数的简洁版本。
void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage, const Scalar& _color)
{
bool isRandColor = _color == Scalar::all(-1);
std::vector<KeyPoint>::const_iterator it = keypoints.begin(),
end = keypoints.end();
for( ; it != end; ++it )
{
Scalar color = isRandColor ? Scalar(rng(256), rng(256), rng(256)) : _color;
p=*it;
Point center( cvRound(p.pt.x * draw_multiplier), cvRound(p.pt.y * draw_multiplier) );
int radius = cvRound(p.size/2 * draw_multiplier); // KeyPoint::size is a diameter
// draw RECTANGLES (actually squares) around keypoints with the keypoints size
rectangle(outImage, center + Point(-radius, -radius), center + Point(radius, radius), color, 1, LINE_AA, draw_shift_bits);
// draw orientation of the keypoint, if it is applicable
if( p.angle != -1 )
{
float srcAngleRad = p.angle*(float)CV_PI/180.f;
Point orient( cvRound(cos(srcAngleRad)*radius ),
cvRound(sin(srcAngleRad)*radius )
);
line( outImage, center, center+orient, color, 1, LINE_AA, draw_shift_bits );
}
}
}