Function getPhoto

This article is automatically translated from Russian by Google Translator.

Function description

The function returns a one-dimensional array of bytes containing the pixels of an rgb32-formatted image taken from the camera (image size 160x120). This function can be used to solve marker recognition problems.

Function call

JavaScript
Python
JavaScript
var pic = getPhoto();
Python
pic = getPhoto();

Work example

Let's look at an example of converting a frame taken with a camera to grayscale and black-and-white color formats.

Take a picture and display the result with the show function.

JavaScript
Python
JavaScript
// take a frame in rgb32 format
var pic = getPhoto();
brick.display().show(pic, total_height, total_width, "rgb32");
script.wait(2000);
Python
# take a frame in rgb32 format
pic = getPhoto();
brick.display().show(pic, total_height, total_width, "rgb32");
script.wait(2000);

Rwsult:

Implementing the grayscale function

JavaScript
Python
JavaScript
function grayScale(sPic){
bufPic = [];
for(var i = 0; i < total_height; i++)
for(var j = 0; j < total_width; j++){
var x = i * total_width + j;
var p = sPic[x];
var r = (p & 0xff0000) >> 16; // take the value of the red component
var g = (p & 0xff00) >> 8; // take the value of the green component
var b = (p & 0xff); // take the value of the blue component
p = r * 0.299 + g * 0.587 + b * 0.114; // component Y of YUV
bufPic[x] = p;
}
return bufPic;
}
Python
def grayScale(sPic):
bufPic = [];
for i in range(total_height):
for j in range(total_width):
x = i * total_width + j;
p = sPic[x];
r = (p & 0xff0000) >> 16; # take the value of the red component
g = (p & 0xff00) >> 8; # take the value of the green component
b = (p & 0xff); # take the value of the blue component
p = r * 0.299 + g * 0.587 + b * 0.114; # component Y of YUV
bufPic.append(p);
return bufPic;

Converts the image to black and white and outputs it to the display:

JavaScript
Python
JavaScript
var pic = grayScale(pic);
brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);
Python
pic = grayScale(pic);
brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);

Result:

Implementing the image binarization function

JavaScript
Python
JavaScript
// Binarization - converting the image to black and white
function binarization(treshold, sPic){
bufPic = []
for(var i = 0; i < sPic.length; i++){
bufPic[i] = sPic[i] > treshold ? 255 : 0;
}
return bufPic;
}
Python
# Binarization - converting the image to black and white
def binarization(treshold, sPic):
bufPic = []
for i in range(len(sPic)):
bufPic.append(255 if sPic[i] > treshold else 0);
return bufPic;

Translates the image to black and white and outputs it to the display:

JavaScript
Python
JavaScript
var pic = binarization(80, pic);
brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);
Python
pic = binarization(80, pic);
brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);

Result: