Getting an image into octave from a local camera

The image-acquisition package of octave-forge will be used for this purpose. So, first of all, you need to install and load this package in octave. You also need to install build dependencies for the package, these are – ‘libv4l-dev’ and ‘libfltk1.3-dev’ or ‘libfltk1.1-dev’.

octave> pkg install -forge image-acquisition
octave:> pkg load image-acquisition

After loading this package, you can further proceed to capture an image from web cam or from any other connected camera.

The function below will open the v4l2 device and output the result-

octave:> obj = videoinput("v4l2", "/dev/video0")
obj = videoinput for v4l2
     device               = /dev/video0
     driver               = uvcvideo
     card                 = UVC Camera (046d:0825)
     VideoInput           = 0
     VideoResolution      = 320 x 240 px
     VideoFormat          = YUYV

Now, follow the given steps-

>> get(obj);  // To check properties available for used device
>> set(obj, "VideoFormat", "RGB3");  // To convert video format from RGB24 to RGB3
>> set(obj, "VideoResolution", [320 240]);  // To get the list of available video resolutions
>> preview(obj)  // To start preview
Hit CTRL+C to exit


>> set(obj, "VideoResolution", [640 480]); // Use higher resolution
>> start(obj, 2) // Start streaming with two buffers
>> img = getsnapshot(obj); // To get an image from the buffers, view and save it
>> image(img) // To show
>> img = getsnapshot(obj);
>> image(img)
>> imwrite(img, "e.png") // To write
   warning: your version of GraphicsMagick limits images to 16 bits per pixel
   warning: called from
   imformats>default_formats at line 256 column 11
   imformats at line 79 column 3
   imwrite at line 99 column 7
>> [img, seq, t] = getsnapshot(obj);
>> seq
   seq = 178

>> t
   t = 
      scalar structure containing the fields:
      tv_sec = 56491 tv_usec = 320175

>> stop(obj) // To stop streaming

We can also set the brightness of video. Use the following to do so-

octave:> get(obj, "brightness")  //To get the current brightness value
ans =  100

octave:> set(obj, "brightness")  //Query possible range for brightness
ans =
 scalar structure containing the fields:
   min = 0
   max =  255
   step =  1
   default =  128

octave:> set(obj, "brightness", 100)  //Set a new value for brightness



You can perform many other image-processing functions on this too.

Convert an integer to a string

We have function in octave to convert an integer or an array of integer to a string or a character array. This function is ‘int2str()’.



>> s = int2str([1,2,3;4,5,6])

=> s = 1 2 3

4 5 6

>> whos s

=> gives the output as below

attr name = s, size = 2x7, bytes = 14 and class = char

This function is not very flexible. For better control over the results, use sprintf().

Converting an image of type uint8 to double

In order to change the class of image, you can use the following functions-

>> im = imread('a.jpg');  // To read the image

>> imshow(im);  // To display the image

>> whos im;  // To get info. about image size, type etc., class of im is uint8 here

>> imd = im2double(im);   // Type conversion

>> imshow(imd);  // To display second type image

>> whos imd;   // It will display class type double

Class of an image is changed. It looks exactly as the other one. When we see the type, it is double and pixel value are floating point numbers now. In th convention ‘0’ represents black and ‘1’ represents white. So, this is the another way to represent each pixel of an image i.e in double precision.

Pixel Data Types

In order to get information about the type of variable/image or any object, use the following function-

>> typeinfo(variable_name);

and in order to know class, you can use-

>> class(variable_name);

To display the values of variable, use-

>> display(variable_name);

The uint8 data types have very limited dynamic range i.e from 0 to 255. So, whenever large any arithmetic operation is performed, it limits its result between 0 to 255. So, what it performs is clipping and rounding, which is collectively known as saturation. You can easily understand this with the help of following example:

When we have data type ‘double’:-

1So, here it is giving the actual result. The exponent range of double is -1022 to 1021 and it occupies 8 times more memory than occupied by uint8 type. Let’s see what happens then if we use uint8 type of data and performed arithmetic operations like in previous case-

