0% found this document useful (0 votes)
12 views10 pages

OpenCV Training for 2D Vision Lab

Uploaded by

edjinedjalaris
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

OpenCV Training for 2D Vision Lab

Uploaded by

edjinedjalaris
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Vision 2D: Lab

Sounkalo DEMBÉLÉ, Mayra Yucely BEB CAAL

Université de Franche-Comté
Contents

1 Introduction 2
2 Training 3
2.1 Experimental setup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 Analysis of imaging system . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.1 Reading of video stream from camera . . . . . . . . . . . . . . . . . . . . . . 4
2.2.2 Objective of 25 mm focal length . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2.3 Objective of 25 mm focal length with extension tube . . . . . . . . . . . . . 5
2.3 Reading, processing and analysis of single image . . . . . . . . . . . . . . . . . . . . 5
2.4 Reading, processing and analysis of video stream from le . . . . . . . . . . . . . . 7

3 Testing 8

1
Chapter 1
Introduction

The educational objective of the lab is initiation into computer vision through OpenCV with
Python by applying it to the metrology of colored targets. OpenCV is the reference library in
computer vision, it is open source and used extensively by both academia and business.

2
Chapter 2
Training

The purpose of this part is learning of OpenCV. No report will be requested or mark be attributed.

2.1 Experimental setup


Used vision system includes :
ˆ a backlight,

ˆ a frontlight,

ˆ a USB 2D camera embedded with a standard objective along with, or not, an extension tube,

ˆ a computer running Python (from Anaconda) with OpenCV version 3.41 .

The camera is an uEyeSE from IDS. Its specications include:


ˆ CMOS color sensor of 1/2" (diagonal: 8 mm),

ˆ format full frame of 2048 x 1536 pixels,

ˆ depth of 8 bits,

ˆ acquisition using rolling shutter method,

ˆ full frame frequency of 11 Hz,

ˆ USB2.0 output.

The specications of objective include:


ˆ C-mount,

ˆ focal lenght of 25 mm,

ˆ aperture from 1.6 to 16 mm,

ˆ minimal working distance of 250 mm,


1 The management of the versions of OPENCV is special: several versions continue to be updated in parallel,

2, 3 and 4. For compatibility with the lab, it is recommended, if you wish to make a personal installation, to use

version 3, regardless of the release.

3
ˆ maximal working distance of innity.

The specications of extension tube include:


ˆ C-mount,

ˆ lenght from 5 to 40 mm.

Working distance can be modied for focusing purpose, assuming height of backlight is about
13.5 cm.

2.2 Analysis of imaging system


An OpenCV program will be used for the characterization of the used optical imagery. It should be
remembered that focusing in such imagery includes the adjustments of, working distance, imaging
distance and aperture of diaphragm.

2.2.1 Reading of video stream from camera

Copy, analyze and test the following code for reading a video stream until SPACE is clicked to stop:
"""
VISION 2D:
Training code
"""

import cv2

"""
Reading a video stream
"""
# Configure video stream source: 0 is the defaut one
cam = [Link](0)
if (not [Link]):
print ('Impossible to read the camera !')

# Display stream until clic on SPACE when mouse pointer in video display
while (True):
ret, frame = [Link]()
[Link]('video', frame)
if [Link](2)>=27:
break
# Deallocate memory
[Link]()
[Link]()
Listing 2.1: Python Code for reading a video stream

2.2.2 Objective of 25 mm focal length

Use the image of a graduated ruler to estimate the maximum and minimum working distances,
and the minimum eld of view.
Therefore, compute the depth of eld, the maximum magnication and the minimum resolution.

4
2.2.3 Objective of 25 mm focal length with extension tube

Recalculate parameters: maximum and minimum working distances, minimum eld of view, depth
of eld, maximum magnication and minimum resolution.

Do the results conform to theory?

An application of such imaging system is analysis of watch parts.

2.3 Reading, processing and analysis of single image


Consider the following code, that read, processes and analyzes the image [Link] (g-
ure 2.1).

Analyze the code then run it: during image display with imshow, the value of pixel under cursor
is displayed botton left.

Figure 2.1: Color image for testing.

