Skip to content

Yashs744/OpenCV-Examples

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenCV-Examples

Examples of OpenCV with Python

1. Basic Image Operation: Load and Display Image (Example 1)

  • Load an Image in Memory using cv2.imread(image_path, format) function. Format take value of IMREADMODES. Avilable Modes to read an image are: cv2.IMREAD_COLOR, cv2.IMREAD_UNCHANGED, cv2.IMREAD_GRAYSCALE, cv2.IMREAD_ANYCOLOR etc.
  • Display the Image Loaded using cv2.imshow(Message, image) function.
  • After displaying the image wait for user to press a key to terminate. cv2.waitKey(0)

Methods Used:

  • cv2.imread()
  • cv2.namedWindows()
  • cv2.imshow()
  • cv2.waitKey()

Output:

UnChanged Image GrayScale Image

2. Basic Image Operation: Tranforming (or Modifiying) Image i.e from BRG-to-GrayScale (Example 2)

  • Modify the Image from BGR Channel to GrayScale Channel using cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).
  • Write the new Image to Disk using cv2.write(name, image)

Methods Used:

  • cv2.cvtColor()
  • cv2.COLOR_BGR2GRAY
  • cv2.resize()
  • cv2.imwrite()

Output:

UnChanged Image

3. Mask Operation on Image (Example 3)

  • Enhance the Image by Manipulating each pixel using the formula
    pixel_value = 5 * IMG(j, i) - [IMG(j - 1, i) + IMG(j + 1, i) + IMG(j, i - 1) + IMG(j, i + 1).
  • Another way to enhance the image is correlating the Image with a Kernel using cv2.filter2D(img, ddpeth, kernel).

Methods Used:

  • Manual Manipulation of Pixels using NumPy
  • cv2.filter2D()

Output:

Original Image Manual Manipulation Image Filter2D Image

4. Image Operation (Example 4)

Methods Used:

  • cv2.cvtColor()
  • cv2.Sobel()
  • cv2.convertScaleAbs()

Output:

Image

5. Blending Two Images (Example 5)

  • Blend two Images together using Linear Blend i.e equation
  • cv2.addWeigthed(array_1, alpha, array_2, beta, gamma) is used to blend the two images together (gamma = 0)
python (or python3) --image_1 "my_images/TG.jpg" --image_2 "my_images/TokyoGhoul.jpg"

Methods Used:

  • cv2.addWeighted()

Output:

Image

6. Image Processing: Contrast and Brightness

  • Image Processing is to pre-process a image and convert it into a suitable image or in other words take a image (or multiple) image as n input and perform some operation to give output image (or images).
    Example: Color Correction, Hue Adjustment, Contrast and Brightness Control.
  • Point Operators (or Point Processes) is one of the Image Processing method where each pixel is manipulated independently from it's neighboring pixels. Formula:

    equation

    where alpha and beta are used to control contrast and brightness respectively.

Methods Used:

  • cv2.convertScaleAbs()
  • NumPy clip() for manual manipulation

Output:

Image

7. Image Processing: Gamma Correction (Example 7)

Formula

Methods Used:

  • NumPy Array
  • cv2.LUT()
  • cv2.convertScaleAbs()

Output:

Image

8. Image Processing: Fetching Objects from Images based on Color (Example 8)

Methods Used:

  • cv2.cvtColor()
  • np.uint8()
  • cv2.inRange()
  • cv2.GaussianBlur()
  • cv2.bitwise_and()

Output:

Image Image

9. Image Processing: Smoothing (or Bluring) an Image using Filters.

Smoothing or Bluring of an Image is a common task in Image Processing which is performed to reduce the noise from the image using filters.
The most common type of filter is Linear Filter where output pixel's value is calculated using the weighted sum of the input pixel value.

where, h(k,l) is a kernel which is nothing but the filter coefficients.

Types of Filters:

  • Normalized Box Filter

    box_filter

    Here, each output pixel is the mean of the kernel neighbours. One such filter is Box Blur or Box Linear Filter.

    box_blur

  • Gaussian Filter
    The Gaussian Filter works by convolving each pixel in the input image with a Gaussian Function and then summing them up together.

    Gaussian Function:
    1d and 2D

  • Median Filter<br? It works by traversing through each pixel in the input image and replacing the pixel with the median of its neighbouting pixels.

  • Bilateral Filter
    It is an non-linear, edge-preserving and noise reducing filter i.e apart from reducing the noise from the image it also preserves the edges of the image i.e doesnot smooth out the edges rather replaces the intensity of each pixel with a weighted average of intensity value from the nearby pixels.

Methods Used:

  • cv2.blur()
  • cv2.GaussianBlur()
  • cv2.medianBlur()
  • cv2.bilateralFilter()

Output:

output

10. Morphological Image Processing

Morphological Image Processing is a collection of non-linear operations related to the shape of morphological features in the image. Morphological Operations usually works on Binary Images as it only rely on the relative ordering of pixel values and not on thier numerical values but they can also be applied to GreyScale Images.
Morphological Operatations uses an Structuring Element which is nothing but a small binary image (matrix) of pixels each with a value of 0 or 1. This Structuing Element (s) is applied to an input image (f) to get a output (g).

The two most common Morphological Operations are:

  • Dilation The Operation consist of colvolving an image (f) with some structuring element (s). The Structuring Element (s) has a defined anchor point (or origin) which is usually at the center. As, (s) moves over the image (f), the maximum pixel overlapped by (s) is computed and the image pixel value is replaced by this maximum value. This operation grows the bright region in the image.

  • Erosion Erosion is in a way opposite of Dilation as it calculates the minimum over the area of the given structuring element (s). It shrinks the bright region in the image.

Methods Used:

  • cv2.getStructuringElement()
  • cv2.erode()
  • cv2.dilate()
  • cv2.putText()

Output:

Structuring Element (s):

  • Shape: cv2.MORPH_RECT
  • Size: (3, 3)

result_10(rect)

Structuring Element (s):

  • Shape: cv2.MORPH_CROSS
  • Size: (3, 3)

result_10(rect)

Structuring Element (s):

  • Shape: cv2.MORPH_ELLIPSE
  • Size: (3, 3)

result_10(rect)


Releases

No releases published

Packages

No packages published

Languages