Setting Manual Exposure in OpenCV

11 June, 2017 (14:01) | Snippets, Programming, Code, Python, OpenCV | comments off

In OpenCV, you can set the exposure for your camera manually by using the VideoCapture::set method. What isn’t obvious is what value to pass in for it. I have experimented with various webcams and the below settings seem to be accurate enough. Not all cameras allow setting the exposure programatically, so if the code below doesn’t work, you might try a different camera, or experiment with different values. Once you’ve set a value manually, I haven’t found a way to switch back to auto-exposure without restarting the camera.

Below is an example of using this setting in Python

import cv2

cap = cv2.VideoCapture(0)

    ret, frame =

    if cv2.waitKey(1) & 0xFF == ord('q'):


Estimated exposure settings from Joan Charmant’s ‘Review of the ELP-USBFHD01M camera’

-1 640 ms
-2 320 ms
-3 160 ms
-4 80 ms
-5 40 ms
-6 20 ms
-7 10 ms
-8 5 ms
-9 2.5 ms
-10 1.25 ms
-11 650 µs
-12 312 µs
-13 150 µs

Android: Simple Example of Page Transition Using ViewPager

17 May, 2015 (18:54) | Java, Snippets, Programming, Code, Android | 0 comments

The ViewPager class built in to the Android SDK is a simple method of showing an animated transition between two views. For example, turning a page, or a calendar scrolling through months. This functionality can be added with only a few lines of code. Below I present an example containing pretty much the bare minimum of code and markup required to get a ViewPager up and running.

The steps are fairly straight foreward:

  • Edit your Activity’s layout to include a “ViewPager”. As in activity_main.xml below.
  • Define the layout for your “pages”. This will be a new .xml file containing a layout just like you define for an activity (pageritemview.xml below).
  • Create a new PagerAdapter class (e.g. ViewPagerAdapter below) which implements the display of your layout. There are a handful of methods you must override, but the bulk of the work is done in “instantiateItem()”.
  • In your activity’s “onCreate()” method, initialize the viewPager.

One likely question after getting this to work is “Can a ViewPager allow infinite scrolling?”. The simple answer is “No”. But there are some workarounds. There are a few people who have implemented their own ViewPager class modifications, but I haven’t used any of them, so cannot recommend any. The more common workaround is to return a count far higher than a user is likely to ever scroll (e.g. 10,000), and then in your Activity’s onCreate() method call the ViewPager’s setCurrentItem() method to set the current item to be the middle.

Below, I have chosen to just display the current position as a string for a simple example. In reality, you would use the position as an index into an array or a database to look up the information you want to display for a specific item.

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

class ViewPagerAdapter extends PagerAdapter {
    Context context;
    LayoutInflater inflater;

