[go: up one dir, main page]

0% found this document useful (0 votes)
0 views15 pages

M-1 Python

OpenCV is an open-source computer vision library that facilitates real-time image processing and supports multiple programming languages and platforms. It provides various functionalities such as image reading, transformation, filtering, and object detection, making it widely applicable in industries like robotics and medical imaging. Key operations include image manipulation, color space conversion, and geometric drawing, all of which are essential for tasks like face recognition and self-driving car navigation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views15 pages

M-1 Python

OpenCV is an open-source computer vision library that facilitates real-time image processing and supports multiple programming languages and platforms. It provides various functionalities such as image reading, transformation, filtering, and object detection, making it widely applicable in industries like robotics and medical imaging. Key operations include image manipulation, color space conversion, and geometric drawing, all of which are essential for tasks like face recognition and self-driving car navigation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

 Introduction and Basics

1. What is OpenCV? Define its Installation and primary roles and


key capabilities in computer vision applications.
Sol:
 OpenCV stands for Open Source Computer Vision Library.
 OpenCV can be installed using the command pip install opencv-python
 For full functionality including GUI features, use pip install opencv-python-headless (for
servers) or opencv-contrib-python (for extra modules).

 Primary Role: OpenCV is a comprehensive open-source library designed for real-time


computer vision and image processing tasks.
 It enables tasks like object detection, face recognition, image transformation, and motion
tracking.

 Key capabilities:

Image Processing:
 Filtering, color conversion, transformations (resize, crop, rotate), and geometric drawing.
Integration & Speed:
 Fast numerical operations using NumPy, suitable for real-time computer vision tasks.

 It supports application in computer Vision, Machine Learning and Deep Learning


 OpenCV supports multiple programming languages like Python, C++, and Java.
 It is widely used in fields like robotics, AI, medical imaging, and augmented reality.

2. Which platforms and programming languages are supported by


OpenCV?
Soln: OpenCV is designed to be platform-independent and supports a wide range
of operating systems and programming languages.
Supported Platforms:
 Windows
 Linux
 macOS
 Android
 iOS
Supported Programming Languages:
 Python (most widely used)
 C++
 Java
 MATLAB
 JavaScript (via OpenCV.js for web applications)
This flexibility makes OpenCV ideal for deploying image processing applications on
desktops, mobile, and embedded systems.

3. How do you install OpenCV in Python using PyCharm?


Soln:
To install OpenCV in Python using PyCharm, follow these steps:
Steps:
1. Open PyCharm and go to your project.
2. Navigate to File > Settings > Project > Python Interpreter.
3. Click the "+" button to install a new package.
4. Search for opencv-python.
5. Click Install Package.

4. Name at least three real-world industry applications of OpenCV.


Soln: OpenCV is widely used across multiple industries due to its powerful
capabilities. Three common applications are:
1. Surveillance and Security:
o Motion detection, face recognition in CCTV.
o Real-time monitoring using object tracking.
2. Medical Imaging:
o Detection of anomalies in X-rays and MRIs.
o Segmenting tumors and other organs using image segmentation.
3. Automotive (Self-Driving Cars):
o Lane detection, traffic sign recognition.
o Obstacle detection and road analysis.
Example:
Tesla uses computer vision (based on OpenCV-like pipelines) for self-driving car
navigation using camera feeds.

 Image Reading and Display

5. How do you read and display an image using OpenCV in


Python?
Soln: OpenCV provides cv2.imread() for reading images and cv2.imshow() for
displaying them

Explanation:
 cv2.imread() loads the image in BGR format.
 cv2.imshow() opens a window and shows the image.
 cv2.waitKey(0) waits indefinitely for a key press.
 cv2.destroyAllWindows() closes all windows created by OpenCV.

6. What are the different flags in cv2.imread() and what does each
one do?
Soln: The cv2.imread() function in OpenCV reads an image from the disk. It accepts
an optional flag parameter that specifies how the image should be read.
 cv2.IMREAD_COLOR – Loads a color image (default).

 cv2.IMREAD_GRAYSCALE – Loads a grayscale image.

 cv2.IMREAD_UNCHANGED – Includes alpha channel if present.

 cv2.IMREAD_ANYCOLOR – Forces any color format.

 cv2.IMREAD_ANYDEPTH – Loads images with any bit depth.

