Wednesday, January 18, 2012

Android Coding - AsyncTask Simple Example

Coming from a C++ background I have found it very insightful to code in Java with Android. I have found that Java lends itself to Object Orientation more readily than C++.  In this case inheriting from Android's AsyncTask Class.

AsyncTask is a way to easily perform asynchronous tasks.  Extremely useful when updating the UI.  In fact, you cannot simply write a For Loop in your base Android code and have it update the UI per each iteration.  It will only update the UI at the end.

My example is simple: take an imageView that has been defined in the main.xml file and update the image that it contains every 500ms after pressing a button.

For this example I assume you have already created the basic structure of your program with an imageView, a button, your images and the structure to allow you to click the button and perform an action.


Call the AsyncTask
This code should go where you are calling your button
// Define the # of iterations to change the image
Integer runLength = new Integer(4);
      
// Link to your imageView in your GUI
imageview1 = (ImageView)findViewById(R.id.imageView1);
      
// Call the AsyncTask
new ChangeGuiImages2().execute(runLength); 
Execute the AsyncTask
This code should go in the same function as the rest of your code (for my example it was directly after the onCreate function)
private class ChangeGuiImages2 extends 
   AsyncTask <integer, boolean integer> {

   // This function performs the task (does not update the UI)
   @Override
   protected Boolean doInBackground(Integer... runLength){
      
      // Make a copy of the runLength Integer
      int l_runLength = runLength[0].intValue();
      
      
      // Run you loop to update the imageView
      for (int i=0; i < l_runLength; i++){
       
         // Log what the function is doing
         Log.d(TAG,"doInBackround"+i);
       
         // Call the progress function, pass the current loop iteration
         Integer progress = new Integer(i);
         publishProgress(progress);
          
         // Sleep for 500ms
         SystemClock.sleep(500);
      }
   return null;    
   }
     
   // This function is used to send progress back 
   //   to the UI during doInBackground
   @Override
   protected void onProgressUpdate(Integer...integers){
      
      // Log what the functions is doing
      Log.d(TAG,"onProgressUpdate:"+integers[0]);
      
      // Create a local copy of the integer 
      int l_integers = integers[0].intValue();
      
      // Set the imageview to the appropriate image based on iteration. 
      //   (sample_0, sample_1, ... etc.)
      imageview1.setImageResource(getResources().getIdentifier(
          "sample_"+l_integers, "drawable", getPackageName()));
   }
     
   // This function is called when doInBackground is done
   @Override
   protected void onPostExecute(Boolean b){
      
      // Log what the functions is doing
      Log.d(TAG,"onPostExecute");
      
   }     
} 

Saturday, September 3, 2011

OpenCV cvReleaseImage Crashing Problem

Random crashing can occur when you are using the cvQueryFrame function and the cvReleaseImage function together. With the below code implemented you can cause your program to sporadically crash.  The issue lies in releasing the frame.  The capture can be released safely.  Not sure why it does this but it does.

...
frame = cvQueryFrame(capture);
...
cvReleaseCapture&capture);
cvReleaseImage(&frame);
...

Tuesday, March 2, 2010

Eclipse Copy Shortcut

Ever want to have something saved in the clipboard and still be able to copy/paste other information?  Well in Eclipse there is a very handy feature for such a thing.  Follow these easy steps:
  1. Copy some text into your clipboard (e.g. CTRL C)
  2. Highlight your second text
  3. Middle click where you want the second text to be pasted (or left click first and then middle click)

Monday, February 22, 2010

Blob Analysis Introduction

I did a presentation on blob analysis a couple years back. I think it is an important topic that I enjoyed learning about. I covered blob analysis and a brief amount of morphology.  Another student covered that in depth so there is not too much info there.  Information on this topic can be found online, but actual algorithms for many of the ideas was tough to find.

Blob Analysis
    - This is taking an image, thresholding it to black and white (binary) only and then using this to make decisions or take measurements.

   Examples:
   Centroid - Find the center of a blob object
   Size - Find the size of a blob object
   Amount - Finds the amount of blobs in the image
   Extent - Find the extent of an image (usually grouping is a good method)

