the Power of Image Processing vs. AI in Smart Parking: A Comparative Analysis

Bilalchahir
5 min readMay 8, 2024

--

Imagine teaching computers to see and understand the world like we do. That’s the magic of image processing and computer vision. In this article, we explore how these technologies are changing the way we park our cars. Join us as we uncover how computers learn to “see” parking spaces and make finding a spot easier for everyone.

Computer vision :

encompasses a broader scope, leveraging algorithms and models to enable machines to interpret and understand visual data much like human vision. In the context of smart parking, a computer vision approach would involve training deep learning models such as convolutional neural networks (CNNs) to directly analyze parking lot images and classify parking spaces as either vacant or occupied. These models can learn complex features and patterns from the data, allowing for sophisticated decision-making processes.

Image Processing :

Image processing, on the other hand, focuses on manipulating and analyzing images to extract meaningful information or enhance their visual quality. In the context of smart parking, an image processing approach might involve traditional techniques such as thresholding, edge detection, and contour analysis. For instance, thresholding techniques can be used to segment parking spaces from the background, while edge detection algorithms can identify boundaries between parked vehicles and empty spaces.

Comparison:

  1. Complexity: Computer vision approaches tend to be more complex due to the involvement of deep learning models that require extensive training data and computational resources. Image processing techniques, on the other hand, are often more straightforward and rely on predefined algorithms.
  2. Flexibility: Computer vision techniques offer greater flexibility in handling diverse scenarios and variations in parking lot conditions. They can adapt and learn from new data, making them more robust in real-world environments. Image processing techniques, while less flexible, can still be effective in controlled settings with consistent lighting and camera angles.
  3. Accuracy: Computer vision approaches generally have the potential to achieve higher accuracy levels, especially when trained on large datasets and optimized with state-of-the-art architectures. Image processing techniques may struggle with complex scenes or variations in lighting conditions, leading to lower accuracy rates.
  4. Resource Requirements: Computer vision approaches typically require significant computational resources for training and inference, as well as access to large datasets for effective learning. Image processing techniques, being more simplistic, may require fewer computational resources and can be implemented on less powerful hardware.
Image Processing , Computer Vision and AI

Example:

Let’s explore two scenarios in the realm of smart parking: one using only image processing and the other solely reliant on computer vision. In the image processing scenario, cameras analyze parking spaces to detect vacancies using basic techniques like identifying edges and colors. Conversely, in the computer vision setup, sophisticated algorithms recognize empty spaces by comprehensively understanding the parking lot environment, including vehicles and other objects.

we will split the article into two part :Image Processing And Computer vision

Image Processing :

We’re diving into image processing for our smart parking solution. Using this method alone, we’ll analyze parking lot images to distinguish between vacant and occupied spaces, all without the use of AI.

  1. Create Project Folder:
  • Open your terminal or command prompt.
  • Navigate to the directory where you want to create the project folder.
  • Run the following command to create the folder:
mkdir smart_parking

2. Install Prerequisites:

pip install opencv-python # Install OpenCV
pip install cvzone # Install cvzone library
pip install numpy # Install numpy library
  • Download the video of the parking area from this link
  • Place the video file in the project folder (smart_parking).
  • Run this Script to Capture Image of Parking from the video.
import cv2

def extract_first_frame(video_path, output_image_path):

video = cv2.VideoCapture(video_path)

if not video.isOpened():
print("Error: Could not open video file.")
return
ret, frame = video.read()

if not ret:
print("Error: Could not read the first frame from the video.")
return
cv2.imwrite(output_image_path, frame)
video.release()

print("First frame extracted and saved as", output_image_path)

video_path = "parking_video.mp4"
output_image_path = "parking_image.jpg"
extract_first_frame(video_path, output_image_path)

3. Annotating Parking Spots

  • Run this Script to start annotation :
import cv2
import pickle

width, height = 70, 30

try:
with open('CarSpots', 'rb') as f:
posList = pickle.load(f)
except:
posList = []

def Click(events, x, y, flags, params):
if events == cv2.EVENT_LBUTTONDOWN:
posList.append((x, y))
if events == cv2.EVENT_RBUTTONDOWN:
for i, pos in enumerate(posList):
x1, y1 = pos
if x1 < x < x1 + width and y1 < y < y1 + height:
posList.pop(i)

with open('CarSpots', 'wb') as f:
pickle.dump(posList, f)


while True:
img = cv2.imread('parking.jpg')
for pos in posList:
cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), (0, 128, 0), 2)

cv2.imshow("Image", img)
cv2.setMouseCallback("Image", Click)
cv2.waitKey(1)

create rectangle on left-click and remove it on right-click.

spot annotations
  • after you finish the annotation the script will save a file with the annotations named “CarSpots” contains the coordinates of the rectangles

4. Final Step:

run the following script

import cv2
import pickle
import cvzone
import numpy as np


cap = cv2.VideoCapture('parking_video.mp4')

with open('CarParkPos', 'rb') as f:
posList = pickle.load(f)
width, height = 70, 30


def checkParkingSpace(imgPro):
spaceCounter = 0

for pos in posList:
x, y = pos

imgCrop = imgPro[y:y + height, x:x + width]
count = cv2.countNonZero(imgCrop)


if count < 500:
color = (0, 255, 0)
thickness = 5
spaceCounter += 1
else:
color = (0, 0, 255)
thickness = 2

cv2.rectangle(img, pos, (pos[0] + width, pos[1] + height), color, thickness)
cvzone.putTextRect(img, str(count), (x, y + height - 3), scale=1,
thickness=2, offset=0, colorR=color)

cvzone.putTextRect(img, f'Free spots: {spaceCounter}/{len(posList)}', (100, 50), scale=3,
thickness=5, offset=20, colorR=(0,200,0))
while True:

if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
success, img = cap.read()
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imgThreshold = cv2.adaptiveThreshold(imgGray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 25, 16)
imgMedian = cv2.medianBlur(imgThreshold, 5)
kernel = np.ones((3, 3), np.uint8)
imgDilate = cv2.dilate(imgMedian, kernel, iterations=1)

checkParkingSpace(imgDilate)
cv2.imshow("Image", img)
cv2.imshow("ImageThres", imgMedian)
cv2.waitKey(10)
Image Threshold

So here I will explain how it works , the main code based on two functions on openCV the first one is “countNonZero” and the second one is “adaptiveThreshold ”.

This is the Final result:

Note that I dont annotate all Spots. you can annotate it with yourself.

Final Result

For further information, please visit my GitHub Repository via this link.

See you in part 2 of the Computer vision project :)

--

--