7. Explain the use of cv2.imwrite() for writing and saving an


image.
Soln: cv2.imwrite() is used to save an image to disk from memory.
Explanation:
This function is useful when you want to store the processed output like filtered,
blurred, cropped, or grayscale versions of images.

8. Why does OpenCV use BGR instead of RGB? What are the
implications while accessing pixel values and manipulation?
Soln: Color Formats:
 RGB stores color in the order Red, Green, Blue.
 BGR stores color in the order Blue, Green, Red.
 Most libraries (like Matplotlib, Pillow) use RGB, but OpenCV uses BGR by default.

 Why OpenCV Uses BGR:

 It’s a legacy decision from older Windows systems and early camera APIs.
 There is no technical benefit — just a default standard OpenCV follows.

 Pixel Access Implication:

 In OpenCV, img[y, x] returns [B, G, R].


 Assuming it's [R, G, B] causes errors in color detection or filtering.

 Color Mismatch with Other Tools:

 Displaying OpenCV images using matplotlib (which expects RGB) causes wrong
colors (e.g., red becomes blue).
 To fix this, convert using:

rgb_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

 examples:

When detecting red color in an image, using incorrect channel indexing due to RGB-BGR
confusion will fail detection logic.

9. Compare cv2.COLOR_BGR2GRAY and


cv2.COLOR_BGR2HSV with suitable use cases.
Soln:
Conversion Description Use Case
cv2.COLOR_BGR2GRAY
Converts image to grayscale (single Used in edge detection,
channel) thresholding, etc.
cv2.COLOR_BGR2HSV
Converts image to HSV (Hue, Used in color filtering, object
Saturation, Value) color space tracking, masking
10. What is the process of splitting and merging color
channels using OpenCV?
Soln:
 OpenCv stores images in BGR format. To process color components separately, we
can split them.
 Use cv2.split(image) to split into B,G,R channels.
 Each channels can be processed individually (e.g. enhanced,threshold)
 To recombine them, use cv2.merge([B,G,R])
 It is useful in color-based segmentation, object tracking, or highlighting specific color
channels.

11. How can you access and modify individual pixel values
and color channels in OpenCV?
Soln: Accessing and modifying pixel values allows fine-grained image
manipulation, useful in tasks like masking, editing, or color enhancement.
12. How do you crop a region of interest (ROI) and flip an
image using OpenCV?
Soln: Cropping in OpenCV:
 Cropping refers to extracting a specific region from an image, called ROI
(Region of Interest).
 OpenCV allows cropping using slicing syntax:

 y1:y2 defines the row range (height), and x1:x2 defines the column range
(width).
 Example: cropped = img[100:300, 200:400] This extracts the portion from
y=100 to 300 and x=200 to 400.

Flipping in OpenCV:
 Flipping creates a mirror image of the input, used for transformation and
augmentation.
 OpenCV uses cv2.flip() function:
| Flip Code | Flip Direction |
| --------- | ---------------------------- |
| `0` | Vertical |
| `1` | Horizontal |
| `-1` | Both (vertical + horizontal) |
 Example: flipped = cv2.flip(img, 1) # Flips the image horizontally
 Cropping helps isolate parts of an image like faces, license plates, or objects.
 Flipping is widely used in data augmentation to increase dataset variety in
machine learning and deep learning.

13. How do you resize an image to half of its original


dimensions in OpenCV?
Soln: You can use cv2.resize() function and set the new dimensions manually or by
scaling.

14. What is the effect of changing the beta value in


cv2.convertScaleAbs() on an image's brightness?
Soln: cv2.convertScaleAbs() is used for image transformation involving scaling
(alpha) and brightness adjustment (beta).

Role of Beta Value:


 Beta is a scalar value added to every pixel in the image.
 Positive beta → increases brightness
 Negative beta → decreases brightness (darkens the image)

 Helps correct underexposed or overexposed images.


 Prepares data for better analysis in image-based machine learning models.
 Performs safe pixel conversion by converting values to unsigned 8-bit (0–255),
avoiding overflow.

15. How do you rotate an image by 90 degrees using