Morphology
   - This is used on a black/white image to enhance the image.  Of course you can apply some of these techniques to enhance the image prior to making the image binary (color or greyscale). 

   Examples:
   Erosion - This shrinks blob objects
   Dilation - This grows blob objects
   Watershed - This helps find edges
   Boundary Extraction - This helps find edges

I plan on going into each section in more detail when I get the chance.  For now here is the presentation that I made for my class. 
 

C++ Code for Minimum Value

Here is some code that I wrote while learning about vectors and templates.  The code takes a large sequence of numbers and finds the smallest value for a smaller segment of the larger sequence.  This is repeated for each small segment that fits into the larger segment.

Example:
   We have a large sequence (size 20) and want to find the minimum value for each small segment (size 5).

Large Sequence:
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

1st Small Sequence               Output
 1 2 3 4 5                                   1

2nd Small Sequence              Output
 6 7 8 9 10                                 6

3rd Small Sequence               Output
 11 12 13 14 15                         11

4th Small Sequence               Output
 16 17 18 19 20                         16
 Code:
    Here is the code

Wednesday, February 17, 2010

How to update a GTK::TextView from a separate object

If you are wondering how to update a text box that was created in your GTK GUI from a separate object then you have come to the right place.  As of this positing date I have searched for the solution to this problem, but there is no real mention on how to do it.  The basic premise is to get the underlying object of the TextView and pass it to the object that wants to edit it.  Then that object must create a pointer to this object and can then access its information.  Anyways lets get to it.

File List:
  • textEdit.cpp - This is where the code is written to update the TextView declared in example.cpp
  • textEdit.h - This is the header file for the code that will update the TextView declared in example.cpp
  • example.cpp - This is where the GUI and TextView are defined

textEdit.h

#include 
using namespace std;

class textEdit{

private:
   GtkTextView *l_text_view;
   Glib::RefPtr l_text_view_buffer;

public:
   textEdit(){l_text_view = NULL; l_text_view_buffer = Gtk::TextBuffer::create();}
   void updateText();
   void setTextViewObject(GtkTextView *text_view){l_text_view = text_view;}
   virtual ~textEdit(){}

}


textEdit.cpp

#include "textEdit.h"

void updateText(){
   string update_words = "Testing, Testing, Testing";

   // Setup a temporary pointer to a TextView
   Gtk::TextView *temp;

   // Get the old buffer and append the new update to it (this can be changed to whatever)
   string bufferNew = update_words + "\n" + l_text_view_buffer->get_text();

  // Set the new buffer text to the actual buffer
  l_text_view_buffer->set_text(bufferNew);

  // Take the l_text_view and assign this pointer to temp
  temp = Glib::wrap(l_text_view,false);

  // Update the temp TextView with the buffer
  temp->set_buffer(l_text_view_buffer);
}


example.cpp

.
.
.

// Create the object
textEdit textEdit;

// Send the text_view object.  This would be defined in you header file as such:
//      Gtk::TextView text_view;
textEdit.setTextViewObject(text_view.gobj)

.
.
.

Tuesday, February 16, 2010

OpenCV 2.0, Windows and Eclipse Setup Instructions

This blog involves the latest release of OpenCV 2.0.  I have concentrated most of my time into developing OpenCV applications in the Linux environment.  This time I wanted to learn how to set it up in Windows with the latest stable release of OpenCV.

Documentation:

This following document involves the setup of Eclipse for OpenCV2.0 in Windows, the installation of all necessary programs to be installed to run OpenCV2.0 in Eclipse, and how to add an existing project.  This document was originally designed for my Professor's Digital Image Processing class at the UNH ECE Department.  The basic setup procedure was created by Jon Carrier over at Carrier Frequency.  This document incorporates images for ease of understanding.

Document: OpenCV2.0 Project Setup in Eclipse.pdf

Sample Code:

This sample code is for someone that wants to get into Image Processing with a minimal setup.  Students of Image Processing classes are encouraged to utilize this code.  I ask that you do not remove the citation information in order to preserve the originality of the work.

What it is:
   This is code that can be imported into Eclipse and run and modified without much effort.

What is does:
   The code opens a camera capture and allows the user to press certain keyboard numbers to toggle between algorithms.  By default the program has 5 settings.
  1. Press the number 1 button to run the Canny edge detector with a specified threshold
  2. Press the number 2 button to run the Canny edge detector with a threshold value of 1
  3. Press the number 3 button to run an algorithm that manipulates at the pixel level to remove the green channel.
  4. Press the ESC key to exit the program
  5. Press any other key to undo all editing settings (default case)
What to do with it:
   Edit the code and make it your own.  For users that do not know much about OpenCV and/or C++ programming the code is very simple and there are great instructions included.  

How to add your own code:
   In the Laboratory.cpp file add a new case to the switch statement.  Just increment the case number and the ASCII value for that value is the button you press to call your function.  Then edit VisionJobs.h to declare a new function.  Then edit VisionJobs.cpp and add your new function there and modify the frames as necessary.

Code:
   Ok lets get to it and let you download the code.

Download location is here (zip file containing all .c and .h files and Eclipse .cproject and .project files)

Tuesday, January 5, 2010

OpenCV libcv.so.2.0 error

Sometimes after a fresh install of OpenCV in a fresh install of Linux you may come across the error:

error while loading shared libraries: libcv.so.2.0: cannot open shared object file: No such file or directory


If you do then follow the instructions provided by an OpenCV thread on nabble.com. Click here

Monday, November 23, 2009

Kalman Filtering Application for Foggy Images

A couple of years ago I took an Estimation and Filtering class. We covered many different topics, but one of the main ones was Kalman Filtering. This is a very popular type of filtering and estimating that attempts to use past measurement(s) (noisy) and estimate the true current value(s) (with error minimized). Some assumptions are made with the type of noise that we are dealing with. Try this page or this page for some better descriptions of Kalman Filtering and the algorithm.

In this class we had to find an application and discuss it.  Here is my presentation on the paper titled:    A KALMAN FILTER BASED RESTORATION METHOD FOR IN-VEHICLE CAMERA IMAGES IN FOGGY CONDITIONS

Paper Link

Presentation Link

Wednesday, October 28, 2009

Gnuplot - Simple Tutorial

There are a ton of tutorials on how to use gnuplot, but most can be convoluted. Here is a simple example/tutorial for gnuplot.

1. Put data in a file (e.g. plot1.dat).  X-axis data followed by a space and Y-axis data

1 23.8
2 23.8
3 23.8
4 23.8
5 23.75
6 23.75
6.5 24.15
6.75 24.45
7 24.95
7.25 25.15
7.5 25.30
7.75 25.40
9 25.70
10 25.83
11 25.88
12 25.89
13 25.90
14 25.90

2. Put the setup commands in another file (e.g. plot1.setup)

# Basics
unset log
unset label
unset arrow
set xtic auto 
set ytic auto 
set autoscale

# Title Information
set title "Temperature vs. Time - Trial 1"
set xlabel "Time (min)"
set ylabel "Temperature (celsius)"

# Initial Point (1,23.80)
set label "i" at 2,24.8 font "Arial,25" offset 1
set arrow from 2,24.80 to 1,23.80

# Final Point (14,25.90)
set label "f" at 13,24.90 font "Arial,25" offset 1
set arrow from 13,24.90 to 14,25.90

# Time fired (6,23.75)
set label "a" at 6,24.75 font "Arial,25" offset 1
set arrow from 6,24.75 to 6,23.75

# Temp reaches 60% (7.1,25.04)
set label "b" at 8.1,25.04 font "Arial,25" offset 1
set arrow from 8.1,25.04 to 7.1,25.04

# Max Temp (13,25.90)
set label "c" at 12,24.90 font "Arial,25" offset 1
set arrow from 12,24.90 to 13,25.90

# Make the plot
plot "plot1.dat" with linespoints

3. Run the following command
   a. gnuplot -persist -raise plot1.setup

Thursday, October 15, 2009

C++ number precision

Today I learned a very useful feature that C++ has.  It is the precision of numbers when using streams. Below I will show how the .precision function is important if you want precision when displaying numbers to the screen using the cout function or when writing numbers to a file using ofstream.  Undoubtedly there are other ways to use it, but I am more concerned with those two functions.

