Matrix and Vectors Construction

Return to Matlab Tutorials

In this tutorial you will learn:

  1.  Matrix and vector Construciton.
  2. Zero Matrix
  3. Ones Matrix
  4. Identity Matrix
  5. Linerally spaced vectors
  6. Logarithmically spaced vectors

So lets get started!

Matrices

Matrices in Matlab are one of the prime reasons for user to stick around.  It is called MATrixLABoratory for a reason. Otherwise, there are better alternatives when it comes to other features.

Now, a matrix has rows and columns. Kinda looks like this 😀

So, how do we make this sucker in Matlab?

Like this!

Or, you can go like this to save space:

m = [ 1, 1, 0;0, 1, 0;0, 0, 1];

A few more matrices are:

Column Vector:  m = [1;2;3;4]

Row vector: m = [1,2,3,4]

A 2×3 vector m = [1,2 ; 3,4 ; 5,6]

Special Matrices

I am calling them special matrices because these matrices have a special property which can be exploited for our purpose:

Zero Matrix

These matrices have nothing but zeroes in them. They are declared like:

zeros(20, 7)

Ones Matrix

Contrary to Zero Matrix, Ones matrix have every element set as “1”. These matrices are declared like,

ones(4, 20)

Identity Matrix

An identity matrix has ones in the diagonal while zeroes on every other element. It is made like this:

eye(10)

eye(4, 6)

 

Linearly Spaced Vector

This command creates an evenly spaced vector between two numbers. The format is,

linspace(startingNumber, endingNumber, numberOfPoints)

e.g.

linspace(1, 10, 10)

Logarithmically spaced vectors

This command creates Logarithmically spaced vector between two numbers. The format is,

logspace(RangeStartingfrom 10^THISNUMBER, RangingEndingAt 10^THISNUMBER, numberOfLogarithmicallyPoints)

So, logspace(1, 5, 10); will mean that we wish to create a logarithmically spaced vector of 10 points between 10^1 and 10^5

This just about covers the basics of Matrix and vectors. You have learnt the basics and construction of some of the most commonly used matrices in Matlab. Now, you have enough understanding to proceed towards matrix operations and manipulation.

Return to Matlab Tutorials

Variables, Arithmetic operations and Functions in Matlab

Return to Matlab Tutorials

In this tutorial I will explain to you how to use variables, perform basic arithmetic operations and usage of some common mathematical functions in Matlab. Lets start right away.

Creating a simple variable

Creating a variable is very simple. A variable name, an “=” sign and a value. For example, in command window, you would write,

x = 100

Here, we defined a variable x to have a value of 100.

Basic Arithmetic Operations

Addition, subtraction, multiplication and division is performed by using +, -, *, / operators respectively on a variable. This is shown below:

Powers

We are going to give power to variables now. It is simple like:

>> x = 4

x =4

>> x^1000

ans =Inf

>> x^100

ans =1.6069e+60

Squareroot

Root the numbers back to their squares using:

sqrt(x);

sqrt(16);

Logarithmic Operations

This is done like:

log(105);

log10(105);

log10(pi);

log(-1);

Trigonometric Ratios

You do it like a boss:

boss = pi;

sin(4*boss/3);

tan(boss*boss);

 

There are much more functions to get you started on your work in Matlab. But this is enough for this tutorial as I will slowly be advancing towards more advanced and specialized tutorials in the future.

Do comment if you have questions.

Return to Matlab Tutorials

Matlab Command Windows Basic Operations

Return to Matlab Tutorials

Command line works similar to our normal handheld calculator.  You gave it an Input and it performs operations. Also, you can tell you to perform certain actions!

These are some of the actions that you need to learn because they will be used repeatedly.

One interesting thing to note is that you can chose to include or omit the use of semicolon 

Inclusion of ; will not display the result of command in command window

Omission of ; will surely display the result of command.

Result could be either a variable value, matrix, graph, image or a video.

disp(‘Hello World!’)

You guessed it right! It shows the world famous Hello World of Matlab.

clc

This command clears the command window. It does no harm to the variables and values saved in your workspace. Instead, it simply clears the window for you. Similar to clearing your desk space while your books are safe in the book shelf 😀

