C++ 在hough变换中如何处理负rho值?
以下是我为图像中的行创建C++ 在hough变换中如何处理负rho值?,c++,opencv,computer-vision,C++,Opencv,Computer Vision,以下是我为图像中的行创建hough累加器的代码: void hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc) { for (size_t r = 0; r < img_a_edges.rows; r++) { for (size_t c = 0; c < img_a_edges.cols; c++) { int thet
hough累加器的代码:
void hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc) {
for (size_t r = 0; r < img_a_edges.rows; r++) {
for (size_t c = 0; c < img_a_edges.cols; c++) {
int theta = static_cast<int> (std::atan2(r, c) * 180 / M_PI);
int rho = static_cast<int> ((c * cos(theta)) + (r * sin(theta)));
if (theta < -90) theta = -90;
if (theta > 89) theta = 89;
++hough_acc[abs(rho)][theta];
}
}
cv::Mat img_mat(hough_acc.size(), hough_acc[0].size(), CV_8U);
std::cout << hough_acc.size() << " " << hough_acc[0].size() << std::endl;
for (size_t i = 0; i < hough_acc.size(); i++) {
for (size_t j = 0; j < hough_acc[0].size(); j++) {
img_mat.at<int> (i,j) = hough_acc[i][j];
}
}
imwrite("../output/ps1-2-b-1.png", img_mat);
}
输出仍然不正确。这里的错误是什么?两个正数组成的2。。。不应该给你负角度,它应该只给你一个0-90的范围
同样对于hough变换,我认为您需要与一个点相关的所有内容(即本例中的0,0)。我认为,对于这一点,你实际上想要使θ=90-atan2(r,c)
不过,无可否认,我有点困惑,因为我认为必须对线方向进行编码,而不仅仅是“edge pt”。也就是说,我认为在每一个边缘点,你必须提供一个离散的猜测线轨迹数组,并计算每一个的ρ和θ,然后把所有这些都放到你的累加器中。事实上。。。我不知道你在计算什么。我想我对算法不是很了解:/在课程中,教授说d=x*cos(θ)-y*sin(θ)
作业说rho=x*cos(θ)+y*sin(θ)
。有什么区别?从语言上看,d和rho似乎与prof将hough acc映射为H[d,θ]相同。此外,我打算在算法中包括一个选项,其中θ允许值为b/w-90和89。我们正在计算θ。我怎么允许这样?这就是它所说的:指定了一个可选参数(θ=整数-90到89,即180个值,包括0)
。哦!我想我明白了!你们不想从任何东西计算θ,这将是我提到的离散列表。创建一个介于-90和90之间的值数组(以30度为增量)。然后,对于数组中的每个边点,计算每个θ的ρ,并将每个θ加到累加器中。-------为了更好地理解这个算法():想象一下,在每一点上,你都在生成一个星型的线条(猜测)。线性相关的点应该添加到相同的猜测中,从而创建峰值!您的函数参数可能会指定猜测范围(-90到90)或更多或更少,或者指定增量步长(30度)。这将允许在你的ρ-θ空间中提高分辨率。d和ρ是一样的。基本上,菱形-θ组合是图像中唯一可能的线条。所以,如果你画一幅图像中的某条线,θ将是这条线和x轴之间的角度(相交处左上角)。现在的问题是有很多可能的线具有相同的角度,所以ρ是从该线到原点的垂直距离(即你可以认为它是从该线到原点的最短距离)。对于这两个值,行描述符现在是唯一的
bool hough_lines_acc(cv::Mat img_a_edges, std::vector<std::vector<int> > &hough_acc,\
std::vector<double> thetas, std::vector<double> rhos, int rho_resolution, int theta_resolution) {
int img_w = img_a_edges.cols;
int img_h = img_a_edges.rows;
int max_votes = 0;
int min_votes = INT_MAX;
for (size_t r = 0; r < img_h; r++) {
for (size_t c = 0; c < img_w; c++) {
if(img_a_edges.at<int>(r, c) == 255) {
for (size_t i = 0; i < thetas.size(); i++) {
thetas[i] = (thetas[i] * M_PI / 180);
double rho = ( (c * cos(thetas[i])) + (r * sin(thetas[i])) );
int buff = ++hough_acc[static_cast<int>(abs(rho))][static_cast<int>(i)];
if (buff > max_votes) {
max_votes = buff;
}
if (buff < min_votes) {
min_votes = buff;
}
}
}
}
}
double div = static_cast<double>(max_votes) / 255;
int threshold = 10;
int possible_edge = round(static_cast<double>(max_votes) / div) - threshold;
props({
{"max votes", max_votes},
{"min votes", min_votes},
{"scale", div}
});
// needed for scaling intensity for contrast
// not sure if I am doing it correctly
for (size_t r = 0; r < hough_acc.size(); r++) {
for (size_t c = 0; c < hough_acc[0].size(); c++) {
double val = hough_acc[r][c] / div;
if (val < 0) {
val = 0;
}
hough_acc[r][c] = static_cast<int>(val);
}
}
cv::Mat img_mat = cv::Mat(hough_acc.size(), hough_acc[0].size(), CV_8UC1, cv::Scalar(0));
for (size_t i = 0; i < hough_acc.size(); i++) {
for (size_t j = 0; j < hough_acc[0].size(); j++) {
img_mat.at<uint8_t> (i,j) = static_cast<uint8_t>(hough_acc[i][j]);
}
}
imwrite("../output/ps1-2-b-1.png", img_mat);
return true;
}