6

A Quick Guide to Image Processing in Computer Vision Using OpenCV

 3 years ago
source link: https://hackernoon.com/a-quick-guide-to-image-processing-in-computer-vision-using-opencv-igb233gc
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A Quick Guide to Image Processing in Computer Vision Using OpenCV

@velocityshouvikShouvik

Enthusiastic about tech, Exploring and building tech hacks

We as humans can see and interpret images using our visual system and remember using our memory, but how do computers remember and interpret images?

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Machines store images in the form of square boxes which contain numbers and each square box is called a pixel. The values of each pixel together make an image. The numbers in square boxes are stored in the form of a matrix of numbers. The size of the matrix depends upon the size of the image (n x m) which refers to the number of pixels in an image.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Images are classified into two types

  1. Grayscale images
  2. Color Images

OpenCV: The image processing library which stands for Open-Source Computer Vision Library was invented by intel in 1999 and written in C/C++. The library's function is to perform image processing jobs such as Resizing, Blurring, sharpening, transformations, etc.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

By using OpenCV we perform image processing and pre-processing of data. By performing certain operations on images, we are able to extract information from the images for intended objectives such as edge detection or Face Recognition.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Image Processing Using OpenCV

Importing an image: The foremost basic task in image processing in OpenCV is to import an image in python.

0 reactions
heart.png
light.png
thumbs-down.png
money.png
Img=cv.imread(‘image.jpg’)
cv.imread(‘Image_tag’,Img)

Images in different color spaces:

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Color spaces: Representation of an image using the different color combinations of the OpenCV library.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

A Grayscale scale image is one channel in which values range from (0–255) lowest value(0) represents dark and the higher value(255) represents brightness, the size of the Grayscale image is represented as n*m.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

To convert images into grayscale images:

0 reactions
heart.png
light.png
thumbs-down.png
money.png
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv.imshow(‘Image’,img_gray) 
cv.waitKey(0) cv.destroyallWindows()

A color image is an image which is made of three color channels RGB(Red, Green, Blue), the size of the color image is represented as n*m*3, where n and m are rows, columns and ‘3’ denotes the three color channels RGB(Red, Green, Blue)

0 reactions
heart.png
light.png
thumbs-down.png
money.png

There are color spaces such as HSV, which stands for hue, saturation, and value, HSL stands for hue, saturation, and lightness. 

0 reactions
heart.png
light.png
thumbs-down.png
money.png

HSV indicates the value of the color, while HSL is the amount of light. Along the angles from the center axis, there is hue, the actual colors. And the distance from the center axis belongs to saturation.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Geometrics in Images: In image processing, we may explicitly need to draw shapes in images to highlight any specific information in an image by a rectangle, circle, a line and we can also write text explicitly to denote or specify something.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

RECTANGLE: To draw a rectangle we use the function cv.rectangle() and the parameters needed to be mentioned: image, coordinate of top-left corner, coordinate of the bottom-right corner, color of rectangle, thickness of rectangle.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Circle: To draw circles, we use the function cv.circle(): mention the image on which to draw circle, coordinate of the point, radius, color of the circle, thickness of the circle.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Line: To draw a line of any length we will use the function cv.line() in which we pass the parameters: image on which to draw line, starting coordinate, ending coordinate, color of the line, thickness of the line.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Text: In case of adding text to images, we will use the function cv.putText(), and mention the parameters: image on which to write, ‘The message’, Coordinate of text in image, font type, font scale, colour of text, thickness, line type

0 reactions
heart.png
light.png
thumbs-down.png
money.png

<Code Link given below>

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Image transformations using OpenCV

Rescaling or Resizing Frames: Images are of different resolution with varied sizes some are large and some are small, but we may need to resize an image either larger or smaller accordingly.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

The frame of the video or image can be resized into any size by rescaling explicitly using the OpenCV library function cv2.resize() and mentioning parameters: the image, width, height of the image, interpolation method for zooming or shrinking.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Translation:The method of shifting an image i.e., in leftmost, rightmost, upward, downward of frame of the image window. We need to create a transformation matrix and pass it to the function cv2.warpAffine with the image and with the dimensions of the image.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Rotation: In OpenCV, rotation of an image can be done using the function warpAffine() which takes the image, the rotation matrix, and the dimension. The Rotation matrix needs the center of the image, the angle of rotation, and the scaling value.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

<Code Link given below>

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Extraction of information by image processing techniques

Kernel: Extraction of information from images requies functions to do so. A kernel is made up of small 2-dimensional matrices of numbers, which act as an operation on pixels of the image.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

