C++ 带有轨迹栏opencv的HoughCircle
C++ 带有轨迹栏opencv的HoughCircle,c++,opencv,opencv3.0,hough-transform,C++,Opencv,Opencv3.0,Hough Transform,houghcirle函数中有几个参数。 有没有办法使用轨迹栏来更改这些参数? 这样我就不必每次想更改它们时都运行程序 多谢各位 使用OpenCV 3.0.0C++对Win 8笔记本电脑进行MS vs 2013 #include <sstream> #include <string> #include <iostream> #include <vector> #include "opencv2/highgui/highgui.hpp" #includ
houghcirle
函数中有几个参数。
有没有办法使用轨迹栏来更改这些参数?
这样我就不必每次想更改它们时都运行程序
多谢各位
使用OpenCV 3.0.0C++对Win 8笔记本电脑进行MS vs 2013
#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>
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);
int param1 = 10;
createTrackbar("param1", "Trackbar Window", ¶m1, 300);
int param2 = 10;
createTrackbar("param2", "Trackbar Window", ¶m2, 300);
cout << "All trackbars created" << endl;
Mat src;
VideoCapture capture;
capture.open("movingBall.wmv");
capture.read(src);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
capture.set(CV_CAP_PROP_FRAME_WIDTH, 640);
cout << "Vidoe opened" << endl;
if (!src.data) {
std::cout << "ERROR:\topening image" << std::endl;
return -1;
}
cv::namedWindow("image1", CV_WINDOW_AUTOSIZE);
cv::namedWindow("image2", CV_WINDOW_AUTOSIZE);
while (true){
capture.read(src);
Mat dst;
int iBrightness = iSliderValue1 - 50;
double dContrast = iSliderValue2 / 50.0;
src.convertTo(src, -1, dContrast, iBrightness);
cout << "1" << endl;
cv::imshow("image1", src);
Mat src_gray2;
cvtColor(src, src_gray2, CV_BGR2GRAY);
GaussianBlur(src_gray2, src_gray2, cv::Size(9, 9), 2, 2);
vector<Vec3f> circles;
cout << "2" << endl;
double dparam1 = param1 / 1.0;
double dparam2 = param2 / 1.0;
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
0, 0); // min and max radius
cout << "3" << endl;
std::cout << circles.size() << std::endl;
std::cout << "end of test" << std::endl;
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);
}
/*std::vector<cv::Vec3f>::
const_iterator itc = circles.begin();
while (itc != circles.end()) {
cv::circle(src_gray2,
cv::Point((*itc)[0], (*itc)[1]), // circle centre
(*itc)[2], // circle radius
cv::Scalar(0,0,0), // color
2); // thickness
++itc;
}*/
cv::imshow("image2", src_gray2);
cout << "5" << endl;
cvWaitKey(33);
}
return 0;
}
#包括
#包括
#包括
#包括
#包括“opencv2/highgui/highgui.hpp”
#包括“opencv2/imgproc/imgproc.hpp”
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
int main(int argc,字符**argv){
//为轨迹栏创建一个窗口
namedWindow(“轨迹栏窗口”,CV\u窗口\u自动调整大小);
//创建轨迹栏以更改亮度
int iSliderValue1=50;
createTrackbar(“亮度”、“轨迹栏窗口”和IsliderValue1100);
//创建轨迹栏以更改对比度
int iSliderValue2=50;
createTrackbar(“对比度”、“轨迹栏窗口”和IsliderValue2100);
int参数1=10;
createTrackbar(“参数1”、“轨迹栏窗口”和参数1300);
int参数2=10;
createTrackbar(“param2”、“Trackbar窗口”和Param2300);
cout我认为houghCircle没有问题……但是您选择的参数给出了许多需要时间处理的圆,这就是为什么它要等待10秒才能转到3。尝试增加参数2,它会给出更快的结果
在这里,我用Python中的图像进行了尝试,您可以尝试从中进行移植
import cv2
import numpy as np
img = cv2.imread('C:/Python34/images/2.jpg',0)
cv2.namedWindow('image')
def nothing(x):
pass
cv2.createTrackbar('Param 1','image',0,100,nothing)
cv2.createTrackbar('Param 2','image',0,100,nothing)
switch = '0 : OFF \n1 : ON'
cv2.createTrackbar(switch, 'image',0,1,nothing)
while(1):
cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
#To Get Parameter values from Trackbar Values
para1 = cv2.getTrackbarPos('Param 1','image')
para2 = cv2.getTrackbarPos('Param 2','image')
s = cv2.getTrackbarPos(switch,'image')
if s == 0:
cv2.imshow('image', img)
else:
#For finding Hough Circles according to trackbar parameters
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,20,para1,para2,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
#For drawing Hough Circles
for i in circles[0,:]:
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
cv2.circle(img,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('image', img)
cv2.waitKey(0)
img = cv2.imread('C:/Python34/images/2.jpg',0)
cv2.destroyAllWindows()
您可以使用上面的代码作为参考,首先它为开关创建一个窗口和轨迹栏,为hough circle创建两个参数。
然后在while循环中,para1和para2将轨迹栏的位置存储为canny参数的值。
然后在cv2.HoughCircles函数中使用该函数并绘制圆。
图像会再次加载,这样每次更改参数时,输出都会显示在新图像上,以避免混淆
希望这可能有用。您错过了一个库math.h
,请在标题中添加#include