HoughLinesP没有检测到OpenCV android行

时间:2015-07-27 08:14:47

标签: java android opencv

我正在使用适用于Android的OpenCV 3.0。我有一个图像,我想要检测圆形表盘内的手的角度。因为我正在- (void)viewDidLoad { [self performSelector:@selector(Fb_Login) withObject:nil]; [super viewDidLoad]; } -(void)Fb_Login { if (!FBSession.activeSession.isOpen) { // if the session is closed, then we open it here, and establish a handler for state changes [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:YES completionHandler:^(FBSession *session,FBSessionState state, NSError *error) { if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Alert" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } else if(session.isOpen) { [self Fb_Login]; } }]; return; } 探测手。 这是代码。

HoughLinesP

但结果是 enter image description here

我需要的是这些圈子中的手的角度。任何有关此问题的帮助都非常感谢。感谢ADvance

修改 我用这个

更新了我的代码
Mat imgSource = new Mat(), imgCirclesOut = new Mat(),imgLinesOut=new Mat();
//grey opencv
Imgproc.cvtColor(Image, imgSource, Imgproc.COLOR_BGR2GRAY);
Imgproc.GaussianBlur( imgSource, imgSource, new Size(9, 9), 2, 2 );

int threshold = 0;
int minLineSize = 0;
int lineGap = 0;

Imgproc.HoughLinesP(imgSource, imgLinesOut, 1, Math.PI/180, threshold, minLineSize, lineGap);
for( int j = 0; i < imgLinesOut.cols(); i++ )
{
    double[] vec=imgLinesOut.get(0,j);
    Point pt1, pt2;
    pt1=new Point(vec[0],vec[1]);
    pt2=new Point(vec[2],vec[3]);
    Imgproc.line( Image, pt1, pt2, new Scalar(0,0,255), 3, Core.LINE_AA,0);
}

正如@Micka所建议的那样,不需要灰化图像(我删除了 Mat imgSource = new Mat(), imgCirclesOut = new Mat(),imgLinesOut=new Mat(); Imgproc.GaussianBlur( Image, imgSource, new Size(5, 5), 2, 2 ); int threshold = 20; int minLineSize = 0; int lineGap = 10; Imgproc.Canny(imgSource, imgSource, 70, 100); Imgproc.HoughLinesP(imgSource, imgLinesOut, 1, Math.PI/180, threshold, minLineSize, lineGap); for( int j = 0; j < imgLinesOut.cols(); j++ ) { double[] vec=imgLinesOut.get(0,j); Point pt1, pt2; pt1=new Point(vec[0],vec[1]); pt2=new Point(vec[2],vec[3]); Imgproc.line( imgSource, pt1, pt2, new Scalar(0,0,255), 3, Core.LINE_AA,0); } )。我还将cvtcolor尺寸的值减少到GuassianBlur。我也在图像上添加了Canny for edge。

产生的模糊图像

enter image description here

2 个答案:

答案 0 :(得分:3)

在如此小的图像中检测线条可能是个问题,因为你需要很少的点才能正确填充霍夫累加器。

我建议使用不同的方法:

  1. 细分每个圆圈(表盘)
  2. 提取最大的黑色斑点(手)
  3. 以下是这个想法的简单实现。代码是用C ++编写的,但您可以轻松地移植到Java,或至少用作参考。

    #include "opencv2/opencv.hpp"
    using namespace cv;
    
    int main(int, char**)
    {
        Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
    
        Mat3b res;
        cvtColor(img, res, COLOR_GRAY2BGR);
    
        // Find dials
        vector<Vec3f> circles;
        HoughCircles(img, circles, CV_HOUGH_GRADIENT, 1, img.cols/10, 400, 40);
    
        // For each dial
        for (int i = 0; i < circles.size(); ++i)
        {
    
            // Segment the dial
            Mat1b dial(img.size(), uchar(255));
            Mat1b mask(img.size(), uchar(0));
            circle(mask, Point(circles[i][0], circles[i][1]), circles[i][2], Scalar(255), CV_FILLED);
            img.copyTo(dial, mask);
    
            // Apply threshold and open
            Mat1b bin;
            threshold(dial, bin, 127, 255, THRESH_BINARY_INV);
            Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(5,5));
            morphologyEx(bin, bin, MORPH_OPEN, kernel);
    
            // Get min area rect
            vector<Point> points;
            findNonZero(bin, points);
            RotatedRect r = minAreaRect(points);
    
            // Draw min area rect
            Point2f pts[4];
            r.points(pts);
            for (int j = 0; j < 4; ++j) {
                line(res, pts[j], pts[(j + 1) % 4], Scalar(0, 255, 0), 1);
            }       
        }
    
        imshow("Result", res);
        waitKey();
    
        return 0;
    }
    

    从这张图片开始:

    enter image description here

    我在这里找到 hands

    enter image description here

答案 1 :(得分:-1)

for( int j = 0; j < imgLinesOut.size(); j++ )

这将给出vector的大小。迭代该向量

相关问题