    public ViewPagerAdapter(Context context) {

    public int getCount() {
        return 10;

    public boolean isViewFromObject(View view, Object object) {
        return view == object;

    public Object instantiateItem(ViewGroup container, int position) {
        inflater = (LayoutInflater) context
        View itemView = inflater.inflate(R.layout.pageritemview, container,

        Just as an example, we just display the current position as text.  In reality
         you would use the "position" variable to look up the current item to display
         in an array or a database.

        return itemView;

    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((LinearLayout) object);


public class MainActivity extends Activity {
    ViewPager viewPager;
    PagerAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {

        viewPager = (ViewPager) findViewById(;
        adapter = new ViewPagerAdapter(MainActivity.this);


<LinearLayout xmlns:android=""
    xmlns:tools="" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

        android:layout_height="fill_parent" />


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" android:layout_width="match_parent"



A Quick Snippet for Drawing Images in Java

2 October, 2014 (02:22) | Java, Snippets, Programming, Code, Android | 0 comments

Java does not have the best built-in tools in the world for drawing images, but it’s still useful for many purposes. Below is just a snippet on getting started drawing in Java. For more information on what’s available see the Java Graphics class reference.

BufferedImage i = new BufferedImage(500, 500,BufferedImage.TYPE_INT_RGB);
Graphics g=i.createGraphics();
g.drawLine(0, 0, 500, 500);
g.drawString("This is a test", 20,20);
ImageIO.write(i,"jpg",new File("test.jpg");

A basic 3d to 2d projection

2 October, 2014 (02:09) | .Net, Programming, C#, Code, Computer Science | 0 comments

Developing an actual 3D engine is an incredible amount of work, and including a 3D library in your project can add a considerable amount of bloat to your project. Fortunately you can implement some very simple 3D projections in just a couple of lines of code, and they are not computationally intensive. There are limitations on using this particular method though:

  • This can draw wire frames only.
  • You need all of the coordinates of your “world” to be centered horizontally and vertically relative to your screen (or you need to convert them to be that way).
  • All of the lines must fit on the screen (e.g. you can’t have the edge of an object partially off-screen)

The basic algorithm is incredibly simple. For each given point (x,y,z) in your 3D model, you convert it to 2D screen coordinates (x’,y’) by the following equations:


Where (x,y,z) are the points in your 3D world, (x’,y’) are the x,y coordinates from the center of your screen. And d is the focal length of the camera (or scaling factor), if you’re not sure what to use you can get good results just by experimenting.

Below is C# code to demonstrate this concept. Notice that most of the code is for manipulating, offsetting, and scaling the box, the actual code projecting to 2D is only two lines.

        protected override void OnPaint(PaintEventArgs e)
            float[]  box = {

            float scale = .2f;
            for (int x = 0; x < box.Length; x++) box[x] *= scale;

            for (int j = 0; j < 10; j++)
                for (int k = 0; k < 10; k++)
                    float zoffset = 10f;
                    float xoffset = 1f*(j-5);
                    float yoffset = 1f*(k-5);

                    float d = 500;
                    float lastX = d * (box[0] + xoffset) / (box[2] + zoffset);
                    float lastY = d * (box[1] + yoffset) / (box[2] + zoffset);

                    Pen p = new Pen(new SolidBrush(ForeColor));
                    int i = 3;
                    while (i < box.Length)
                        float x = d * (box[i + 0] + xoffset) / (box[i + 2] + zoffset);
                        float y = d * (box[i + 1] + yoffset) / (box[i + 2] + zoffset);
                        e.Graphics.DrawLine(p, translate(new PointF(lastX, lastY)), translate(new PointF(x, y)));
                        lastX = x;
                        lastY = y;

                        i += 3;


        private PointF translate(PointF p)
            PointF f = new PointF(p.X+this.Width/2, p.Y+this.Height/2);
            return f;

3D Projection example

This is a very useful technique for drawing diagrams and simple objects, and can be a starting point for adding more features like rotation, or solids.

Android: Accessing other views from inside a custom view using findViewById().

1 October, 2014 (11:15) | Java, Snippets, Programming, Android | 0 comments

If you attempt to call findViewById() on the ID of a view that is not from the Activity that holds the view, findViewById() returns null. I’ve seen a lot of solutions posted for attempting to access a View from outside the activity that created it. Most of them involve inflating the original view and going from there. I can’t speak to whether or not that actually works (I do not believe they will), but I can offer an alternative, more attractive solution.

For this option, you will need a reference to the activity that holds the view you want to modify, this is usually given to you in the form of a Context object passed into your constructor. You can save that context in a member variable, and from there call context.findViewById(), which will return the view you want.

public class SomeClass extends View{
     Context context;

     public SomeClass(Context context, AttributeSet attrSet){

     public void someFunction(){
          TextView t=(TextView)context.findViewById(;

In many cases, it will be preferable to not access the View directly, and instead add a public function to the Activity that actually holds the view. This way many other classes will be able to access the same function, which is very often necessary. Then, in order to access that function, you cast the Context to the Activity Class that holds the view:

public class SomeClass extends View{
     Context context;

     public SomeClass(Context context, AttributeSet attrSet){

     public void someFunction(){

I would say the second example is a much more elegant solution as it keeps the logic controlling a View inside the class that holds it. But, as with everything, you’ll likely find many exceptions.

Why you should return “this” from your setter methods.

26 September, 2014 (21:35) | .Net, Java, Snippets, Programming, C#, Code | 0 comments

Fortunately most API designers know to use this technique, but it’s still pretty common to find some that do not. It’s generally good practice, that when writing a setter method for a class, that you return the object’s “this” rather than “void”. The reason is that it allows a user of your class to chain together setter methods and save some typing and screen space. For example:

SomeObject o=new SomeObject().setTextSize(5).setBackgroundColor(Color.BLUE).setSomeOtherProperty(true);

The alternative would look like:

SomeObject o=new SomeObject();

The first example is much more concise and uses up much less screen real estate for someone reading the code, and also requires less typing. Granted the second example is much easier to read. But you should leave it up to the developer to determine which style they want to use.

In order to enable such a feature, all you need to do is return “this” from your class’ setter methods. For example:

public SomeObject setTextSize(Integer textSize){

     return this;

Since almost all setter methods do not return a value, this can easily be added to a class.

An alternative in some situations, is where the developer is likely to want the old value (usually because they want to restore it when they’re done). For example:

public Integer setTextSize(Integer textSize){
     Integer temp=this.textSize;
     return temp;

If you have some methods that return the previous value of a setter, it’s generally not a good idea to mix returning “this” and previous values inside the same class. It would be difficult and cumbersome for a user to remember which functions return the object, and which functions return the previous value, and may try to chain together setters that aren’t possible. Such a situation would be particularly problematic if the object returned from your setter actually had a valid function that the user was trying to call.

There is also a small amount of overhead in returning an object that will just be ignored in many (or most) circumstances. But, in almost all circumstances, this small amount of overhead is negligible.

Resurecting a broken Android phone as a development sandbox for app development.

24 August, 2014 (22:17) | Java, Programming, Operating Systems, Android | 0 comments

I had the unfortunate incident of cracking the screen on my fairly new Galaxy S5. Since most of the phone worked, I wanted to get all of my old data off, on to my new phone, and finally re-purpose the old phone as a development sandbox which I can dedicate to Android app development. Exactly which steps you’ll need to go through will depend on the functionality remaining in your device, what settings you’ve enabled, and the tools you have at hand. This details the method I used, which is possibly one of the worst case scenario that can happen, so you can probably use some of this info even if you aren’t in quite as bad of a situation.

My Situation

In my case, the screen was completely broken, it would not display information nor register touches. I had also not enabled developer options, and had a lock screen swipe pattern set. Fortunately, the new phone I bought was exactly the same as the old one, this allowed me to see what the screen would look like without being able to see the screen of my old phone. This was important for counting keystrokes needed to perform tasks in the blind, as well as tracing out the pattern for the lock screen. I had not yet paired the phone with any Bluetooth devices like a keyboard or mouse, nor had I rooted my phone.

Features that made it possible

The Galaxy S5, and many other phones have the build in ability to use a mouse, keyboard, and display through the USB port on the phone. You will need a USB host cable (apparently also called USB-to-go cable) to plug in a mouse and keyboard, you will also need an HDMI adapter for your phone to display the screen on an external monitor.

The Ultimate Goal

The bad thing is that you can’t have a mouse, keyboard, computer, and the display hooked up at the same time, and you need all of these to develop apps in a normal manner. Here is a small overview of the steps:

  • Unlock the phone completely in the blind
  • Pair the phone with a Bluetooth keyboard and mouse
  • Enable developer options
  • Disable the lock screen
  • Backup phone
  • Root the phone
  • Set up the phone to install APKs over the air

Unlocking the phone

This was undoubtedly the hardest part. There were many claims of being able to unlock the phone through USB hooked up to the computer, but required a rooted phone. If you have a password set rather than a swipe pattern, you can simply plug a USB keyboard into your phone using a USB Host Cable, type in your password, press Enter, and be done with it.

With a swipe pattern, it’s far more difficult as you need to know where the mouse pointer is, and if you attempt to swap between the mouse and HDMI display the pattern times out and resets, so the only way to do this is completely in the blind. What I did was set the same unlock pattern on my new phone, then taped a piece of paper to my desk, taped a pencil to my mouse, and traced out the pattern on the sheet of paper. After plugging in the mouse, I moved it to the lower left corner, to ensure I had a home location to start the pattern from, I then traced a line to where my pattern started, clicked the button and traced out the pattern. I then plugged the mouse into my new phone and retraced over the same line to unlock the phone. It is important to keep the mouse in a common orientation as well as trace the line, because if you rotate the mouse with your wrist, it won’t actually be moving in the same direction. For this I visually kept the side of the mouse square with the sheet of paper. It took me a good 15-20 minutes to finally unlock my phone. When it unlocks, it will play a sound, so there’s no need to keep switching back to the HDMI display to see if it worked.

Paring with a Bluetooth Keyboard and Mouse

After you have the phone unlocked, you can switch between the HDMI display, keyboard, and mouse to see where the cursor is and what’s on the display. In my case, I did not have a real Bluetooth mouse, the regular wireless mice work only with their specific dongle, and although you can use them as a mouse with the dongle plugged in, you cannot register them as a Bluetooth mouse. I did have a Bluetooth keyboard available, and fortunately this was enough to navigate around and take care of most of the work. You can move around the screen and select icons using the arrow keys, and in most apps you can use the arrow keys to select most fields, but in some cases the app only works with mouse clicks.

I never figured out how to get the menu to pull down with just they keyboard, but I was able to access the settings by navigating around with the arrow keys to select the “All Apps” from my quick launch, from there I made my way to the settings icon and managed to pair my Bluetooth keyboard. This was all done over the course of 10-15 minutes of swapping out the USB keyboard with the USB HDMI display and seeing where the cursor was and where it needed to go.

Enabling Developer Options

Once you have a Bluetooth keyboard attached, it’s fairly easy to navigate to the settings an enable the developer options (on the Galaxy S5 that’s done by going to “About” in the phone settings and clicking on the build number 10 times). But when you actually plug it into the computer, you’ll be prompted to give the computer permissions, and this prompt goes away if you try to unplug it from the computer and plug in the HDMI display. This issue was solved for me by following the same steps on my new phone, from there I was able to determine that to accept the prompt using the Bluetooth keyboard I just needed to click the down arrow once and the right arrow twice, then press enter.

Disable the lock screen

Without a Bluetooth mouse, I did not bother attempting to disable the lock screen. You have to draw the lock pattern again to disable the lock screen, and it does not play a sound when you do it correctly, making it a very time consuming process. The pattern is also slightly different on the disable screen than on the real lock screen which meant I’d have to trace out a new pattern again, making it take even longer. So, instead I went into the developer options and enabled the “Stay Awake” option which causes the phone to not re-lock while it’s charging. This was able to hold me over until I came across a Bluetooth mouse. An actual Bluetooth mouse is kinda hard to find, I finally found a combination small keyboard and touch pad at Best Buy near the tablet Bluetooth keyboards, after pairing that, I was able to use it as a mouse to disable the lock screen permanently.

Back up the phone and restore it to my new one

This was a lot harder than it should be. There is a backup service built into Android, but it is far from reliable, on my new phone only about 10 apps out of the hundreds were actually restored, and about only 5 of my contacts. If you try to search around, you’ll see a lot of very sketchy apps, and some legitimate ones that require root. The one that finally worked for me, not requiring root, was Helium. It does require that developer options are turned on. But it was able to back up and restore all of my apps and data except two of them which deliberately blocked being backed up.

Rooting the phone

I generally do not like to root my phones. I prefer that when I develop an app, that I can test it in a very similar environment that will be used by all of my customers, and I’ve never found a particularly good reason to do so anyway, at least until now. In order to install applications over the air, that is with the HDMI cable plugged in rather than plugged in to the computer, you’ll require a rooted phone. I was a bit concerned about having to do any work in the blind during the root process, but it went off without a hitch just following the “normal” instructions found pretty much everywhere.

Another option that might not require root is to connect the phone to the computer by USB and echo the screen to the PC. But this likely has performance issues, and the apps I have seen as of this writing either require root or don’t appear very mature.

Setting up the phone to receive APKs wirelessly

I used “ADB Wireless” from the play store for this. You run the app on a rooted phone, then you run “adb connect″ from your PC. From then on, the remote phone appears just like a directly connected one, which you can verify using the command “adb devices”.

How To Measure Any Distance With The Pythagorean Theorem

8 July, 2013 (18:23) | Programming | 0 comments

Interesting article on how to use the Pythagorean Theorem to measure distances of non-spatial domains.

List of my favorite free/open source utilities 2012

8 September, 2012 (23:17) | Windows, Linux, Programming | 0 comments

While I’ve been a huge fan of Scott Hanselman’s Tool List, but was pretty disappointed in the 2011 list in that it lacked conciseness. Below is a short list of software I use on a very regular basis, install on pretty much every PC that I can, and are all tools I know very well. Of course there’s a lot of commercial software I use too, but below are the ones you can download freely.

  • Notepad++ This is the Notepad replacement for me, and makes a great code editor.
  • Cygwin This utility gives you many of your favorite *nix commands under windows like grep, head, etc, many interpreters like perl, python, bash, etc, and a wide range of many other utilities. Powershell, which is now bundled with Windows is often touted as an alternative, but does not have anywhere near the number of utilities/scripts available. Additionally Powershell gives significantly different names and parameter names to similar Linux utilities, forcing you to learn two different command sets if you work with Linux too. So, if you’re using Linux at all, Cygwin is the way to go.
  • Robocopy comes with Windows now. Beyond just copying files from one place to another, I use it to back up my important code/documents to a separate drive every night at 3am.
  • VirtualBox My favorite virtualization software. I no longer have any PCs dedicated to running Linux for testing or development work. There are lots of virtualization options out there, I chose VirtualBox mainly for it’s USB support.
  • Virtual PC This used to be my virtualization software of choice, I keep it around because I still have some old VMs I never converted over.
  • 7-Zip This has been my archive software of choice for many years now. Previously it was WinRar, which is a commercial program. Even though I own several licenses for WinRar, I no longer feel a need to use it as 7-Zip does everything I need now.
  • Foxit This is a PDF reader, an alternative to Acrobat Reader. I switched to this reader from Acrobat many years ago due to the fact that Acrobat was becoming so bloated. I also don’t like reading PDF files in my browser, and attempting to configure Acrobat to only open as a separate process wasn’t reliable, seems it would always somehow revert to opening in the browser.
  • Combined Community Codec Pack This is a codec pack that makes fairly certain you’ll be able to play almost any video you come across. Downloading videos is fairly rare now days, but I still install this on every fresh Windows install I come across.
  • Synergy allows you to use the same keyboard and mouse across several PCs. You just move the mouse off the edge of the screen on one computer and on to the screen of another.
  • Thunderbird I haven’t yet given up on a local e-mail client. But I do have all of my e-mail forwarded to GMail too. I actually still receive a lot of e-mail, and more than Google allows me to store. Not to mention I have 15+ years of e-mail history stored locally.
  • Arduino The Arduino IDE for working with electronics. Before the Arduino came along I was using SDCC for the PIC. Working with Arduinos is so much easier for me, and does pretty much everything I need.
  • Processing The Processing IDE. Uses a similar language as the Arduino, and is the de-facto language many people use to interface a PC to an Arduino. I can’t say I write a whole lot of programs using this, but mostly use it to run other people’s programs to interface with an Arduino.
  • CDBurner XP Software for buring CD/DVDs. The name makes it sound obsolete, but it works with Win7 and Vista, and burns DVDs too.
  • Dimension 4 A program to synchronize your PC’s clock with an atomic clock over the Internet. There is functionality built in to Windows to do the same thing, but this gives you more control. The Windows functionality is difficult to verify if it’s working properly without just changing the clock time and waiting an hour to see if it’s fixed. D4 also has the added feature in that it graphs all of the adjustments made, so you can see how (in)accurate your clock is.
  • Eclipse This IDE should need no introduction. I use it only for Java development, and often still use Notepad++ instead for small, one off programs.
  • Free Download Manager For downloading large files that may fail, or that you want to schedule to download at a later time.
  • Frhed A free and Open Source Hex editor.
  • Oracle Java JDK Needed to develop Java applications. I have no real reason for using the Oracle JDK over any other, other than it’s just what I’ve always used.
  • Linux in a virtual machine - Ubuntu Server Many things are just easier with Linux, especially if you work with other Linux servers. Every PC I have will have at least one “utility” Linux VM, this is in addition to any sandbox or development Linux VMs.
  • WinMerge A graphical version of the “diff” utility. It compares two text files and highlights the differences between them.
  • cmdhere.reg This adds a context menu to Windows Explorer allowing you to quickly open a command prompt at the location you’re viewing. Additionally, I also create a shortcut to CMD.EXE on my desktop, and assign the hot-key CTRL-ALT-D, to open a command prompt.
  • Putty An SSH client for connecting to the many Linux systems I use.
  • SpaceMonger A utility to visually show you what is using up all of the space on your hard drive. The old 1.4 version is free. Another purely free similar application is WindirStat, but not quite as good of a UI IMHO.
  • RealVNC Software that lets you remote control another Windows [or Mac, or Linux, etc] PC from anywhere you can open a TCP connection. There is Remote Desktop built in to Windows now, but Remote Desktop has a lot of limitations like the fact that it blanks out the screen on the host PC when it’s remote controlled, and you have to log out the current user if its an account with a blank password. I do use Remote Desktop when I log in to other Windows servers, where I don’t actually want to control what’s on the screen of another system, but I use VNC when I’m connecting to other desktop PCs.
  • Firefox My browser of choice. The only other browser I’ve spent much time with is Internet Explorer, which I feel hides too much information from developers. Other browsers like Chrome and Safari seem to lack the wide range of plugins Firefox has.
  • Visual Studio Express Well, I don’t actually use the Express version which is free, but if you don’t want to buy the whole thing [or your employer won’t buy it for you] then Visual Studio Express is a great way to program in C#.

Copying text and HTML to the clipboard at the same time from a Firefox add-on

8 July, 2011 (10:07) | JavaScript, Programming | 0 comments

The JavaScript code below demonstrates how to copy two different formats of data to the clipboard at the same time. So applications wanting only text can grab the text version, but applications supporting HTML can grab that version instead. The basic idea is that you call addDataFlavor() for each type of data you want to represent on the clipboard, then call setTransferData() for each of those flavors of data.

function CopyToClipboard(textData,htmlData){
	var text      = Components.classes[";1"].createInstance(Components.interfaces.nsISupportsString);     = textData;
	var html      = Components.classes[";1"].createInstance(Components.interfaces.nsISupportsString);     = htmlData;
	var trans = Components.classes[";1"].createInstance(Components.interfaces.nsITransferable);
	trans.setTransferData("text/html", html, htmlData.length);
	trans.setTransferData("text/unicode", text, textData.length*2);
	var clipid = Components.interfaces.nsIClipboard;
	var clip   = Components.classes[";1"].getService(clipid);
	clip.setData(trans, null, clipid.kGlobalClipboard);