Clear

This sucker clears all the variables that you have saved in the workspace. Use carefully as it has messed up many people’s progress.

 Clf

So you have 100 fancy plots made using various scripts and you want to clear the plots? This is where it comes handy. Run clf in command window and all the plots will be cleared.

 close all

This closes all the windows that you have opened using your program in Matlab. For example, it will close all plots, images, graphs, etc.

 

doc <function>

This is the best feature of Matlab. Lets suppose you want to know what the frigging “:”  does in Matlab. Simply type doc : in matlab and it will show you help about it.

% (used for comments)

For example, “% clear all” will  make your program ignore that line of code. Best used using Shortcut CTRL+R.

format short and format long

If you wish to change the command window output to smaller or longer decimal places, you can use this command like this.

 

Return to Matlab Tutorials

LibGDX Android Studio: Download and set up a new LibGDX Project

LibGDX android Setup

Hey fellow developers!

Welcome to the beginning of LibGDX for Android tutorials. In this tutorial, you will learn how to download, setup and create a new LibGDX project in Android Studio.

At the end of this tutorial, you will:

  1. Download LibGDX
  2. Setup LibGDX
  3. Create a new LibGDX project in Android Studio

So lets get Started!


Download LibGDX

First of all, lets download LibGDX. (I am assuming that you already have Android Studio.)

  1. Go to official LibGDX website.
  2. Go to Download
  3. Download (Duh!)
  4. And open the folder

This process is given here

 LibGDX Android Studio - Download

Now, we will setup LibGDX for our project.


Setup LibGDX

Now, we will setup the LibGDX project using the downloaded gdx-setup.java file.

  1. First of all, fill in the basic project information
    1. Run gdx-setup.java
    2. Provide your Project name.
    3. Provide package information according to your settings
    4. Game Class is the java class file name that you will eventually be working with.
    5. Destination is where you will work on your project.
    6. Android SDK is where your Android SDK is installed. Give its path.
  2. Sub-Projects is where you provide the platforms that you wish to support.
    1. Chose Android
      (Only because this is an android based tutorial. But you are not limited to that.)
  3. Extensions is where you tell the setup what extensions to load.
    1. For simplicity, just chose Box2d
      (you can learn more later)
  4. Press Generate
    1. Wait until the download completes.

Here is the screen shot of my completed setup.

This entire process is given below.

 LibGDX Android Studio - Setup

Now, we will open this project in Android Studio


Open in Android Studio

Follow these steps to load the LibGDX project in Android Studio

  1. Open Android Studio
  2. Click Import Project
    • Here is how
  3. Give path to the LibGDX download location (which you provided in setup) and press OK
    • Here is how
  4. Wait till the project is loaded in Android Studio

This entire process is given below:

LibGDX Android Studio - Load project in Android Studio

This is it. You have just loaded LibGDX in Android Studio. Now lets run it in Android Virtual Device 😀


Run LibGDX in Android Virtual Device

Follow these steps to run your new LibGDX project in AVD

  1. Launch your Android Virtual Device (AVD)
  2. Press the Run button in Android Studio
  3. Now, the app will run on your AVD. Done!

This process is given below:

LibGDX Android Studio -Launch app in AVD

This is it. You have just created and run a new LibGDX project in Android Studio.

Lets draw some shapes in the next tutorial.

Return to the LibGDX Tutorials

MATLAB Tutorial 1 – Introduction

Who cares what genre of sorcery MATLAB belongs to, you are here to learn it and this alone proves the fact that you know what it does and doesn’t.  But it won’t hurt to shed some light into its basics:

What is the purpose of MATLAB?

MATLAB stands for Matrix Laboratory. First released on 1984 :S. Runs on Windows, Linux, OS X. Has 32-bit and 64-bit support. The data type used generally is a Matrix. MATLAB helps you to perform numerical computing.

Why learn/use MATLAB?

  1. Research
  2. PhD/MSc/Academic Purpose
  3. Rapid prototyping
  4. Engineering Simulation
  5. Algorithm Development
  6. Analysis of data
  7. Fast Visualization of data
  8. Huge multidimensional arrays
  9. Data Proccessing
  10. Frying the CPU
  11. Image Processing
  12. Cracking a nut open
  13. A SUPERDUPERHIFI Calculator
  14. And much more!!

