I'm trying to determine if portion of the picture contains red-white striped object (liftramp). If it is present, it looks like this: , and when not like this:
The naive approach was to extract histogram, and count if there is more red pixels than blue/green ones:
use Image::Magick;
my $image = Image::Magick->new;
my $rv = $image->Read($picture);
#$rv = $image->Crop(geometry=>'26x100+484+40');
my @hist_data = $image->Histogram;
my @hist_entries;
# Histogram returns data as a single list, but the list is actually groups of 5 elements. Turn it into a list of useful hashes.
while (@hist_data) {
my ($r, $g, $b, $a, $count) = splice @hist_data, 0, 5;
push @hist_entries, { r => $r, g => $g, b => $b, alpha => $a, count => $count };
}
my $total=0;
foreach my $v (@hist_entries) {
if ($$v{r}>($$v{g}+$$v{b})) { $total +=$$v{count}; }
}
and then comparing if $total > 10
(arbitrary threshold). While that seems to work nice for relatively sunny day (giving 50-180 for presence vs 0-2 for not present), heavy clouds and dusk make the detection always say the liftramp is not present.
I guess there must be smarter way to detect if red-white object is present. So the question is how to do that detection more reliably?
Note that grayish/green background might change with seasons to more of gray-brown or something. I also cannot count on pixel precision as it might move a little (or I'd just crop a 3-4 pixels and look if they are red) - but it should mostly fit it he cropped box.