The kernel computes the values of the surrounding pixel neighborhood's. To determine the value for the centre pixel, the kernel repeats the process until it finishes scanning the whole target image and at last results in a blurred, sharpened image of the original image.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

The values of the convoluted image pixel are the result of the weighted sum of all the pixels in the neighborhoods. The new values of the image are formed from the sum of the elements resulting from the element-wise multiplication of two matrices.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Smoothing: Removing of noises from the images i.e., the high frequency content of the image using a low-pass-filters (LPF) by averaging the pixel values with a help of a kernel, resulting in a blurred image.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

We can perform blurring using the Gaussian Blur function

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Sharpening: To discover edges in an image we use sharpening to help computers differentiate between foreground and background in images, and extract edges to detect objects accurately. Edges in the images are sharp changes of brightness curves.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

We can detect edge features in an image using the Canny function in the OpenCV library.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Thresholding: Binarization of images into either black as background and white as foreground vice versa by using thresholding parameters.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

In simple words, when the pixel value is determined to be greater than the set threshold value, the pixel is assigned to whiten or darken.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

In the cv.threshold() function the arguments are: source image, value of threshold, value to assign for above threshold value, types of thresholding.

0 reactions
heart.png
light.png
thumbs-down.png
money.png
  1. · cv2.THRESH_BINARY : 
  2.  ·cv2.THRESH_BINARY_INV : 
  3.  ·cv2.THRESH_TRUNC : 
  4. cv2.THRESH_TOZERO:
  5. ·cv2.THRESH_TOZERO_INV:

Adaptive Thresholding In OpenCV thresholding can be used for varied image types. Calculating thresholds for small regions of the images can give us better results in extracting features of varying illumination. In the cv.adaptiveThreshold() function arguments passed are: source img, ,ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY, block size, Constant.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

ADAPTIVE_THRESH_MEAN_C: threshold value is the mean of the neighborhood area.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

ADAPTIVE_THRESH_GAUSSIAN_C: threshold value is the weighted sum of neighborhood values where weights are a gaussian window.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Histogram Computation: Histogram is a graph which gives the measure of the intensity distribution on an image. The graph value ranges from 0 to 255 along the X-axis and the number of pixels in the image along the Y-axis.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

We can calculate histogram in OpenCV by the cv2.calcHist() function. The parameters for the function are: 

0 reactions
heart.png
light.png
thumbs-down.png
money.png
cv.calcHist(images, channels, mask, histSize, ranges[0,256])

Image: the source image of type uint80r float32 in brackets, because it is a list of images.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Channels: colour images have three channels, channels indicate for which we want to calculate histogram [0],[1],[2] indicates histogram for R,G,B. For Grayscale its value is [0]

0 reactions
heart.png
light.png
thumbs-down.png
money.png

mask: To find the histogram of a particular region of the image, otherwise the histogram for a full image is NONE.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

histSize: Representation of BIN count(pixel interval), number of pixels in each interval.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

ranges: Values which represent pixel range; it is[0,256].

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Masking: Determination of pixel intensities at any particular region of the image is possible, the area of interest can be extracted explicitly using Bitwise operations.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Bitwise Operations refers to bitwise AND, OR, NOT and XOR operations. The size of the blank image and the source image needs to same to perform bitwise operation.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Create a mask: To perform masking over a region we need to make a circle/rectangle over a blank image (refer on how to geometric in blank images).

0 reactions
heart.png
light.png
thumbs-down.png
money.png
0 reactions
heart.png
light.png
thumbs-down.png
money.png
#create a mask
mask = np.zeros(img.shape[:2], np.uint8)
circle=cv.circle(blank,(img.shape[1]//2,img.shape[0//2,
masked_img = cv2.bitwise_and(img,img,mask = mask)

Code Link: </>

0 reactions
heart.png
light.png
thumbs-down.png
money.png

Summary:

These are some of the basic image processing techniques using which we can extract information from images and so machines can able to interpret images and perform applications like face detection, gesture recognition, etc.

0 reactions
heart.png
light.png
thumbs-down.png
money.png

I hope you find the tutorial informative and practical in your journey to learn image processing.

0 reactions
heart.png
light.png
thumbs-down.png
money.png
3
heart.pngheart.pngheart.pngheart.png
light.pnglight.pnglight.pnglight.png
boat.pngboat.pngboat.pngboat.png
money.pngmoney.pngmoney.pngmoney.png
Share this story
Read my stories

Enthusiastic about tech, Exploring and building tech hacks

Join Hacker Noon

Create your free account to unlock your custom reading experience.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK