Detecting speed of a car from two images
Tags: computervision, java, imageprocessing, programming
Published: 2017-08-10 22:02:36

---
title: Detecting speed of a car from two images
published: true
description: We explore how to use basic Computer Vision techniques to get a car's speed
cover_image: https://thepracticaldev.s3.amazonaws.com/i/f4id4oaejwid5l90vv3p.png
tags: computervision, java, imageprocessing, programming
---

# Introduction
This article is intended for both beginners and experts in Computer Vision. I hope this post does CV justice ðŸ™ðŸ¾ .

## Problem
We already have motion sensors in play that does this sort of thing easily, but if we want to use computer vision, things get tricky. How to get the speed from just pictures may seem impossible at first but after reading this, hopefully things get better.

## Approach
To do this we need to follow the following steps
<ul>
    <li>Feed the two images to our application - <em>obviously</em> ðŸ˜‚ </li>
    <li>Get the difference between the two images</li>
    <li>Calculate the ðŸš—ðŸ’¨ from the image difference</li>
</ul>
*Sounds pretty easy right? Lemme take you on a ðŸŽ¢*

## Feed The Images To Our Application
We are using the default Buffered Image class provided by JAVA to read in the files with their paths specified.

*Implementation*

```java
BufferedImage image1 = ImageIO.read(new File(img1));
BufferedImage image2 = ImageIO.read(new File(img2));
```
<img src="https://thepracticaldev.s3.amazonaws.com/i/khx9i1gjjai315ygonkw.PNG">
*First Image*
<img src="https://thepracticaldev.s3.amazonaws.com/i/2wf68l7av82kkyb5vevh.PNG">
*Second Image*


## Image Subtraction To Get The Difference
Now that we have our two images, we then perform Image Subtraction. You may be wondering what image subtraction is.
Images are pretty much *an array of many pixel values that have been combined together.*
Image Subtraction in the simplest of terms is *Finding the difference between every pixel in an image and the corresponding pixel in another image.* To perform image subtraction, both images need to have the same resolution (*since we are subtracting every pixel in one from the other*). 
When we subtract the image, we set a *threshold* and set any pixel value higher than the threshold to BLACK, and pixel values lower than the threshold to WHITE ( *We are doing this because we want to see the cars in black after subtracting the images* )

*Implementation*

```java
public BufferedImage ImageSubtract(BufferedImage img1 , BufferedImage img2){
		
    int imageheight = img1.getHeight();
    int imagewidth = img1.getWidth();	
    
    WritableRaster image1 = img1.getRaster();
    WritableRaster image2 = img2.getRaster();
		
    // pixel values have their, red , green and blue components

    int diffred;    
    int diffblue;
    int diffgreen;
    
    // set threshold for subtraction
    
    Color treshold = new Color(30,30,30);
                
    // Create an empty image
    BufferedImage DiffImage = new BufferedImage(imagewidth,imageheight,BufferedImage.TYPE_INT_RGB);
	
    for(int y = 0 ; y < imageheight ; y++){    
        for( int x = 0 ; x < imagewidth ; x++){
	    diffred = Math.abs( image1.getSample(x, y, 0) - image2.getSample(x,y,0));
	    diffgreen = Math.abs(image2.getSample(x,y,1) - image1.getSample(x, y, 1));
	    diffblue = Math.abs(image2.getSample(x,y,2) - image1.getSample(x, y, 2)); 
	    
            // combine individual rgb components to give one color
            
            Color diff = new Color(diffred,diffgreen,diffblue);
	    DiffImage.setRGB(x, y, diff.getRGB());
            
            // to visualize the differences in the image
	    
            if(DiffImage.getRGB(x, y) < treshold.getRGB()){
	        diff= Color.WHITE;
		DiffImage.setRGB(x, y,diff.getRGB());
      	    }
	    else{
	        diff = Color.BLACK;
	        DiffImage.setRGB(x,y,diff.getRGB());
	    }
	}
    }
    try {
        // write the result image to a file
        ImageIO.write(DiffImage,"jpg",new File("MotionDetected.jpg"));
    } catch (IOException e) { e.printStackTrace(); }		
        return DiffImage;
    }
```
<img src="https://thepracticaldev.s3.amazonaws.com/i/lvma4uh5m26bcn1j3hge.png">
*Result Image*

## Speed Calculation From Image Difference
Now that we have the difference visualized, its time for the "fun" part ðŸ•ºðŸ¾. Getting the speed from the image. To this, we applied some fun logic 
*Disclaimer : There may be a better way to implement this, but I was gunning for the method most easy to explain*

We run through the "difference" image and then pick the first BLACK pixel you find and then save it. Then we run through the "difference" image again and then pick the last BLACK pixel we find.

Now some assumptions will have to be made
<ul>
    <li>Time passed between the first and second picture say 5s</li>
    <li>Assumed Magnification of the camera used to take the pictures</li>
    <li>Length of the car in millimeters (say 6000mm)</li>
</ul>

Once these assumptions have been made, we then covert the length of the car to pixels to make life easier and then get the distance moved,
`distance_moved = Math.abs(x2 - x1) - car_length_in_px;`
We subtracted the car length in pixel because the distance moved is from the front of the car.
We then solve for speed
`speed = (double) distance_moved / timepassed;`

*More details below*
<img src="https://thepracticaldev.s3.amazonaws.com/i/oo330kthg8tmr2waqj0h.png">


*Implementation*

```java
public double SpeedCalc(BufferedImage img){
		double speed = 0.0;
		int x1 = 0;
		int x2 = 0;
		WritableRaster newImg = img.getRaster();
		for(int y = 0; y< img.getHeight(); y++){
			for(int x = 0; x < img.getWidth(); x++){
				if( newImg.getSample(x, y, 0)== 0){
					x1 = x; // thats the the first x black value
					break;
				}
				else{
					continue;
				}
			}
			if(x1 > 0){
				break;
			}
		}
		for(int y=0; y< img.getHeight(); y++){
			for(int x = 0; x<img.getWidth(); x++){
				if( newImg.getSample(x, y, 0)== 0){
					x2 = x; // thats the last x black value
					break;
				}
			}
		}
		// we only really need the x values 
		int timepassed = 10; // in seconds
		double mag = 0.09; // assumed magnification more details in report
		int car_length = 6000; // in millimeters
		int car_px = (int)(car_length * mag);
		System.out.printf("Final Position of Car:%d, Initial Position of Car:%d \n" , x1,x2);
		int distance_moved = Math.abs(x2 - x1) - car_px;
		speed = (double) distance_moved / timepassed;
		return speed;
	}
```

*Results*

```java
$ java MotionDetection images/car_moving_final_1.PNG images/car_moving_final_2.PNG
The resulting difference of the image is stored in MotionDetected.jpg
Time Passed between 2 images 7 seconds 
Final Position of Car:370, Initial Position of Car:1311 
Speed of car is 0.63 metres/second
```
Here's a link to the github repo if you want to see the full source code
https://github.com/oreHGA/Speed-Detector

*Thanks for riding with me!* ðŸ‘¨ðŸ¾”ðŸ’»