我正在嘗試制作一個檢測數獨網格的代碼。這意味著我必須在影像上應用大量過濾器,然后在 Sobel 演算法的輸出之后對其應用霍夫變換演算法(這使得邊緣在黑色影像上顯示為白色像素)。
我有點理解它的概念以及為什么使用“Rho = x cos(Theta) y sin(Theta)”來獲得引數空間中的邊坐標很重要。
但是,我遇到了 Rho 和我的累加器陣列的問題。到目前為止,這是我的代碼:
void hough(SDL_Surface* image_surface)
{
unsigned int width = image_surface->w;
unsigned int height = image_surface->h;
unsigned int Rhos, Thetas;
Rhos = sqrt(width * width height * height);
Thetas = 180;
//initialise accumulator array
unsigned int acc_array[Rhos][Thetas];
for (size_t i = 0; i < Rhos; i )
{
for (size_t j = 0; j < Thetas; j )
acc_array[i][j] = 0;
}
Uint32 pixel;
Uint8 r, g, b;
//go through each pixels
for (size_t x = 0; x < width; x )
{
for (size_t y = 0; y < height; y )
{
pixel = get_pixel(image_surface, x, y);
SDL_GetRGB(pixel, image_surface->format, &r, &g, &b);
//if white
if (r g b == 765)
{
//p = x*cos(t) y*sin(t)
//check for every t
for (int t = 0; t < 180;t )
{
unsigned int p = x*cos(t) y*sin(t);
acc_array[p][t] ;
}
}
}
}
//rest of the code below...
我的問題是,例如,當我在影像的像素 (20, 1882) 上并且我的 theta(或 t)= 4 時,p(或 Rho)變為 -1437。如果 Rho 為負,則我無法增加它在累加器陣列中的位置,因為索引不能為負。
誰能幫我解決這個問題?
uj5u.com熱心網友回復:
cos() 和 sin() 函式期望角度為弧度。根據您的 for 回圈范圍,它看起來 t 是以度為單位的值。
uj5u.com熱心網友回復:
您不需要經過 180 度,只需 90 度。負值來自 thetas > 90。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/331903.html
下一篇:C中的memchr如何實際作業?
