arrow-left

All pages
gitbookPowered by GitBook
1 of 1

Loading...

Function getPhoto

circle-info

This article is automatically translated from Russian by Google Translator.

hashtag
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.

hashtag
Function call

hashtag
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 function.

Rwsult:

hashtag
Implementing the grayscale function

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

Result:

hashtag
Implementing the image binarization function

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

Result:

circle-info

.

var pic = getPhoto();
# take a frame in rgb32 format
pic = getPhoto();
brick.display().show(pic, total_height, total_width, "rgb32");
script.wait(2000);
show
An example of a program using the getPhoto functionarrow-up-right
pic = getPhoto();
// take a frame in rgb32 format
var pic = getPhoto();
brick.display().show(pic, total_height, total_width, "rgb32");
script.wait(2000);
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;
}
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;
var pic = grayScale(pic);

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

brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);
// 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;
}
# 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;
var pic = binarization(80, pic);
brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);
pic = binarization(80, pic);
brick.display().show(pic, total_height, total_width, "grayscale8");
script.wait(2000);