COUT
The below code will produce an output that is the full 10 digits in length

double digit = 5.000000001
cout.precision(10);
cout << digit << endl;
visit here for a more in depth explanation/example

OFSTREAM 

The below code will save your value in 10 digit format to a file (appending it to the end)
// Make a value to write to a file called file.txt
double value = 15.12345678;

// Create and open a stream
ofstream myFileWrite; 
myFileWrite.open("file.txt",ios::app); // append to the end of the file

// Make sure we opened it right
if (!myFileWrite.is_open())
   return -1;


// Define the precision to write at 
myFileWrite.precision(10);

// Write the value to the file at the end of the file 
myFileWrite << value << endl;

// Close the file
myFileWrite.close();

Tuesday, October 6, 2009

Eclipse - Go to function

Following in the foot steps of Jon's blog on a very cool Eclipse function (Eclipse C/C++ IDE File/Folder Restore), here is another very useful function of Eclipse.

This function allows you to go to a variable, function, etc. from where you are calling it.  The variable/function that you are interested in is linked to another part of your code and Eclipse of course knows this (assuming you have completed a build).  So how do you do it?

Hold down the Ctrl button and click on the variable/function as if it were a regular link.

Friday, October 2, 2009

Senior Year Project - Robotics


At the University of New Hampshire Electrical Engineering Department there is a requirement to take a final project class for your entire last year. Students from EE and ME combine together and choose projects that they are interested in. Teams then complete a full development cycle on the project they choose. During the class we did such things as team organization, budget creation, criticality analyses, bill of materials, Gantt charts, weekly reports, complete testing for design verification, project design, hands-on creation of project, coding, engineering, and more! The project was very detailed and ended with a final product, a final report over 70 pages in length and a presentation at UNH's Interdisciplinary Science & Engineering Symposium (click here for the webpage).  Also visit the link for a video of the robot in action.  And you can see that we won "Best Project Presentation" for 2007.

(The robot image above is property of my buddy Jeff Nichols who was our team leader and made this for our team)

So what did my team do? We developed an Autonomous Fire Fighting Robot and entered it into the Trinity College Competition. My group, Johnny 5 Associates, Inc., had 5 engineers. We had 2 Software Engineers, 2 Electrical Engineers and a Mechanical Engineer. We designed a robot that had a Motorola Microprocessor, stepper motors, rechargeable batteries, CO2 extinguisher, horn detector, Hamamatsu flame detector, infrared phototransistors, sonar, and white-line detectors.




My main part of the project was sensor research/integration for location information, object detection, horn detection and cabling.

Location Information & Object Detection

In order to know where the robot is and where it should go I found multiple sensors that would follow along with the specifications set forth by Trinity College.  I decided to utilize 2 infrared "white-line" sensors, 4 sonar sensors and dead reckoning.  I looked at other sensors such as accelerometer and compass sensors, but decided against them for cost and other reasons.  The compass for example would not be the best choice because there can be metallic objects in the maze that could mess with the sensor.

Infrared "white-line" sensors
Each room in the maze had a white line across it.  We decided to use this line as a way to reorient the robot as it enters each room (thus decreasing error).  So as the robot approached the line, it would sense the line with one sensor and rotate the opposite wheel until the robot was square with the line.

Sonar sensors
To avoid objects that may be in the maze and to measure distances to the nearest walls we utilized sonar.  Infrared would have worked, but there is the possibility that mirrors may exist in the maze.  The sonar worked great most of the time, but weird readings would occur sometimes.

Dead Reckoning
A great method is one where you count the turns of your wheels.  In this case if you have a stepper motor, you can pulse the motors and count how many pulses you send.  Thus you can get a distance measure.  The problem with this is that you accumulate error over time.  In order to fix this error we would take measurements with the sonar at certain points to fix the error.

Horn Detection

One of the best parts of this project for me was the creation of a horn and a detector.  This was part of the rules as an added bonus.  The idea being that your  fire alarm goes off and the robot begins.  So the rules stated that your detector must be between 3 and 4 kHz and that you must provide a transmitter that a judge, or the user, can start the robot with.

I implemented a frequency transmitter using a 555 timer and a gain circuit that drives a small speaker.