"""
VISION 2D:
Training code
"""

import cv2

5
"""
Reading a single image
"""
# Read and display BGR image
bgr = [Link] ( './[Link]', cv2.IMREAD_UNCHANGED )
if (not [Link]):
print ('Impossible to read image !')
[Link]("RGB image", bgr)
[Link](0)

# Convert BGR into HSV and display


hsv = [Link](bgr, cv2.COLOR_BGR2HSV)
[Link]("HSV image", hsv)
[Link](0)

# Extract hue and display


h,s,v = [Link](hsv)
[Link]("Hue image", h)
[Link](0)

# Convert BGR into GRAYSCALE and display


gray = [Link](bgr, cv2.COLOR_BGR2GRAY)
[Link]("Gray image", gray)
[Link](0)

# Display grayscale image with a color map


clr = [Link]( gray, cv2.COLORMAP_JET )
[Link]("Color map image", clr)
[Link](0)

# Global thresholding of GRAYSCALE and display


rv, bry = [Link](gray, 0,255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
[Link]("Binary image", bry)
[Link](0)

# Local thresholding of GRAYSCALE and display


bry2 = [Link](gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,\
cv2.THRESH_BINARY, 11, 2)
[Link]("Binary image 2", bry2)
[Link](0)
[Link]()
Listing 2.2: Python Code for reading a video stream
Complete the code to perform the following tasks:
1. erosion, dilatation, opening and closing of images, grayscale as well as global-thresholded
binary images (functions erode, dilate et morphologyEx, respectively),
2. calculating the regions of binary image assuming a closed edge corresponds to a region (func-
tion findContours),
3. calculating and displaying bounding rectangles of regions (function boundingRect),
4. deblurring of grayscale image with rectangle kernel (function blur) then with gaussian kernel
(function GaussianBlur),

6
5. enhancement of grayscale image with morphological gradient (morphologyEx), Sobel gradient
(function Sobel) then with Canny gradient (function Canny).

2.4 Reading, processing and analysis of video stream from


le
It is similar to reading a video stream from a camera except the source number is replaced with
the le name including extension, inside single or double quotes.

7
Chapter 3
Testing

The purpose of this part is testing of your skills in computer vision through the use of OpenCV.
It is requested to supply a report describing the solution and the corresponding code. They will
be used for mark.

Some colored papers such that each paper has a unique uniform color are considered: pink,
purple, blue, green, orange, yellow and white (gure 3.1).

Figure 3.1: Reference image for color and sector calibration.

The papers move in a space structured in sectors numbered from top to bottom: 0 to 3.

The problem to solve consists in recognizing the color and position of the papers from their
images recorded in a video ([Link]).

8
The image [Link] is provided for manual calibration of the sectors and colors, assum-
ing it is acquired in the same conditions as the video.

More precisely, the application should deliver per frame the following informations and save
them in a le [Link]: identication of any object by its appearance number in the image,
its color, the sector in which it (its center of inertia) is located (gure 3.2).

Figure 3.2: A frame illustrating application output.

It is advised to organize the code as follows


ˆ a function to manually calibrate the seven colors of the problem, i.e. to determine minimal
and maximal hues of every color;
ˆ a function to manually calibrate the three sectors of the problem, i.e. to determine minimal
and maximal locations of every sector;
ˆ a function to identify every paper by its order of appearance and calculate its color and
sectors;
ˆ a function for display information: number, color, sector.

Common questions

Powered by AI

Morphological operations like erosion, dilation, opening, and closing help enhance image accuracy by altering pixel structures for clearer delineation of object boundaries. These techniques refine noisy data, making objects within images more discernible. Gradient enhancements, including morphological gradient, Sobel, and Canny operations, accentuate edges, improving detection accuracy in object recognition tasks. These processes are vital when identifying and analyzing objects' features for higher precision detections in tasks like dynamic object tracking or distinguishing intricate details in images, providing cleaner and more informative visual data for analysis .

The educational objectives of the computer vision lab are to introduce students to computer vision with a hands-on approach using OpenCV. By engaging in activities such as video and image processing, students familiarize themselves with real-world applications of vision technology, like metrology of colored targets. The setup, including cameras, lighting, and computer processing, allows students to apply theoretical knowledge practically, enhancing comprehension through exploration, experimentation, and validation of concepts like focal length, magnification, and color calibration, reinforcing critical thinking and problem-solving skills .

Programmatic calibration of colors and sectors in a vision system requires determining the minimal and maximal hues for each target color (pink, purple, blue, green, orange, yellow, and white) and identifying the sector location limits in images. This involves manually calibrating using given images to ensure accurate color detection and spatial recognition in varying lighting and positional conditions. Additionally, considerations must be given to ensure that the calibration is robust enough to handle slight variations in imagery, providing consistency in identifying object's color and sector accurately within recorded videos .

Manual calibration of colors and sectors is crucial for ensuring accuracy in video analysis, especially when testing on papers of unique colors. This process fine-tunes the recognition system to precise hues and positional data, reducing errors caused by environmental factors like lighting variances. Consistent calibration allows the system to differentiate among initially similar objects, achieving precise tracking and classification in diverse testing conditions. In paper-based tests, this precision ensures robust performance, accurately logging and analyzing colors and their spatial sectors within video frames, which is critical for tasks like automated sorting or visual inspection .

The process of reading, processing, and analyzing video streams involves configuring and capturing video from the default camera source using OpenCV in Python. The program is set to display the video until a space is clicked, which is used for calibration exercises like estimating distances using camera imagery. This practical application allows users to test vision algorithms in real-time situations, crucial for understanding dynamic vision environments. It serves educational purposes, enabling students to interactively engage with vision data and corresponding code for learning computer vision dynamics .

Integrating OpenCV into educational labs enriches learning by providing students with a practical, open-source tool widely used in academia and industry for computer vision tasks. This exposure enables students to gain skills applicable to real-world scenarios, bridging the gap between theoretical studies and practical applications. It fosters creativity as students experiment with image and video processing, enhances analytical skills through troubleshooting and problem-solving, and encourages collaborative learning as students share and discuss their findings. Furthermore, such integration inspires interest and innovation, paving the way for advancements in fields like robotics, automation, and artificial intelligence .

In computer vision tasks, techniques like erosion and dilation are applied through respective functions (erode, dilate) to refine image shapes by adding or removing pixels on the object boundaries. Techniques such as thresholding convert grayscale images into binary by setting a threshold intensity, achieved through methods like OTSU for global thresholding, or adaptive for local. These processes are critical for tasks such as contour detection and image segmentation, wherein they identify and highlight key features for analysis. This aids in object recognition and enhances image clarity for further processing steps, like region calculation and bounding rectangles .

The focal length is used to estimate maximum and minimum working distances, along with the minimum field of view by analyzing imagery with specific setups like a ruler. This determination influences the depth of field, magnification, and resolution. For example, with a 25 mm focal length, imaging adjustments calculate these parameters, critically affecting how detailed and close objects appear in images. An extension tube modifies these parameters, further affecting theoretical versus practical results in imaging, such as in watch part analysis .

The main components of the vision system in the lab include a backlight, a frontlight, a USB 2D camera (uEyeSE from IDS), and a computer running Python with OpenCV version 3.41. The camera features a CMOS color sensor with specifications like a full frame format of 2048 x 1536 pixels, and a depth of 8 bits, and it utilizes a rolling shutter method with a frequency of 11 Hz. The objective lens has a focal length of 25 mm, C-mount, with an aperture range from 1.6 to 16 mm, and working distances from 250 mm to infinity. An extension tube can adjust the working distance, improving focus for detailed analysis. These components collectively support the practical application and exploration of OpenCV in metrology of colored targets .

Recalculating imaging parameters with an extension tube involves determining the maximum and minimum working distances, minimum field of view, depth of field, magnification, and resolution. Practically, the extension tube enhances the focal length, allowing closer observation of small details, such as watch parts. However, this recalibration must confirm with theoretical expectations, challenging students to verify practical results against expected outcomes. Discrepancies might arise from real-world imperfections or variations, which can influence the system’s reliability and require adjustments for precision in observations .

You might also like