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

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

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

Rwsult:

Implementing the grayscale function

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;
}

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);

Result:

Implementing the image binarization function

// 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;
}

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);

Result:

Last updated