Why MATLAB hurts?

  1. Devours a large chunk off your computer’s memory.
  2. If you have a slow computer, get ready to face long processing times.
  3. When you perform a large calculation/analysis, MATLAB tends to take maximum performance that Windows can provide it. Very Greedy!

Lets start learning 🙂

Return to Tutorials List

Use OpenCV to show camera on android App with correct orientation

Return to OpenCV 4 Android Tutorials List

In this series of tutorials, you are learning OpenCV for Android using Android Studio. So far, you have made a working OpenCV Android Studio Project containing OpenCV module.

In Here is the overview of what we are going to achieve:

  • Create an OpenCV for Android Project (As in previous tutorial in this series) (Here)
  • Show Camera on the Screen
    1. Edit Manifest
      1. Add permission to allow camera.
      2. Support various screen sizes and adaptability of camera.
      3. Use specific features of the camera.
    2. Add a custom Layout show_camera.xml to display camera in it.
    3. Edit MainActivity_show_camera.java 
      1. Import required android classes
      2.  Import OpenCV classes
      3. Connect to OpenCV manager
      4. Initiate LogCat to log events of out app
      5. SHOW CAMERA ON LAYOUT
      6. Corrected Orientation (portrait)

Trust me, it is very simple to do. Lets do it!


 

Manifest.xml

We need to edit the AndroidManifest.xml file like following:

  1. Allow permission to use camera
  2. Allow camera to optimize to the dimensions of the device in use
  3. Use Front camera and autofocus of camera :S

The Manifest.xml file in its entirety is given here

 

This entire process is given here:

Show camera on android App using OpenCV for Android - Edit Manifest

This is all you need to do in AndroidManifest.xml


Create a new layout (show_camera.xml )

Now in your project, do the following:

  1. Create a new layout called show_camera.xml
  2. Add the following code it to:

    Note that the JavaCameraView has the id of show_camera_activity_java_surface_view

This is given below:

 


MainActivity_show_camera.java

Import the following Android Classes in your MainActivity_show_camera:

Import the following OpenCV classes in your MainActivity_show_camera:

Now, lets work with the MainActivity_show_camera Class 🙂 (Code in the end). I will explain every line of OpenCV code in detail.

  • Implement the OpenCV class CvCameraViewListener2 to allow OpenCV to communicate with android camera functionalities.

  • Declare the following items in the class
  • Now, lets call OpenCV manager to help our app communicate with android phone to make OpenCV work

    • CameraBridgeViewBase mOpenCvCameraView
      This variable acts as a bridge between camera and OpenCV library.
    • BaseLoaderCallback mLoaderCallback = …..
      Well, once OpenCV library is loaded, you may want to perform some actions. For example, displaying a success or failure message.
  • Initiate the LogCat
  • Now, when the activity is created, display the OpenCV camera in the layout. show_camera.xml.
  •  The following three functions handle the events when the app is Paused, Resumed and Closed/Destroyed
  • Now, we will do two main things:
    • Receive Image Data when the camera preview starts on your screen 😀
    • Destroy image data when you stop camera preview on your phone screen
    • Now, this one is interesting! OpenCV orients the camera to left by 90 degrees. So if the app is in portrait more, camera will be in -90 or 270 degrees orientation. We fix that in the next and the most important function. There you go!

The entire code is given below:

The entire process of editing MainActivity_show_camera.java is shown here:

Show camera on android App using OpenCV for Android - MainActivity_show_camera


Note: The camera disorientation problem is a bitch an issue not to be takien lightheartedly. Thanks for noting.

This concludes this tutorial, now that you have created a camera app, it is now time to continue this and perform more Computer Vision! In the next tutorial, I will draw a simple object on the screen. Be sure to check out the list of OpenCV for Android tutorials for latest tutorials.

Thanks for reading, and please share and comment if this helped and to share your ideas and opinions.

Return to OpenCV 4 Android Tutorials List

Update Knoppix 7.6 from Terminal

