天道酬勤,学无止境

Speeding up HoughCircles

I have problem with the houghcircles function, regarding its processing speed. when i try to run the code with this video file, i uploaded onto Youtube. https://youtu.be/5RGRTPEdHVY

The problem is, when the copter is taking off, the code is stucked, and stop moving. At this stage, when the copter is taking off, the param must be set manually at 300 to avoid being stucked. then after taking off, the param must be reduced to about 150 or 130 to detect the circles on the field.

Now i do not want to do it manually, is there a way to do it intelligently?

Or is there any way to ensure the video is smooth? are my parameters too small? And what is the unit for these parameters?

Also i wish to know if it is the Hough Circle that is slowing things down or the drawing of the circles?

Thank you

Full Code is attached.

#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv\cv.h>
#include <opencv\highgui.h>
#include <stdlib.h>
#include <stdio.h>
#include <opencv2\opencv.hpp>
#include <time.h>
#include <fstream>

#include <stdio.h>
#include <conio.h>
#include "tserial.h"
#include "bot_control.h"


using namespace std;
using namespace cv;


int main(int argc, char** argv) {


    //Create a window for trackbars
    namedWindow("Trackbar Window", CV_WINDOW_AUTOSIZE);

    //Create trackbar to change brightness
    int iSliderValue1 = 50;
    createTrackbar("Brightness", "Trackbar Window", &iSliderValue1, 100);

    //Create trackbar to change contrast
    int iSliderValue2 = 50;
    createTrackbar("Contrast", "Trackbar Window", &iSliderValue2, 100);

    //Create trackbar to change param1 in HoughCircle
    int param1 = 300;
    createTrackbar("param1", "Trackbar Window", &param1, 300);

    //Create trackbar to change param2 in HoughCircle
    int param2 = 300;
    createTrackbar("param2", "Trackbar Window", &param2, 300);

    //Create trackbar to change min Radius in HoughCircle
    int minR = 0;
    createTrackbar("minRadius", "Trackbar Window", &minR, 300);

    //Create trackbar to change max Radius in HoughCircle
    int maxR = 0;
    createTrackbar("maxRadius", "Trackbar Window", &maxR, 300);

    //Debugging purpose
    cout << "All trackbars created" << endl;

    int iBrightness;
    double dContrast;

    double dparam1;
    double dparam2;

    time_t start, end;
    int counter = 0;

    ofstream myfile;

    serial comm;
    char data = 1;

    comm.startDevice("COM3", 9600);

    time(&start);   
    //Create a variable to store image
    Mat src;
    //Create video capture
    VideoCapture capture;
    //open video from either a file or webcam

    capture.open("C:\\Users\\Student-ID\\Downloads\\GOPR0506.MP4");
    //capture.open(0);



    //set frame height
    //capture.set(CV_CAP_PROP_FRAME_HEIGHT, 120);
    //capture.set(CV_CAP_PROP_FRAME_WIDTH, 120);

    //Set the capture FPS
    //capture.set(CV_CAP_PROP_FPS,25);

    //store whatever is captured to src
    capture.read(src);

    //Debugging purpose
    cout << "Vidoe opened" << endl;

    if (!src.data) {
        std::cout << "ERROR:\topening image" << std::endl;
        return -1;
    }

    //Create window to display videos
    cv::namedWindow("image1", CV_WINDOW_AUTOSIZE);

    vector<Vec3f> circles;
    while (true){

        capture.read(src);
        //Code for changing Brightness and contrast
        iBrightness = iSliderValue1 - 50;
        dContrast = iSliderValue2 / 50.0;
        src.convertTo(src, -1, dContrast, iBrightness);

        //Debugging purpose
        //cout << "1" << endl;


        //Create variable to store the processed image
        Mat src_gray2;
        //Convert the colour to grayscale
        cvtColor(src, src_gray2, CV_BGR2GRAY);
        //Smooth and blur the image to reduce noise
        GaussianBlur(src_gray2, src_gray2, cv::Size(9, 9), 2, 2);





        //Change the param1 and 2 to double from integer
        dparam1 = param1 / 1.0;
        dparam2 = param2 / 1.0;

        //Debugging purpose
        cout << "2" << endl;

        //Apply HoughCircle function
        HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT,
        2,   // accumulator resolution (size of the image / 2)
        5,  // minimum distance between two circles
        dparam1, // Canny high threshold
        dparam2, // minimum number of votes
        minR, maxR); // min and max radius

        //Debugging purpose
        cout << "3" << endl;

        cout << "size of circle is: "<< circles.size() << endl;


        if (circles.size() != 0){
            //Draw the circle
            for (size_t i = 0; i < circles.size(); i++)
            {
                Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
                int radius = cvRound(circles[i][2]);
                circle(src, center, 3, Scalar(0, 255, 0), -1, 8, 0);
                // circle outline
                circle(src, center, radius, Scalar(0, 0, 255), 3, 8, 0);
                //Display words on top left hand corner
                putText(src, "Circle Found", Point(0, 50), 1, 2, Scalar(0, 255, 0), 2);

                comm.send_data(data);
            }



        }



        //display video

        imshow("image1", src);

        //debugging purpose
        cout << "5" << endl;

        //delay to refresh the pic
        int key = cvWaitKey(33);
        if (key == 27) break;

        time(&end);
        ++counter;
        cout << "fps is: " << counter / difftime(end, start) << endl << endl;
    }
    return 0;
}

