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(;
// 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)
   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
         // Call the progress function, pass the current loop iteration
         Integer progress = new Integer(i);
         // Sleep for 500ms
   return null;    
   // This function is used to send progress back 
   //   to the UI during doInBackground
   protected void onProgressUpdate(Integer...integers){
      // Log what the functions is doing
      // 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.)
          "sample_"+l_integers, "drawable", getPackageName()));
   // This function is called when doInBackground is done
   protected void onPostExecute(Boolean b){
      // Log what the functions is doing

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

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.

   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)

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

   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.

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


using namespace std;

class textEdit{

   GtkTextView *l_text_view;
   Glib::RefPtr l_text_view_buffer;

   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(){}



#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

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



// Create the object
textEdit textEdit;

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


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.


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.

   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)