I also implemented a frequency detector... Unfortunately we purchased a  pre-made circuit that can detect a certain frequency range based on your pot. adjustment and then it activates a relay.  It is unfortunate because I found a chip that performs the same task and is easily created.  Either way I had to create a triple gain stage from a microphone and feed it into the relay circuit which when activated flips the voltage of a microcontroller input.

The system worked flawlessly and was an impressive way to start the robot.

Make sure to come back to my blog as I will eventually be describing more of the robot in greater detail (with more images).







Tuesday, September 29, 2009

Eclipse - OpenCV Project Setup

After you install OpenCV you will most likely want to use a programming IDE to write your code. In this case I am assuming you have chosen the Eclipse C++ IDE. To install this go here to download and untar the program where you will want it to reside on your machine.  Then run the program (no install necessary... it's written in java).

If you have installed the OpenCV library on your machine to /usr/local then you can follow this setup. Otherwise you will have to change how you link to OpenCV appropriately.  This section also relates to the setup of gtkmm and fftw.  If you do not want to use them, remove their sections.

1) Right click on your project and go to Properties
2) Go to the C/C++ Build section and then Settings
3) Under GCC C++ Compiler,  click on Directories
4) Add the following paths under Include Paths

/usr/local/include/opencv
/usr/lib/gtkmm-2.4
/usr/include/gtkmm-2.4


5) Under GCC C++ Compiler,  click on Optimization
6) Add the following under Other optimization flags

-ggdb `pkg-config opencv --cflags --libs` `pkg-config gtkmm-2.4 --cflags --libs`


7) Under GCC C++ Linker,  click on Libraries
8) Add the following under Libraries

cv
gtkmm-2.4
highgui
fftw3
9) Add the following under Library search path


/usr/local/lib
/usr/lib

Thursday, September 24, 2009

Neural Networks - Perceptron Character Recognition

A couple of years ago I took a really fun and challenging class called Introduction to Neural Networks.  We discussed the theory and applications of neural networks, including single- and multi-layer feedforward and recurrent networks, supervised and unsupervised learning, etc.

If you are unfamiliar with Neural Networks, they are methods that attempt to mimic the human brain (hence neural) by utilizing the basic fundamental building block of a neuron.  Neural Networks provide the ability to learn and make decisions based on various ideas related to increasing or decreasing the connections between neurons.

For a great introduction please see Jon Carrier's Neurocomputing Presentation

In this class I did a presentation on the ability of single perceptrons to perform character recognition.

Here is a link to my professors blog that describes our classes presentations: eceblogger

Here is a link to my presentation: Google Doc Link... Well I was going to post it, but I do not have it on hand just yet.  Will post once I get to it.

Some Interesting Bash Functions

In Linux there is a type of scripting that can be done in the Bash shell.  This is a very powerful feature of Linux, as Bash offers many advanced features.  This blog will show some of the features I have learned.  I will not attempt to cover a tutorial but rather varying functions that I have found useful.

IFS
   One of the interesting things that I have learned is the usage of IFS.  According to wikipedia it stands for Internal Field Separator.  What it does is allow you to define what Bash defines as a separator.  Bash defaults to space as the separator, therefore functions that utilize IFS will separate items based on spaces.

  While scripting you might want to change this.  Maybe a comma or a new line will be what is wanted.  Here is an example that sets the value to a new line and then back to the default.


#set IFS to be a new line
IFS="
"
#set IFS to be a space
IFS=" " 

Checking Arguments
  When you are implementing a bash script you can pass in arguments to the program.  Each argument is listed as $(value) where value ranges from 0 on up.  $0 is your program, $1 is the first passed in argument, etc.

Now what we want to do is check how many the user input.  Maybe we only want 2 values to be input, so we should check that.  Here is an example that checks for 2 inputs.