2So, here you can see the difference. Addition of variable gives the result 255 rather than giving 300 because uint8 type limits to 255 maximum value only. Similarly, in case of subtraction, instead of giving negative value it returns the lowest possible unsigned integer value i.e 0. Here, clipping is performed. Now, in case of division, the result is rounded of to 1 from 0.5 as per the common rounding off rule.

Solving linear equation in octave

Create a matrix for linear equations :

2X+4Y+5Z=1, 3X+6Y+7Z=2 and 4X+7Y+8Z=3

>> A = [2 4 5; 3 6 7; 4 7 8]     // Enter values of equations in a matrix form
A =

2   4   5
3   6   7
4   7   8

>> B = [1 2 3]     // Create second matrix of constant values
B =

1   2   3

>> B’      // Transpose to convert row matrix to column matrix
ans =


>> B”    // Double transpose, to convert transposed matrix again to its original form
ans =

1   2   3

>> X = A/B    // To find the values of x, y and z
X =



Simple Arithmetic Operations in octave

I am working on octave-4.0.0 GUI version. Here, we can use simple arithmetic operations on octave

>> a = 20;
>> b = 30;
>> ans = a+b;  // adding two numbers
>> ans
ans =  50

>> ans2 = a*b; // Multiplication of two numbers
>> ans2
ans2 =  600

>> ans3 = a/b; // Division
>> ans3
ans3 =  0.66667

>> a = [1 2;3 4]  // Displaying a matrix
a =

1   2
3   4

>> d = 89  // Assigning value to a variable
d =  89

>> d*2  // Scalar multiplication

ans =  178

>> d/2   // Scalar Division
ans =  44.500

>> [1 2 3 4] // Simple row matrix
ans =

1   2   3   4



Octave will use GraphicsMagick for reading and writing your images (not plots, only if you are actually doing image processing). This means that the GraphicsMagick configuration you have on you system will limit what your Octave installation can do. Most systems will have it on their own repositories. Unfortunately, their configuration is not always the best for image analysis as it limits reading of images to 8-bit.

To solve the problem, GraphicsMagick needs to be rebuilt with the appropriate settings. See below for tracking dependencies and the reasoning behind each flag. Once done, the following commands should work to build GraphicsMagick.

./configure  --with-quantum-depth=16 --enable-shared --disable-static --with-magick-plus-plus=yes
make check
sudo make install

Source –

14 Bugs on octave-4.0.0

After compilingh, command used was:

make check

At the end got this-


PASS     12941
FAIL         0
XFAIL       14
SKIPPED      1

See the file /tmp/octave-4.0.0/test/fntests.log for additional details.

Expected failures (listed as XFAIL above) are known bugs. Please help improve Octave by contributing fixes for them.

Tests are most often skipped because the features they require have been  disabled.  Features are most often disabled because they require dependencies  that were not present when Octave was built.  The configure script should have  printed a summary at the end of its run indicating which dependencies were not  found.

207 (of 874) .m files have no tests.

Please help improve Octave by contributing tests for these files
(see the list in the file /tmp/octave-4.0.0/test/fntests.log).

Open to contribute. Well! I continued to sudo make install.

Getting an image into the octave

In order to load the image into octave, use ‘imread’ function. For example-

>> I = imread('Desktop/a.jpg');
>> imshow(I);

imshow(I) will display the image. You can also use image() instead of imshow().

>> I                // To display image matrix
>> whos I;          // To display basic information about image

It will display the following-
Variable in the current scope:-
Attr Name Size Bytes Class
I 604x604x3 1094448 uint8
Total is 1094448 elements using 1094448 bytes.

New octave is similar to MATLAB. It provides GUI and in workspace you can see the properties of image, you just added.

You can also pass address of the image as an argument to imread function.

>> filename = ('path');
>> A = imread(filename);
>> image(A);

In order to read the meta data of an image, use function imfinfo().

>> info = imfinfo(filename);

This will display the complete data about the image as given below:

scalar structure containing the fields:-
filename = a
FileModDate =
File Size =

In order to get pixel’s intensity value at any particular point of u and v coordinate, use the following:

>> A(131,154);
ans = 4

That’s all.