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
var pic = getPhoto();
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 showfunction.
// take a frame in rgb32 formatvar pic =getPhoto();brick.display().show(pic, total_height, total_width,"rgb32");script.wait(2000);
# 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
functiongrayScale(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 componentvar g = (p &0xff00) >>8; // take the value of the green componentvar 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;}
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:
var pic =grayScale(pic);brick.display().show(pic, total_height, total_width,"grayscale8");script.wait(2000);
// Binarization - converting the image to black and whitefunctionbinarization(treshold, sPic){ bufPic = []for(var i =0; i <sPic.length; i++){ bufPic[i] = sPic[i] > treshold ?255:0; }return bufPic;}
# 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:
var pic =binarization(80, pic);brick.display().show(pic, total_height, total_width,"grayscale8");script.wait(2000);