You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

2 Días
17 Hrs
45 Min
1 Seg

Introducción a OpenCV para Análisis de CCTV

3/16
Resources

Image processing with OpenCV has become a fundamental tool for visual analysis in retail environments. This technology enables companies to gain valuable insights into customer behavior, optimize product distribution and improve the shopping experience. In the following, we will explore how to implement computer vision solutions for a real case of customer flow analysis in a store.

What is OpenCV and why is it important for visual analytics?

OpenCV (Open Source Computer Vision Library) is an open source library designed specifically for image and video manipulation and analysis. This powerful tool offers numerous advantages that make it the preferred choice for computer vision projects:

  • Real-time processing optimized to run complex algorithms at high speed.
  • Cross-platform compatibility with Python, Java and C++
  • Extensive toolset including edge detection, face recognition, and image segmentation
  • Active community that constantly generates tutorials and updated documentation

To start working with OpenCV in Python, we need to install the library and perform some basic imports:

# Installation (if not already installed)# pip install opencv-python matplotlib
 # Necessary importsimport cv2import matplotlib.pyplot as plt

How to load and manipulate images with OpenCV?

Basic image loading

The first step in working with images is to load them correctly. OpenCV provides simple but powerful methods for this task:

# Define image path path = "data/centro_comercial.jpg"
 # Load image image = cv2.imread(path)
 # Check if image was loaded correctlyif image is not None: # Convert from BGR to RGB for display with Matplotlib image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)    
 # Display the image plt.figure() plt.title("Image captured by CCTV") plt.imshow(image_rgb) plt.axis('off') plt.show()else: print("Could not load image")

It is important to note that OpenCV handles images in BGR (Blue, Green, Red) format, while most other libraries, such as Matplotlib, use RGB (Red, Green, Blue) format. For this reason, it is necessary to perform a color conversion when working with both libraries simultaneously.

Real-time video capture and display

To capture video from a webcam, OpenCV offers a simple interface:

# Start video capture (0 for the first camera, 1 for the second, etc.)cap = cv2.VideoCapture(1)
if not cap.isOpened(): print("Could not open camera")else: while True: # Capture frame by frame ret, frame = cap.read()        
 if not ret: print("Could not read frame") break            
 # Show resulting frame cv2.imshow('Frame CCTV', frame)        
 # Exit if 'q' is pressed if cv2.waitKey(1) == ord('q'): break            
 # Release capture object and close windows cap.release() cv2.destroyAllWindows()

Loading pre-recorded videos

Similarly, we can load and play videos stored on disk:

# Load a video from filevideo_path = "data/video_store.mp4"cap = cv2.VideoCapture(video_path)
if not cap.isOpened(): print("Could not open video")else: while True: # Capture frame by frame ret, frame = cap.read()        
 if not ret: print("Could not read frame") break            
 # Show resulting frame cv2.imshow('Video CCTV', frame)        
 # Exit if 'q' is pressed if cv2.waitKey(25) == ord('q'): break            
 # Release capture object and close windows cap.release() cv2.destroyAllWindows()

What enhancement and annotation techniques can we apply to images?

Brightness and contrast adjustment

To improve the visual quality of images, especially in variable lighting conditions, we can adjust brightness and contrast:

# Adjust brightness and contrastcontrast_factor = 1.2 # Greater than 1 increases contrastbrightness_value = 30 # Positive value increases brightness
 # Apply transformationenhanced_image = cv2.convertScaleAbs(image,  alpha=contrast_factor,  beta=brightness_value)
 # Convert for displayenhanced_image_rgb = cv2.cvtColor(enhanced_image, cv2.COLOR_BGR2RGB)
 # Display enhanced imageplt.figure()plt.title("Image with adjusted brightness and contrast")plt.imshow(enhanced_image_rgb)plt.axis('off')plt.show()
 # Save processed imagecv2.imwrite("ccv_image_correction.jpg", enhanced_image)

Color correction and normalization

For more advanced color adjustments, we can work in the HSV (Hue, Saturation, Value) color space:

import numpy as np
 # Convert to HSVhsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
 # Increase saturationhsv_image[:, :, 1] = hsv_image[:, :, 1] * 1.5 # Multiply saturation channel
 # Ensure values are within valid rangehsv_image[:, :, 1] = np.clip(hsv_image[:, :, 1], 0, 255)
 # Convert back to BGR and then to RGB for displaysaturated_image = cv2.cvtColor(hsv_image, cv2.COLOR_HSV2BGR)saturated_image_rgb = cv2.cvtColor(saturated_image, cv2.COLOR_BGR2RGB)
 # Display image with adjusted saturationplt.figure()plt.title("Image with adjusted saturation")plt.imshow(saturated_image_rgb)plt.axis('off')plt.show()
 # Save processed imagecv2.imwrite("adjustment_saturation.jpg", saturated_image)

Image annotations

One of the most useful features of OpenCV is the ability to add annotations to images, such as lines, rectangles and text:

# Create a copy of the original imageannotated_image = image.copy()
 # Draw a linecv2.line(annotated_image, (50, 50), (200, 50), (255, 0, 0, 0), 3) # Blue in BGR
 # Draw a rectanglecv2.rectangle(annotated_image,(100, 100), (300, 200), (0, 255, 0), 2) # Green in BGR
 # Add textfont = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(annotated_image, "High traffic zone", (100, 90), font, 0.7, (0, 0, 0, 255), 2) # Red in BGR
 # Convert for displayannotated_image_rgb = cv2.cvtColor(annotated_image, cv2.COLOR_BGR2RGB)
 # Display annotated imageplt.figure()plt.title("Image with annotations")plt.imshow(annotated_image_rgb)plt.axis('off')plt.show()

Comparison of all applied techniques

To visualize the impact of the different techniques, we can show all processed images together:

plt.figure(figsize=(15, 10))
 # Original imageplt.subplot(2, 2, 1)plt.title("Original image")plt.imshow(image_rgb)plt.axis('off')
 # Image with adjusted brightness and contrastplt.subplot(2, 2, 2)plt.title("Adjusted brightness and contrast")plt.imshow(enhanced_image_rgb)plt.axis('off')
 # Image with adjusted saturationplt.subplot(2, 2, 3)plt.title("Adjusted saturation")plt.imshow(saturated_image_rgb)plt.axis('off'))
 # Annotated imageplt.subplot(2, 2, 4)plt.title("Annotated image")plt.imshow(annotated_image_rgb)plt.axis('off')
plt.tight_layout()plt.show()

Image processing with OpenCV offers a world of possibilities for visual analysis in commercial environments. From tracking customer flow to identifying high traffic areas, these techniques can provide valuable information for strategic decision making. In what specific situations would you apply these image processing techniques? Share your ideas and experiences in the comments.

Contributions 4

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?

La variaciones serian muy util es un joyeria : encerrando en un cuadrado si el cliente solicita que le enseñen algún producto. En un Taller mecánico, por ejemplo saturando los autos que esten en determinada parte del taller y que vayan por cierta etapa de reparación o servicio
yo lo utilizaría para nalizar videos de seguridad, gracias, en verdad es genial, sigamos aprwndiendo
Esto estaba buscando hace rato un curso practico para comenzar a ver que se puede optimizar!!! muchos exitoss para todos!!! buen curso.
*descargue dos vídeos de* [*https://www.pexels.com/es-es/buscar/videos/centro%20comercial/*](https://www.pexels.com/es-es/buscar/videos/centro%20comercial/) Son mp4, pero el código se va por if not ret: print ("Error:no se pudo leer") Alguien sabe que puede ser? En visual studio los puedo abrir y los lee sin problema