OpenCV?
Soln:  Use: rotated = cv2.rotate(img, cv2.ROTATE_90_CLOCKWISE)

 Alternative: Use affine transforms.

 Useful in orientation correction.

 Helps align datasets or images from cameras.

 Easy, efficient with built-in function.


16. How do you apply a Gaussian Blur to an image using
OpenCV?
Soln: cv2.GaussianBlur() is used to reduce image noise and detail by applying a
Gaussian kernel. It smoothens the image using a weighted average, giving more
weight to central pixels.

 src: Source image.

 ksize: Kernel size (must be odd, e.g., (5, 5)).

 sigmaX: Standard deviation in X direction.

Use Case:
Gaussian blur is used in:
 Noise reduction
 Preprocessing before edge detection (e.g., Canny)
 Smoothing effects in UI

17. How can you save a modified image to disk using


OpenCV?
Soln: To save an image after any modification, use cv2.imwrite().
18. How do you create a side-by-side collage of two images
using OpenCV?
Soln: You can horizontally concatenate two images using np.hstack() (horizontal
stack) or vertically using np.vstack().
19. What are some key image processing tasks you can
perform using OpenCV?
Soln:
OpenCV offers a wide range of image processing functions used in real-time
applications:
Key Tasks:
1. Image I/O:
o cv2.imread(), cv2.imwrite(), cv2.imshow()
2. Image Transformation:
o Resizing, cropping, rotating, flipping
3. Color Space Conversion:
o cv2.cvtColor() (e.g., BGR ↔ GRAY ↔ HSV)
4. Filtering and Blurring:
o cv2.GaussianBlur(), cv2.medianBlur()
5. Edge Detection:
o cv2.Canny()
6. Geometric Drawing:
o cv2.line(), cv2.rectangle(), cv2.circle()
7. Thresholding and Masking:
o cv2.threshold(), cv2.inRange()
8. Morphological Operations:
o cv2.erode(), cv2.dilate()
9. Feature Detection and Tracking:
o Contours, keypoints, object tracking
10. Video Processing:
 Frame reading, writing, real-time tracking

Use Cases:
 Face detection
 Object tracking
 OCR (Optical Character Recognition)
 Image classification (as preprocessing)

20. How can you create a black image and draw basic
geometric shapes on it using OpenCV? Explain with code.
Soln: To create a black image and draw geometric shapes using OpenCV, we follow
two main steps:
Step 1: Create a Black Image
A black image is simply an array of zeros using NumPy with shape (height, width, 3)
where each pixel's BGR value is (0, 0, 0):
Step 2: Draw Geometric Shapes
Use OpenCV’s drawing functions to draw shapes on this black image:

Function Purpose
cv2.line() Draws a line between two points
cv2.rectangle() Draws a rectangle using two corners
cv2.circle() Draws a circle (can be filled)
cv2.ellipse() Draws an ellipse arc

Use Cases:
 Visualizations in GUIs
 Annotating images in object detection
 Generating test patterns for computer vision

21. Converting a color image to graysacle using open cv


Soln: In OpenCV, converting a color image (BGR format) to grayscale simplifies
the image by removing color information, keeping only intensity, which is often
essential for further image processing tasks like edge detection, thresholding, and face
detection.

Explanation:
 In grayscale images, each pixel is a single intensity value from 0 (black) to 255
(white).
 The grayscale image reduces storage space and simplifies processing.

Use Cases of Grayscale Conversion:


1. Preprocessing for edge detection (e.g., Canny).
2. Image thresholding (binary segmentation).

22. Loading and Cropping Region of Interest (ROI) from an


Image Using OpenCV
Soln: In OpenCV, a Region of Interest (ROI) refers to a selected rectangular
portion of an image that you want to focus on or process separately. You can load an
image using cv2.imread() and crop the ROI using NumPy slicing.
Explanation:
 The image is treated as a NumPy array: image[rows, columns] = image[y1:y2, x1:x2]
 ROI cropping helps you isolate a specific part of the image (like a face, object, or
text).
 You can then apply filters, OCR, object detection, or saving only the ROI.
Use Cases:
1. Face Detection: Cropping face region from detected coordinates.
2. License Plate Recognition: Extracting just the number plate area.

You might also like