gpt4 book ai didi

c++ - opencv HoughCircle 和 trackbar

转载 作者:行者123 更新时间:2023-11-28 06:04:38 26 4
gpt4 key购买 nike

我正在研究 Hough Circle 函数。基本上我可以使用 4 个参数来获得我想要的正确圆。

所以我想到要创建一个跟踪条来监视正在处理的图像的状态。

所以我这样修改了我的代码

#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", &param1, 300);

int param2 = 10;
createTrackbar("param2", "Trackbar Window", &param2, 300);



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);


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);

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;

HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT,
2, // accumulator resolution (size of the image / 2)
5, // minimum distance between two circles
param1, // Canny high threshold
param2, // minimum number of votes
0, 0); // min and max radius

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);

cvWaitKey(33);
}
return 0;
}

如 Hough Circle 函数所示,我使用 int param1; 作为我希望更改的值。但是,代码没有语法错误,但无法编译。我想知道我的轨迹栏设置是否有问题..谢谢

最佳答案

我在这里使用 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()

您可以使用上面的代码作为引用,首先它为开关创建一个窗口和轨迹栏,为霍夫圆创建两个参数。然后在 while 循环中,para1 和 para2 会将轨迹栏的位置存储为 canny 参数的值。然后在 cv2.HoughCircles 函数中使用它并绘制圆圈。再次加载图像,以便每次更改参数时都会在新图像上给出输出以避免混淆。

希望这可能有用。

关于c++ - opencv HoughCircle 和 trackbar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32647897/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com