if [ $# -ne 2 ]; then
  echo "You did not input 2 arguments, try again!"
  exit
fi

Iterate Through all Files in CWD
  Sometimes it is necessary to iterate through all files in a certain directory.  There are a few ways to do this, but if you are dealing with files that may have spaces in them, the best way is the following.


for file in `ls -A $src`; do 
  sourceFile="$src/$file"
  # Now do what you want it to do with each source file
done


* Note: In the above example you will note $src, this is the full length path where you are searching files.  This can be a hard coded variable or possibly an argument to your function.

* Note: Whenever you reference sourceFile make sure to use quotes

Checking for Certain Things
  When you are coding you might want to check what something is.  Here are a few ways to check.

Checking that a directory exists

if [ ! -d "$dir" ]; then
  #do something since the directory does not exist
fi

Checking for a regular file

if [ -f "$file" ]; then
  #do something since it is a normal file
fi

Checking if file1 is newer than file2

if [ "$file1" -nt "$file2" ]; then
  # file1 is newer, so do something here
fi

Tuesday, September 15, 2009

Audio Watermarking

While working towards my Master's thesis I took an Advanced Topics class titled Speech Processing. In this class we covered the basics of processing speech data (not specifically speech recognition, but all encompassing). Topics such as the anatomy of speech production, speech synthesis, voice estimation and specific applications through student presentations. My presentation was on Audio Watermarking.

I found a recent paper on the subject and attempted to recreate what the authors had implemented. I implemented their algorithm in Matlab. Unfortunately it did not work as well as theirs, but I was lucky enough to be chosen as the best presentation by the class.

To view my presentation go here: Google Doc Link

For images of my presentation as well as other students please visit my professors eceblogger site.

Just for fun, here is an image of the matlab gui I created for this project.

Wednesday, August 26, 2009

Ubuntu OpenCV FFMPEG error

If you are in Ubuntu 9.04 and attempting to write a video using OpenCV you may encounter an error such as:

OpenCV Error: Bad argument (codec not found) in CvVideoWriter_FFMPEG::open

The only solution that I have found is to install the following
sudo apt-get install libavcodec-unstripped-52

It seems to remove a bunch of packages but everything I do (reading/writing videos) seems to be working. So proceed with caution on installing this, but it may fix what ails you.

Working with gtkmm - A basic intro

I am not attempting to know everything about gtkmm or post an exhaustive tutorial on it. However if you would like to create a basic GUI in a relatively short period of time then you have come to the right place.

Here is a link to a more exhaustive tutorial (where I learned myself):Programming with gtkmm

Also visit this link which has detailed information on each class/namespace/file:
gtkmm 2.4 Documentation

Quick Introduction:

Abstract:
This tutorial covers how to create a GUI including one button arranged within a single frame. In another post I will eventually cover the basics of how to create a "more" section (i.e. hidden widgets). This is a very basic GUI that simply allows you to run commands depending on the callback of the button.

Idea:
The basic idea of gtkmm (GTK in general) is to create a gui where all elements are relative in position and size (for the most part). Each element is considered a widget and the things that hold it, containers. Elements are typically considered children, and containers parent(s).

Main.h
Add:
#include 
#include "gui.h"


Main.cpp
Add:
Gtk::Main kit(argc,argv);
gui gui;
Gtk::Main::run(gui);


gui.h
Add:
class gui : public Gtk::Window
{
public:
gui();
virtual ~gui();

protected:
//Signal handlers:
virtual void on_button_clicked();

//WINDOW - Child widgets
Gtk::Frame window_frame;
Gtk::VBox window_box;
Gtk::Button window_button,

gui.cpp
Add:
// Sets the Window Title
set_title("Window Title");

// Sets the border width of the window
set_border_width(10);

// Add the box to the window (the window can contain one widget/container)
add(window_box);

// Add the frame to the box (boxes can contain multiple widgets/containers)
window_box.pack_start(window_frame);

// Label the frame
window_frame.set_label("Main");

// Add a button to the frame (frames can contain one widget/container)
window_frame.add(button);

// This is how you do a call back (to a function where you define whatever you want)
button.signal_clicked().connect(sigc::mem_fun(*this,
&gui::on_button_clicked));


That's it. Just define your on_button_clicked() function and you are all set to make a simple GUI.

How to post code on your blog

I was unaware of how to do this and did some searching online. There are many ways of doing it. The following blog has a great way to do it and the directions are very clear and to the point.

Vivian's Tech Blog - How to post source code in blogspot.com