Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Abstract image registration or feature detection

by vr (Curate)
on Jul 03, 2022 at 23:39 UTC ( [id://11145258]=note: print w/replies, xml ) Need Help??


in reply to Abstract image registration or feature detection [UPDATED w examples]

Task as stated looks pretty much as case for interpolation to me. One trick pony, etc., but I even found my answer (Re: which function in PDL can do the same thing as matlab pcolor?) through SS for "Delaunay", see links in last paragraph there, especially ugly formulae for barycentric weights (didn't find ready-made solution on CPAN). Code is adjusted version of what I did a few years ago for some image hacking, unrelated to feature recognition.

No idea what data pryrt used for a sample (same "red-blue" terms used here), and whether they are supposed to represent a trivial demo, or, indeed, an "interesting" non-affine distortion was applied. Anyway, results below seem to match well with his, even if it's just a hack -- X and Y of target are treated as separate functions to interpolate, with, furthermore, simple linear interpolation.

use strict; use warnings; use feature qw/ say /; use POSIX qw/ round /; # use 5.022; use List::Util qw/ sum /; use Math::Geometry::Delaunay qw/ TRI_CONSTRAINED /; use constant EPSILON_NEGATIVE => -1e-6; sub key { pack 'II', @{ $_[ 0 ]}} my @red_in = ( [58,48], [108,155], [186,80], [255,191], [331,48] ); my @red_out = ( [471,15], [531,141], [603,90], [682,227], [747,107] ); my %mapped = map { key( $red_in[ $_ ]), $red_out[ $_ ]} 0 .. $#red_in +; my $tri = Math::Geometry::Delaunay-> new( TRI_CONSTRAINED ); $tri-> addPoints( \@red_in ); $tri-> triangulate; my @blue_in = ( [125,73], [197,158], [282,94] ); BLUE: for my $blue ( @blue_in ) { TRI: for my $elem ( @{ $tri-> elements }) { my $y23 = $elem-> [ 1 ][ 1 ] - $elem-> [ 2 ][ 1 ]; my $x32 = $elem-> [ 2 ][ 0 ] - $elem-> [ 1 ][ 0 ]; my $x13 = $elem-> [ 0 ][ 0 ] - $elem-> [ 2 ][ 0 ]; my $y13 = $elem-> [ 0 ][ 1 ] - $elem-> [ 2 ][ 1 ]; my $denominator = $y23 * $x13 + $x32 * $y13; my $xx3 = $blue-> [ 0 ] - $elem-> [ 2 ][ 0 ]; my $yy3 = $blue-> [ 1 ] - $elem-> [ 2 ][ 1 ]; my @weights; next TRI if EPSILON_NEGATIVE > ( $weights[ 0 ] = ( $y23 * $xx3 + $x32 * $yy3 ) / $denominator ); next TRI if EPSILON_NEGATIVE > ( $weights[ 1 ] = ( -$y13 * $xx3 + $x13 * $yy3 ) / $denominator ); next TRI if EPSILON_NEGATIVE > ( $weights[ 2 ] = 1 - $weights[ 0 ] - $weights[ 1 ]); printf "[%3d,%3d] => [%3d,%3d]\n", @$blue, map { my $coord = $_; round sum map { $weights[ $_ ] * $mapped{ key $elem-> [ $_ ]}[ $co +ord ] } 0 .. 2 # 3 vertices } 0, 1; # x, y next BLUE } die "point @$blue outside convex hull" } __END__ [125, 73] => [541, 63] [197,158] => [621,174] [282, 94] => [701,137]

Replies are listed 'Best First'.
Re^2: Abstract image registration or feature detection
by pryrt (Abbot) on Jul 04, 2022 at 01:14 UTC
Re^2: Abstract image registration or feature detection
by kikuchiyo (Hermit) on Jul 04, 2022 at 18:24 UTC

    Thanks for this!

    I've updated the main node with real-ish example data, and while this algorithm is not perfect (it gives errors up to 7 pixels for some points), it might be good enough for my use case.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145258]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-03-29 00:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found