So you downloaded Knoppix and realized that it is a little bit rusty. There is a way to make it fresh and update all the packages with two simple commands.

First you need to Refresh the packages database. Go to terminal and type the following command and press enter.

 

This will initiate an activity on the terminal that will download the latest packages information. This will take a few minutes. After the activity finishes, you will have the latest packages information. This is time to Upgrade the packages that are already installed in your Knoppix system to the latest versions that are available in online repositories.

Simply, type in the following command in terminal:

 

This will initiate a download activity on your terminal that will download all the upgradable packages from online repositories. This will take longer than the “update” activity.

At the end, you will have the latest packages installed and you will be running smooth!

 

Creating a new OpenCV project in Android Studio

Return to OpenCV for Android Tutorials List

In this tutorial, we will create a new OpenCV project in Android Studio. Since you have everything downloaded already, we are ready to move on. By the end of this tutorial, you will have a new project set up, which you can use to build your projects upon. So lets do some CV!

Note: This tutorial has been tested on Android Studio 1.5 and 2.0

In short, the steps are, (2) Create simple project. (3) Import OpenCV SDK as a Module in Android Studio. (4) Set OpenCV Version. in Project (5) Fix Library association in project. (6) Done! Now actual OpenCV code can be written in future. The longer and detailed guided tutorial is given below.

  1. I beg you to open Android studio.
  2. Create a new project
    • I named it “OpenCV_Test”. (It could be different in the following GIF)
    • Minimum SDK is “API 19: Android 4.4 (KitKat).
    • Default Activity is “Basic Activity” (easily changeable later on)
    • Activity name is “MainActivity” (i.e. default settings)
    • I have shown the entire process in this GIF.
      Android-Studio-Create-New-Project
    • After pressing Finish, Gradle will perform some processes and eventually, you will be presented with your Android project.
    • Now, we can start importing OpenCV into the project.
  3. Import the OpenCV for Android SDK module in Android Studio.
    • To import the OpenCV SDK as a module in your project, go to File>New>Import Module.
    • Then Give path to your OpenCV as ..\OpenCV-android-sdk\sdk\java
    • You can chose to modify your module’s name.
    • Press Next and Finish the Dialogue.
    • The entire process is given here:
      OpenCV-Android-Import-Module-Android-Studio
    • Note that in the end, there are some errors in the code as well as the console. This is because you need to fix the Version number and link the libraries with your own project. This is shown in the next two steps.
  4. In this step, we will fix the Android Project SDK information in the OpenCV build.gradle file.
    • In project explorer, switch to Project view.
    • Go to OpenCVLibrary300 > build.gradle file and open it.
      • I have updated the contents of the file with the following code
      • Note that the android project’s SDK information can be obtained from the project’s own build.gradle file.
    • Once the Android SDK settings are updated, press “Try Again” button.
    • This entire process is given below:
      OpenCV-Android-Update-DefaultProject-SDK-Information
    • Now. we need to fix library association.
  5. Looking good so far! We now need to make sure that the OpenCV for Android libraries are associated with our Android Studio project. Currently, Android Studio will not recognize the OpenCV code that we type in our files. We fix this as follows.
    • Go to File>Project Structure.
    • When window opens, under Modules, select app.
    • Click Dependencies.
    • Click +.
    • Select Module Dependencies.
    • (In my case) Select :openCVLibrary300.
    • Press Ok and Ok.
    • After a few seconds, the libraries will be associated with your Android project.
    • This entire process is shown here:
      OpenCV-SDK-library-association
    • You can now write more OpenCV code in future in the same project.
  6. We did it! Project is ready for future OpenCV development.

This concludes this tutorial. You are now ready to start making your own Android Studio Project that consists of OpenCV SDK features limited only by your imagination. In the next tutorial, I will SHOW CAMERA on android app screen using OpenCV. Make sure that you read it. Do not forget to SHARE this post and write your opinions, ideas and views  in the  COMMENT  section below.

Thanks for reading :). Please like my Facebook page and follow my Twitter for more tutorials.

Return to OpenCV for Android Tutorials List

Learning the Packages of OpenCV SDK for Android

Return to OpenCV for Android Tutorials List

