DEV Community

Cover image for Detecting speed of a car from two images
Oreoluwa Ogundipe
Oreoluwa Ogundipe

Posted on

Detecting speed of a car from two images

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

  • Feed the two images to our application - obviously 😂
  • Get the difference between the two images
  • Calculate the 🚗💨 from the image difference

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

BufferedImage image1 = ImageIO.read(new File(img1));
BufferedImage image2 = ImageIO.read(new File(img2));
Enter fullscreen mode Exit fullscreen mode


First Image

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

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;
    }
Enter fullscreen mode Exit fullscreen mode


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

  • Time passed between the first and second picture say 5s
  • Assumed Magnification of the camera used to take the pictures
  • Length of the car in millimeters (say 6000mm)

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

Implementation

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;
    }
Enter fullscreen mode Exit fullscreen mode

Results

$ 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
Enter fullscreen mode Exit fullscreen mode

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! 👨🏾”💻

Top comments (4)

Collapse
 
arj profile image
arj

Very interesting!

Did you try it with cross-correlating the one car to the other in the image? That would maybe not require knowing the length of the car.

Collapse
 
cannuhlar profile image
Can Nuhlar

Project is on GitHub, why don't you make a pr and give it a try :) I would like to see it in action.

Collapse
 
ore profile image
Oreoluwa Ogundipe

Yeah that's a bright idea. Seeing as we may not have the length of the car every time. I'll look into it. Thanks for sharing

Collapse
 
jordanirabor profile image
Jordan Irabor

This was very enjoyable to read!