我尝试使用OpenCV检测以下框尺寸。如果你发现它有用,我也有深度图像。
我所尝试的是使用霍夫变换并得到线的相互交叉点......
我的问题是,是否有可能获得轮廓然后在所选轮廓上使用霍夫变换?代码怎么样呢?
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
cv::Point2f center( 0, 0 );
cv::Point2f computeIntersect( cv::Vec4i a,
cv::Vec4i b )
{
int x1 = a[0], y1 = a[1], x2 = a[2], y2 = a[3], x3 = b[0], y3 = b[1], x4 = b[2], y4 = b[3];
float denom;
if ( float d = ((float)(x1 - x2) * (y3 - y4)) - ((y1 - y2) * (x3 - x4)) )
{
cv::Point2f pt;
pt.x = ((x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4)) / d;
pt.y = ((x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4)) / d;
return pt;
}
else
return cv::Point2f( -1, -1 );
}
void sortCorners( std::vector<cv::Point2f>& corners,
cv::Point2f center )
{
std::vector<cv::Point2f> top, bot;
for ( int i = 0; i < corners.size(); i++ )
{
if ( corners[i].y < center.y )
top.push_back( corners[i] );
else
bot.push_back( corners[i] );
}
corners.clear();
if ( top.size() == 2 && bot.size() == 2 ) {
cv::Point2f tl = top[0].x > top[1].x ? top[1] : top[0];
cv::Point2f tr = top[0].x > top[1].x ? top[0] : top[1];
cv::Point2f bl = bot[0].x > bot[1].x ? bot[1] : bot[0];
cv::Point2f br = bot[0].x > bot[1].x ? bot[0] : bot[1];
corners.push_back( tl );
corners.push_back( tr );
corners.push_back( br );
corners.push_back( bl );
}
}
int main( int argc, char** argv )
{
Mat src, src_copy, edges, dst;
src = imread( "freezeFrame__1508152029892.png", 0 );
src_copy = src.clone();
GaussianBlur( src, edges, Size( 5, 5 ), 1.5, 1.5 );
erode( edges, edges, Mat() );// these lines may need to be optimized
dilate( edges, edges, Mat() );
dilate( edges, edges, Mat() );
erode( edges, edges, Mat() );
Canny( edges, dst, 50, 300, 3 ); // canny parameters may need to be optimized
imshow( "canny", dst );
vector<Point> selected_points;
vector<vector<Point> > contours;
findContours( dst, contours, RETR_LIST, CHAIN_APPROX_SIMPLE );
for ( size_t i = 0; i < contours.size(); i++ )
{
Rect minRect = boundingRect( contours[i] );
if ( (minRect.width > src.cols / 2) | (minRect.height > src.rows / 2) ) // this line also need to be optimized
{
selected_points.insert( selected_points.end(), contours[i].begin(), contours[i].end() );
drawContours( src, contours, i, Scalar( 0, 0, 255 ), 3 );
}
}
imshow( "Selected contours", src );
cv::waitKey( 0 );
vector<Vec4i> lines;
HoughLinesP( selected_points, lines, 1, CV_PI / 180, 50, 50, 10 );
for ( size_t i = 0; i < lines.size(); i++ )
{
cv::Vec4i v = lines[i];
lines[i][0] = 0;
lines[i][1] = ((float)v[1] - v[3]) / (v[0] - v[2]) * -v[0] + v[1];
lines[i][2] = src.cols;
lines[i][3] = ((float)v[1] - v[3]) / (v[0] - v[2]) * (src.cols - v[2]) + v[3];
v = lines[i];
cv::line( src, cv::Point( v[0], v[1] ), cv::Point( v[2], v[3] ), CV_RGB( 0, 255, 0 ) );
}
std::vector<cv::Point2f> corners;
for ( int i = 0; i < lines.size(); i++ )
{
for ( int j = i + 1; j < lines.size(); j++ )
{
cv::Point2f pt = computeIntersect( lines[i], lines[j] );
if ( pt.x >= 0 && pt.y >= 0 )
corners.push_back( pt );
}
}
for ( int i = 0; i < corners.size(); i++ )
{
// Draw corner points
cv::circle( src, corners[i], 3, CV_RGB( 255, 0, 0 ), 2 );
}
imshow( "line src", src );
imshow("line dest", edges );
cv::waitKey( 0 );
return 0;
}
我想实际计算那个盒子的体积......
答案 0 :(得分:0)
这是一个非常广泛的问题......我会给你一些关于我将如何继续的建议,而不是代码。我想你想要识别休闲桌上任何一个盒子的体积,而不仅仅是这个特定图像中的盒子。
使用(据称)密集深度图像,您可以重建场景的3D点云。您可以使用pcl library和RANSAC等拟合方法来适应pointcloud中的平面。
然后,您应该了解表格的哪个表面,然后找到框表面并计算封闭的体积。
另一种方法是使用cv :: goodFeaturesToTrack()在图像中找到角点,再次使用深度图像计算相应的3D点,仅计算检测到的角落的3D位置,然后找到连接角落的线条并尝试找出哪些线垂直于其他线。