In the previous tutorial, you learnt about the file structure of OpenCV SDK for Android. Now, we will learn the packages which are available for us to use in OpenCV SDK for Android. At the end of this tutorial, you will definitely know what OpenCV for android has to offer.

Each package is imported like this:

Each packages has java classes which contain functions which you use.

First of all, lets find the packages in the sdk folder manually. On your computer, once you have downloaded the OpenCV SDK, you can browse to the following address in order to find all the OpenCV for Android classes.

OpenCV-android-sdk\sdk\java\src\org\opencv

There, you will find the following packages (OpenCV 3.0):

Also, when you have created an OpenCV project in Android Studio, you will find the packages in the project browser like this:

Now, I will explain to you the purpose of each and every package so that you learn about OpenCV without any effort :D. You won’t regret coming on my blog 🙂

Note that each package can be imported like this as I told before:

Package  Name Purpose Description
android OpenCV interaction with Android platform This package has functions which can allow you to implement an OpenCV project in your Android device. This package handles interactions between OpenCV and Your phone.
calib3d  Project 3D coordinates of objects in a scene. “calib3d” stands for Camera Calibration and 3D Reconstruction. This package has functions which can determine 3D coordinates of objects. So lets say you have a box in a picture. calib3D functions can project 3D coordinates of that box if you provide “intrinsic” and “extrinsic” coordinates.

There are functions to estimate intrinsic and extrinsic parameters 🙂

core Brain of OpenCV. Does all what OpenCV does elsewhere.  “core” is the core package of OpenCV. This package contains functions which provide the base functionality of OpenCV. This include mathematical and matrix operations, algorithmic procedures and much more.

This library is not responsible for image processing. That library is called “imgproc” and it is discussed below.

engine  Provides basic information about current OpenCV SDK installation in your project  Basically, if in your project, you wish to obrain basic information about your OpenCV SDK such as  its packages, version, SDK path, and list of libraries.

Moreover, you can specify a specific version of OpenCV that user’s phone should download from play store. Pretty cool, right?

features2d  Detect and track features in a a 2D test image/frame  You will use this library to detect features, draw them and track them on the screen.
imgcodecs  Read/write images from your disc  Well, imagine you wish to work on an image that is in your disk instead of receiving it from camera. In this case, you will use this library. Its functions allow you to do the following:

  • Read single image
  • Read multiple images
  • Write an image (output)
  • Read specific formats of images in different ways to suit your project and purpose (encode/decode)
imgproc  Image Processing. DUH!  Whether you have loaded an image, or are using camera as input, you will be needing OpenCV to process the image. Well, “imgproc” is the library that has all the functions that you can use, manipulate and exploit to your liking.

This library has functions and classes which allow you to blur image, draw shapes, compare shapes, write text on image, apply enhancement functions and much, much, much more!

This library also has functions which detect points of interest of your liking.

ml  Machine Learning (Artificial Intelligence)  You can train OpenCV to do do Computer Vision according to your requirements. All you need to do is to teach it.

This library has classes and functions that train OpenCV to perform computer vision job for you 🙂

 

objdetect  Detect features or an object  This library has functions that give you the ability to implement detection of a specific custom-defined object in an image or camera feed.

Ever heard of Haar like feature detection? You can do that in this library.

photo  Image Enhancement  This library handles Image enhancement.

Want to set ISO? Exposure setting? contrast? Saturation? hue? Shit? Well, this library has all these functionalities and much more! You can calibrate so much.

One interesting feature is de-noising which will let you play with noise in your image ;D. Useful, right?

utils  Compatibility converters to support different formats of data This library has functions that convert image data from one mathematical format into another. For example, you can convert from one matrix format into another for image processing using a specifically designed code. It is a life saver.
video  Video Processing  This library will help you to perform video processing. You can track objects in a video, subtract background, apply video filters and much more.
videoio  Interact with video files  Using this library, you can open video files, open different format of video files and work with them.

 

This tutorial was brief and was supposed to give you the starting knowledge in order to grow your knowledge about OpenCV by yourself. Let me know in the comments what you think. I will be glad to answer. Now proceed to hte next tutorial 🙂

Return to OpenCV for Android Tutorials List