EDIT: I tried limiting the number of circles being drawn suggested by Miki (Thank you). The frame still stops at take off, although it is only drawing 2 circles and maybe 5 circles on the next frame.

Please do comment if you have some methods in mind.

评论

I recently use Houghcircle function. I have a little experience. You declare circles(variable) that is global variable, and if you move copter but HoughCircle fcn haven't found the circle yet. It will remain circles values (center(x,y),radius) in the past, so the image looks like some latency. You can write about Hough's program as a subroutine. vector circles variable that is local variable in this subroutine. May improve latency.

受限制的 HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。

相关推荐
  • HoughCircles 无法检测到此图像上的圆圈(HoughCircles can't detect circles on this image)
    问题 我试图在我的图像中检测包含圆点的圆,但不幸的是我无法这样做。 我正在使用 opencv HoughTransform,但找不到使这项工作的参数。 src = imread("encoded.jpg",1); /// Convert it to gray cvtColor(src, src_gray, CV_BGR2GRAY); vector<Vec3f> circles; /// Apply the Hough Transform to find the circles HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, 10, 100, 30, 1, 30 // change the last two parameters // (min_radius & max_radius) to detect larger circles ); /// Draw the circles detected for (size_t i = 0; i < circles.size(); i++) { cout << "Positive" << endl; Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius = cvRound(circles
  • OpenCV中用于虹膜检测的HoughCircles的正确用法/参数值是多少?(What are the correct usage/parameter values for HoughCircles in OpenCV for Iris detection?)
    问题 我一直在阅读有关该主题的内容,但无法用“普通英语”来了解HoughCircles的用法和参数(特别是CV_HOUGH_GRADIENT之后的用法和参数)。 什么是累加器阈值? 100个“票”是正确的值吗? 我可以找到并“遮盖”学生,并通过Canny函数进行工作,但是我为此感到挣扎,而我的问题是HoughCircles函数。 似乎找不到鸢尾花的圈子,我也不知道为什么。 这是我正在处理的功能: def getRadius(area): r = 1.0 r = math.sqrt(area/3.14) return (r) def getIris(frame): grayImg = cv.CreateImage(cv.GetSize(frame), 8, 1) cv.CvtColor(frame,grayImg,cv.CV_BGR2GRAY) cv.Smooth(grayImg,grayImg,cv.CV_GAUSSIAN,9,9) cv.Canny(grayImg, grayImg, 32, 2) storage = cv.CreateMat(grayImg.width, 1, cv.CV_32FC3) minRad = int(getRadius(pupilArea)) circles = cv.HoughCircles(grayImg, storage, cv.CV
  • Using HoughCircles to detect and measure pupil and iris
    I'm trying to use OpenCV, more specifically its HoughCircles to detect and measure the pupil and iris, currently I've been playing with some of the variables in the function, because it either returns 0 circles, or an excessive amount. Below is the code and test image I'm using. Code for measuring iris: eye1 = [self increaseIn:eye1 Contrast:2 andBrightness:0]; cv::cvtColor(eye1, eye1, CV_RGBA2RGB); cv::bilateralFilter(eye1, eye2, 75, 100, 100); cv::vector<cv::Vec3f> circles; cv::cvtColor(eye2, eye1, CV_RGBA2GRAY); cv::morphologyEx(eye1, eye1, 4, cv::getStructuringElement(cv::MORPH_RECT,cv
  • 使用HoughCircles检测和测量瞳孔和虹膜(Using HoughCircles to detect and measure pupil and iris)
    问题 我正在尝试使用OpenCV,更具体地说是使用其HoughCircles来检测和测量瞳孔和虹膜,目前我一直在使用该函数中的某些变量,因为它要么返回0个圆,要么返回过多的圆。 下面是我正在使用的代码和测试图像。 测量虹膜的代码: eye1 = [self increaseIn:eye1 Contrast:2 andBrightness:0]; cv::cvtColor(eye1, eye1, CV_RGBA2RGB); cv::bilateralFilter(eye1, eye2, 75, 100, 100); cv::vector<cv::Vec3f> circles; cv::cvtColor(eye2, eye1, CV_RGBA2GRAY); cv::morphologyEx(eye1, eye1, 4, cv::getStructuringElement(cv::MORPH_RECT,cv::Size(3, 3))); cv::threshold(eye1, eye1, 0, 255, cv::THRESH_OTSU); eye1 = [self circleCutOut:eye1 Size:50]; cv::GaussianBlur(eye1, eye1, cv::Size(7, 7), 0); cv::HoughCircles(eye1, circles, CV
  • OpenCV HoughCircles(OpenCV HoughCircles)
    问题 我使用 Xcode 和 C++ 我已经从 OpenCV 文档中复制了 HoughCircles 代码: #include <cv.h> #include <highgui.h> #include <math.h> using namespace cv; int main(int argc, char** argv) { Mat img, gray; if( argc != 2 && !(img=imread(argv[1], 1)).data) return -1; cvtColor(img, gray, CV_BGR2GRAY); // smooth it, otherwise a lot of false circles may be detected GaussianBlur( gray, gray, Size(9, 9), 2, 2 ); vector<Vec3f> circles; HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 2, gray->rows/4, 200, 100 ); for( size_t i = 0; i < circles.size(); i++ ) { Point center(cvRound(circles[i][0]), cvRound(circles[i][1])); int radius
  • OpenCV Python HoughCircles 错误(OpenCV Python HoughCircles error)
    问题 我正在开发一个检测图像中圆形形状的程序。 我认为 Hough 变换是最好的,我在 OpenCV 库中找到了一个。 问题是,当我尝试使用它时,出现了一个我不知道如何修复的错误。 Python 的 OpenCV 没有完全实现吗? 是否有修复程序运行所需的库? 这是代码: import cv #cv.NamedWindow("camera", 1) capture = cv.CaptureFromCAM(0) while True: img = cv.QueryFrame(capture) gray = cv.CreateImage(cv.GetSize(img), 8, 1) edges = cv.CreateImage(cv.GetSize(img), 8, 1) cv.CvtColor(img, gray, cv.CV_BGR2GRAY) cv.Canny(gray, edges, 50, 200, 3) cv.Smooth(gray, gray, cv.CV_GAUSSIAN, 9, 9) storage = cv.CreateMat(1, 2, cv.CV_32FC3) #This is the line that throws the error cv.HoughCircles(edges, storage, cv.CV_HOUGH_GRADIENT, 2, gray
  • OpenCV HoughCircles 偶尔会返回 [0. 0. 0.](OpenCV HoughCircles occasionally returning [0. 0. 0.])
    问题 我在 Raspberry Pi 上的 HSV 过滤 PiCamera 馈送上运行HoughCircles()以检测粉红色球。 有时我会得到[[[0. 0. 0.]]] [[[0. 0. 0.]]]与np.zeros(shape=(1, 1, 3)) ,而不是在相机视图中没有圆形时预期的None 。 我查看了文档,但没有看到有关为什么会返回此内容的任何详细信息。 我还查看了源代码,但找不到 C++ 代码的 Python 包装器,这似乎对我也没有帮助。 这是我的函数调用: circles = cv2.HoughCircles(frame, cv2.HOUGH_GRADIENT, 3, frame.shape[0] / 4, param1=220, param2=110, minRadius=5) 我想知道为什么它正在做一些事情,比如识别一些我什至在imshow()上都看不到的非常小的东西(它在 (0,0) 周围总是完全黑色;几十行和列只有零),那么为什么它总是在 (0,0) 处,半径为 0? 当我提供minRadius=5时,我看不到它如何返回半径为 0 的圆。 我不相信这实际上是一个有效的检测。 有时需要数百次执行才能发生这种情况,但似乎我可以通过快速移动相机的某些东西(例如我的手)来触发它。 任何想法为什么会发生这种情况,也许其他人经历过这种情况? 回答1 我在 C++
  • 无助地迷失在 openCV 和 HoughCircles(Helplessly lost with openCV and HoughCircles)
    问题 我试图在这里检测这个黑色圆圈。 应该不会太难,但出于某种原因,我到处都只有 0 个圆圈或大约 500 个圆圈,具体取决于参数。 但没有中间立场。 感觉就像我已经尝试玩了几个小时的论点,但绝对没有成功。 使用 HoughCircles 和黑白图片有问题吗? 这项任务对人眼来说似乎很简单,但出于某种原因,这对计算机来说很难吗? 这是我的代码: import numpy as np import cv2 image = cv2.imread('temp.png') output = image.copy() blurred = cv2.blur(image,(10,10)) gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY) circles = cv2.HoughCircles(gray, cv2.HOUGH_GRADIENT, 1.5, 20, 100, 600, 10, 100) if circles is not None: circles = np.round(circles[0, :]).astype("int") print len(circles) for (x, y, r) in circles: cv2.circle(output, (x, y), r, (0, 255, 0), 4) cv2.rectangle
  • HoughCircles参数来识别球(HoughCircles Parameters to recognise balls)
    问题 通过将图像转换为灰度然后进行模糊处理后,我尝试使用以下参数应用霍夫圆变换: CV_HOUGH_GRADIENT dp = 1 min_dist = 1 param_1 = 70 param_2 = 100 min_radius = 0 max_radius = 0 这是我尝试过的众多图像之一:http://i.stack.imgur.com/JGRiM.jpg 但是,即使使用松弛的参数,该算法也无法识别出球。 (当我尝试使用在GIMP中创建的圆形图像时,它可以正常工作) 回答1 我同意克尔奇(Krzych)的观点。 我毫不费力地与它一起工作: cv::Mat img,img2; std::vector<cv::Vec3f> circles; img = cv::imread("JGRiM.jpg",1); cv::bilateralFilter(img, img2, 15, 1000, 1000); cv::cvtColor(img2, img2,CV_BGR2GRAY); cv::HoughCircles(img2, circles, CV_HOUGH_GRADIENT, 1,300,50, 10); cv::circle(img2,cv::Point(circles[0][0],circles[0][1]),circles[0][2],cv::Scalar(126),2)
  • 使用 opencv 和 python 检测 HoughCircles 圆(HoughCircles circle detection using opencv and python-)
    问题 我正在尝试使用 OpenCV 的 (Hough)Circle 检测来检测圆圈。 我在黑色背景上创建了一个实心圆圈,尝试使用参数,使用模糊和所有内容,但我无法让它找到任何东西。 任何想法,建议等都会很棒,谢谢! 我目前的代码是这样的: import cv2 import numpy as np """ params = dict(dp=1, minDist=1, circles=None, param1=300, param2=290, minRadius=1, maxRadius=100) """ img = np.ones((200,250,3), dtype=np.uint8) for i in range(50, 80, 1): for j in range(40, 70, 1): img[i][j]*=200 cv2.circle(img, (120,120), 20, (100,200,80), -1) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) canny = cv2.Canny(gray, 200, 300) cv2.imshow('shjkgdh', canny) gray = cv2.medianBlur(gray, 5) circles = cv2.HoughCircles(gray, cv2.cv.CV
  • iOS 的 OpenCV 错误/检测霍夫圆(OpenCV errors for iOS / detecting Hough Circles)
    问题 我一直在尝试使用 openCV 运行 xcode 项目数小时。 我已经构建了源代码,将其导入到项目中,并在 .pch 文件中包含了 #ifdef __cplusplus #import opencv2/opencv.hpp> #endif。 我按照 http://docs.opencv.org/trunk/doc/tutorials/introduction/ios_install/ios_install.html 中的说明进行操作 我在编译时仍然遇到许多 Apple Mach-O 链接器错误。 Undefined symbols for architecture i386: "std::__1::__vector_base_common<true>::__throw_length_error() const", referenced from: 请帮帮我我真的迷路了.. 更新: 错误全部修复,现在我正在尝试检测圆圈.. Mat src, src_gray; cvtColor( image, src_gray, CV_BGR2GRAY ); vector<Vec3f> circles; /// Apply the Hough Transform to find the circles HoughCircles( src_gray, circles, CV_HOUGH
  • 显示使用 houghcircles opencv 函数后的累加器结果(Show the accumulator result after use houghcircles opencv function)
    问题 请我想为 opencv hougcircles 函数绘制累加器输出。 . 该函数运行正常并仅检测到圆(中心和半径)我想显示此函数的累加器 函数Opencv\modules\imgprocs\src\hough.cpp的路径霍夫圆的函数名icvHoughCirclesGradient累加器的参数是“accum”。 任何帮助请谢谢
  • 环形霍夫变换错过了圈子(Circular Hough Transform misses circles)
    问题 我已经阅读了很多有关Stack Overflow上的Round Hough变换的信息,但似乎缺少了一些东西。 我编写了一个程序来检测“靶心”目标的圆圈。 但是,即使在使用了参数之后,该算法也相当糟糕-它忽略了大多数圆圈,有一次它找到了一个圆圈,但似乎在“徘徊”。 我什至尝试应用“不清晰的蒙版”都无济于事。 我已经添加了代码,我开始使用的图像和输出。 我希望有人能指出我正确的方向。 import cv2 import cv2.cv as cv import numpy as np import math # Load Image img = cv2.imread('circles1.png',0) # Apply Unsharp Mask tmp = cv2.medianBlur(img,5) img = cv2.addWeighted(img,1.5,tmp,-0.5,0) cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR) # Hough Transform circles = cv2.HoughCircles(img,cv.CV_HOUGH_GRADIENT,1,5, param1=100,param2=100,minRadius=0,maxRadius=0) circles = np.uint16(np.around(circles)
  • OpenCV HoughCircles
    Im using Xcode and c++ I have copied the HoughCircles code from the OpenCV documentation: #include <cv.h> #include <highgui.h> #include <math.h> using namespace cv; int main(int argc, char** argv) { Mat img, gray; if( argc != 2 && !(img=imread(argv[1], 1)).data) return -1; cvtColor(img, gray, CV_BGR2GRAY); // smooth it, otherwise a lot of false circles may be detected GaussianBlur( gray, gray, Size(9, 9), 2, 2 ); vector<Vec3f> circles; HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 2, gray->rows/4, 200, 100 ); for( size_t i = 0; i < circles.size(); i++ ) { Point center(cvRound(circles[i][0])
  • What are the correct usage/parameter values for HoughCircles in OpenCV for Iris detection?
    I've been reading about the subject but cannot get the idea in "plain English" about the usage and parameters for HoughCircles (specially the ones after CV_HOUGH_GRADIENT). What's an accumulator threshold? Are 100 "votes" a right value? I could find and "mask" the pupil, and worked my way through the Canny function, but I'm struggling beyond that and my problem is the HoughCircles function. There seems to be failing at finding the Iris' circle and I don't know why. And this is the function I'm working on: def getRadius(area): r = 1.0 r = math.sqrt(area/3.14) return (r) def getIris(frame)
  • cv2.HoughCircles的结果不可靠(Unreliable results with cv2.HoughCircles)
    问题 我有一个包含5个油滴的视频,我正在尝试使用cv2.HoughCircles查找它们。 这是我的代码: import cv, cv2 import numpy as np foreground1 = cv2.imread("foreground1.jpg") vid = cv2.VideoCapture("NB14.avi") cv2.namedWindow("video") cv2.namedWindow("canny") cv2.namedWindow("blur") while True: ret, frame = vid.read() subtract1 = cv2.subtract( foreground1, frame) framegrey1 = cv2.cvtColor(subtract1, cv.CV_RGB2GRAY) blur = cv2.GaussianBlur(framegrey1, (0,0), 2) circles = cv2.HoughCircles(blur, cv2.cv.CV_HOUGH_GRADIENT, 2, 10, np.array([]), 40, 80, 5, 100) if circles is not None: for c in circles[0]: cv2.circle(frame, (c[0],c[1]), c[2],
  • Unreliable results with cv2.HoughCircles
    I have a video with 5 oil droplets, and I am trying to use cv2.HoughCircles to find them. This is my code: import cv, cv2 import numpy as np foreground1 = cv2.imread("foreground1.jpg") vid = cv2.VideoCapture("NB14.avi") cv2.namedWindow("video") cv2.namedWindow("canny") cv2.namedWindow("blur") while True: ret, frame = vid.read() subtract1 = cv2.subtract( foreground1, frame) framegrey1 = cv2.cvtColor(subtract1, cv.CV_RGB2GRAY) blur = cv2.GaussianBlur(framegrey1, (0,0), 2) circles = cv2.HoughCircles(blur, cv2.cv.CV_HOUGH_GRADIENT, 2, 10, np.array([]), 40, 80, 5, 100) if circles is not None: for c
  • Helplessly lost with openCV and HoughCircles
    I'm trying to detect this black circle here. Shouldn't be too difficult but for some reason I just get 0 circles or approximately 500 circles everywhere, depending on the arguments. But there is no middle ground. Feels like I have tried to play with the arguments for hours, but absolutely no success. Is there a problem using HoughCircles and black or white picture? The task seems simple to a human eye, but is this difficult to the computer for some reason? Here's my code: import numpy as np import cv2 image = cv2.imread('temp.png') output = image.copy() blurred = cv2.blur(image,(10,10)) gray =
  • OpenCV Python HoughCircles error
    I'm working on a program that detects circular shapes in images. I decided a Hough Transform would be the best, and I found one in the OpenCV library. The problem is that when I try to use it I get an error that I have no idea how to fix. Is OpenCV for Python not fully implemented? Is there a fix to the library I need for the program to work? Here's the code: import cv #cv.NamedWindow("camera", 1) capture = cv.CaptureFromCAM(0) while True: img = cv.QueryFrame(capture) gray = cv.CreateImage(cv.GetSize(img), 8, 1) edges = cv.CreateImage(cv.GetSize(img), 8, 1) cv.CvtColor(img, gray, cv.CV
  • OpenCV HoughCircles occasionally returning [0. 0. 0.]
    I'm running HoughCircles() on an HSV filtered PiCamera feed on a Raspberry Pi to detect a pink ball. Occasionally I will get a ndarray result of [[[0. 0. 0.]]] which is the same as np.zeros(shape=(1, 1, 3)), rather than the expected None when there is no round shape in the camera's view. I looked at the docs but didn't see any details on why it would return this. I also looked at the source but can't find the Python wrapper for the C++ code, which doesn't seem to help me either. Here's my function call: circles = cv2.HoughCircles(frame, cv2.HOUGH_GRADIENT, 3, frame.shape[0] / 4, param1=220