は、画像内の線のためhough accumulator
を作成するための私のコードです:ハフ変換での負のrho値はどうすればよいですか?ここ
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);
}
theta
が-90 to 89
によって異なります。私は負のrho値を得ています。今私は否定的な人を正の人で置き換えるだけですが、正解を得ていません。否定的なρに私は何をするのですか?答えを説明してください。
シータ=アークタンジェント(Y/X)
ロー= X * COS(シータ)の+ Y *罪(シータ)
編集コード:
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;
}
まだ、正しい出力。ここのエラーは何ですか? 2つの正の数の
私は本当によく分かっているとは思っていませんが、教授は「d = x * cos(theta) - y * sin(theta)」と言っていて、「rho = x * cos θ)+ y * sin(θ) 'となる。違いは何ですか?言語によってそれはdのように見え、rhoはprofと同じであるが、houghをH [d、θ]としてマッピングする。 –
また、私はthetaに値がb/w -90と89の値が許されるオプションをalgoに含めるつもりです。我々はthetaを計算しています。これをどうすればいいですか?これは、「θ=整数90〜89、すなわち0を含む180の値 」と指定された1つのオプションのパラメータを持つことです。 –
OH!分かったと思います!あなたは、私が言及したあなたの離散的なリストになる何かからシータを計算したくありません。 -90と90の間の値の配列を作る(例えば、30度の増分)。次に、配列の各エッジポイントについて、各シータのrhoを計算し、それぞれをアキュムレータに追加します。 ------- algoをもっとよく理解するには(https://en.wikipedia.org/wiki/Hough_transform):あらゆる点で線の星パターン(推測)を生成していると想像してください。直線的に関連するポイントは、同じ推測に追加してピークを作成する必要があります。 –