PDA

View Full Version : A kind of Hough Transform



__bob__
02-25-2016, 09:44 AM
Hi all!

Starting from 2d lines as vec3(a,b,c) with ax+by+c=0 in cartesian coordinates and (a,b) normalized , i need to compute the rho theta representation of Hough transform vec2(rho, theta) in polar representation theta in [0,PI/2].
I also need the reverse operation.

To begin i write few function to test this in shader toy :


vec4 DrawCartesian(in vec2 uv, in vec3 dr)//in put uv and line
{
if(abs(dot(uv,dr.xy)+dr.z)<0.005) return vec4(1.,0,0.,1.);
return vec4(0.0);
}

vec4 DrawPolarline(vec2 uv, float r, float t)
{
vec2 er = vec2(cos(t),sin(t));
float ui= dot(uv.xy,er)-r;
if(abs(ui)<0.005) return vec4(0,0.0,1.0,0.0);
return vec4(0.0);
}


and 2 functions to try the conversion :


float linePtDist(vec2 pt,vec3 dr) //to compute rho
{
vec2 p = vec2(0,dr.z);
float t = dot(dr.xy,(pt-p))/dot(dr.xy,dr.xy);
return length(pt-p-t*(dr.xy));
}
vec2 drToRo(vec3 dr,vec2 uv) //convert cartesian to rho/theta
{

float dist = linePtDist(vec2(0),dr);
return vec2(dr.z,atan(dr.y/dr.x));
}


Where is the bug? and how to reverse.

Thanks!

https://www.shadertoy.com/view/XsG3DK

GClements
02-25-2016, 04:30 PM
The cartesian representation is homogeneous, i.e. multiplying all three components by the same factor results in the same line. Even if you require the direction to be normalised, that still allows for a scale factor of -1.

If dr.xy is normalised and dr.z is negative, then rho is just -dr.z.

Also, drToRo() is using the one-argument form of atan() whereas it should be using the two-argument form to get the correct quadrant when the x component is negative.

In short:


vec2 cartesian_to_polar(vec3 dr)
{
if (dr.z > 0)
dr = -dr;
float theta = atan(dr.y, dr.x);
float rho = -dr.z / length(dr.xy);
return vec2(rho, theta);
}

vec3 polar_to_cartesian(vec2 rt)
{
return vec3(cos(rt.y), sin(rt.y), -rt.z);
}

__bob__
02-26-2016, 03:13 AM
Thanks!

Maybe an little error :

vec3 polar_to_cartesian(vec2 rt)
{
return vec3(cos(rt.y), sin(rt.y), -rt.x);
}