Skip to content

Instantly share code, notes, and snippets.

@wopfel
Created November 27, 2018 19:45
Show Gist options
  • Save wopfel/7ecdbdb12b3b5cf2ecd3266d0e5a7f83 to your computer and use it in GitHub Desktop.
Save wopfel/7ecdbdb12b3b5cf2ecd3266d0e5a7f83 to your computer and use it in GitHub Desktop.
Detect LED state from image created by Raspberry Pi (raspistill)
<?php
#
# PiCam is placed on Buderus control panel
# /opt/vc/bin/raspistill -ISO 200 -t 3000 -rot 270 -awb shade -o /tmp/test.jpg
#
# Sorry, this may be a poor description
#
$img = imagecreatefromjpeg( "/tmp/test.jpg" );
$size_x = imagesx($img);
$size_y = imagesy($img);
print "Original: $size_x x $size_y\n";
# Ausgabe von einem raspistill-Bild:
# 3280x2464
if ( $size_x != 3280 or $size_y != 2464 ) {
print( "Fehler: Falsche Abmessungen! Abbruch." );
exit(1);
}
$percent = 0.25;
$new_width = $size_x * $percent;
$new_height = $size_y * $percent;
print "Verkleinert: $new_width x $new_height\n";
$image_p = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_p, $img, 0, 0, 0, 0, $new_width, $new_height, $size_x, $size_y);
#######
function pixelsAboveThreshold( $spot_x, $spot_y, $image, $threshold ) {
$pixels_on = 0;
$range = 10;
for ( $x = $spot_x-$range; $x < $spot_x+$range; $x++ ) {
for ( $y = $spot_y-$range; $y < $spot_y+$range; $y++ ) {
$rgb = imagecolorat( $image, $x, $y );
$r = ($rgb >> 16) & 0xFF;
#$g = ($rgb >> 8) & 0xFF;
#$b = $rgb & 0xFF;
if ($r>$threshold) {
$pixels_on++;
}
}
}
return $pixels_on;
}
# Links oben noch nicht getestet, weil noch kein Sommerbetrieb war
print "Pixel links oben ";
print pixelsAboveThreshold( 152, 153, $image_p, 150 );
print "\n";
# Die anderen drei sind geprueft und passen
print "Pixel rechts oben ";
print pixelsAboveThreshold( 686, 153, $image_p, 150 );
print "\n";
print "Pixel links unten ";
print pixelsAboveThreshold( 85, 455, $image_p, 130 );
print "\n";
print "Pixel rechts unten ";
print pixelsAboveThreshold( 680, 435, $image_p, 150 );
print "\n";
exit(0);
$spot_x = 152;
$spot_y = 477;
$pixels_on = 0;
for ( $x = $spot_x-20; $x < $spot_x+20; $x++ ) {
for ( $y = $spot_y-20; $y < $spot_y+20; $y++ ) {
$rgb = imagecolorat( $image_p, $x, $y );
$r = ($rgb >> 16) & 0xFF;
#$g = ($rgb >> 8) & 0xFF;
#$b = $rgb & 0xFF;
if ($r>200) {
$pixels_on++;
}
}
}
print "Pixel an: $pixels_on\n";
#######
$spot_x = 755;
$spot_y = 187;
$pixels_on = 0;
for ( $x = $spot_x-20; $x < $spot_x+20; $x++ ) {
for ( $y = $spot_y-20; $y < $spot_y+20; $y++ ) {
$rgb = imagecolorat( $image_p, $x, $y );
$r = ($rgb >> 16) & 0xFF;
#$g = ($rgb >> 8) & 0xFF;
#$b = $rgb & 0xFF;
if ($r>100) {
$pixels_on++;
}
}
}
print "Pixel an: $pixels_on\n";
exit(0);
#####
for ( $x = 0; $x < $new_width; $x++ ) {
for ( $y = 0; $y < $new_height; $y++ ) {
$rgb = imagecolorat( $image_p, $x, $y );
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
if ($r>100 and $r<190) {
#print("$x:$y ");
$line[$y]++;
if ($y==187) { print("<$x>"); }
}
#if ($r>3) { var_dump($r, $g, $b); }
}
}
var_dump($line);
# Rechte obere Ecke pruefen
#for ( $x = 0; $x < 3280; $x++ ) {
# for ( $y = 2464-1; $y > 0; $y-- ) {
# $rgb = imagecolorat( $img, $x, $y );
# $r = ($rgb >> 16) & 0xFF;
# $g = ($rgb >> 8) & 0xFF;
# $b = $rgb & 0xFF;
# if ($r>220) {
# #print("$x:$y ");
# $line[$y]++;
# }
# #if ($r>3) { var_dump($r, $g, $b); }
# }
#}
#var_dump($line);
#for ( $x = floor(3280*0.8); $x < 3280; $x++ ) {
# for ( $y = floor(2464*0.8); $y > floor(2464*0.6); $y-- ) {
# $rgb = imagecolorat( $img, $x, $y );
# $r = ($rgb >> 16) & 0xFF;
# $g = ($rgb >> 8) & 0xFF;
# $b = $rgb & 0xFF;
# if ($r>3) { var_dump($r, $g, $b); }
# }
#}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment