Getting Started: Image Processing with OpenCV

introduce

Image processing is a branch of computer vision that uses various algorithms to process and analyze digital images. It involves the use of mathematical or statistical operations to modify images for many applications, including but not limited to medical and satellite imagery and digital photography. This article explores the basics of image processing and some of the techniques used in this field.

Table of contents

  1. Image processing basics

  2. Image processing applications

  3. Image preprocessing technology

  4. Apply some tips

  5. in conclusion

Image processing basics

Digital images are made up of pixels, which are small boxes that represent the color and brightness values ​​of that point in the image. Image processing involves processing these pixels in the desired way to achieve the desired effect in the image. Most of the common operations performed on digital images include filtering, enhancement, restoration, etc.

Filtering is the process of removing unwanted noise from an image. This is done by applying a filter that adjusts the pixel values ​​of the image. Depending on the type of filter, they can be used in a wide range of applications. They can be designed to remove specific types of noise, such as Gaussian, salt and pepper, or speckle noise. Filters that help remove the above noise include median filters, mean filters, and Gaussian filters.

495f7c458842aecc0402abc95cdc49e8.png

Enhancement is a process that improves image quality. This is done by modifying the brightness or contrast of the image. These techniques may be as simple as using histograms to adjust brightness and contrast, or they may be more complex, such as using algorithms to enhance edges and texture in images.

14ea4ba97d4f29fce659d529a5660a47.png

Restoration is the process of restoring an image, some noise or other artifacts may degrade the image quality. These techniques involve using mathematical methods to estimate the original image from a corrupted version. It is done using techniques such as deconvolution (used to obtain the original image from a blurred version) or denoising (used to remove noise from the image).

4c325d94319e404f1656a4fa69617870.png

Image preprocessing is very useful to improve image quality, thereby aiding analysis and further processing. Some powerful image preprocessing techniques include noise reduction, contrast enhancement, image resizing, color correction, segmentation, feature extraction, etc. It is an essential step in image analysis, helping to enhance the data in the image and reduce clutter.

As technology continues to advance, image processing is becoming more and more important in our daily lives.

Image processing applications

Image preprocessing is a critical step when processing image data. Best results are obtained when image preprocessing is performed in accordance with the application in question. It is used in various fields:

  • Medical imaging, improving the quality of medical images and making it easier to detect diseases or abnormalities

  • Object recognition in images, such as identifying faces or license plates in surveillance videos

  • Object detection, i.e. mainly used in self-driving cars to better navigate the roads and avoid accidents

  • Satellite imagery uses image processing to improve image quality for weather forecasts, maps, and more

Image preprocessing technology

The choice of technique depends on the nature of the image and the application. Here are some tips to improve image quality and usability:

  • Noise Reduction: Noise in images can be caused by a variety of factors, such as low light, sensor noise, and compression artifacts. Noise reduction techniques aim to remove noise from images while retaining their essential features. Some common noise reduction techniques include Gaussian smoothing, median filtering, and wavelet denoising.

  • Contrast enhancement: Contrast enhancement technology aims to increase the contrast of an image, making it easier to distinguish different image features. These technologies can be used in applications such as medical imaging and surveillance. Some standard contrast enhancement techniques include histogram equalization, adaptive histogram equalization, and contrast stretching.

  • Image resizing: Image resizing technique is used to resize images. You can resize to make an image smaller or larger or change its aspect ratio. Some typical image resizing techniques include nearest neighbor interpolation, bilinear interpolation, and bicubic interpolation.

  • Color Correction: Color correction technology is used to adjust the color balance of an image. Color correction is important in applications such as photography, where the color accuracy of images is critical. Some common color correction techniques include gray world assumption, white balance, and color transfer.

  • Segmentation: Segmentation technique is used to divide an image into multiple regions based on its content. Segmentation is helpful in applications such as medical imaging, where specific structures or organs must be separated from the image. Some standard segmentation techniques include thresholding, edge detection, and region growing algorithms.

  • Feature Extraction: Feature extraction technology is used to identify and extract relevant features from images. These features can be used in object recognition and image classification applications. Some standard feature extraction techniques include edge detection, corner detection, and texture analysis.

Apply some tips

Here are some image processing techniques involving grayscaling, thresholding, noise reduction using median and Gaussian filters, histogram visualization before and after thresholding, and canny edge detection applied to sample images.

# Sample downloaded image
import cv2
import matplotlib.pyplot as plt

pic1 = plt.imread('download.jpg')
plt.imshow(pic1)
9fd38fb966ad14abc10a07162b4bd9c7.png
#Converting the sample image to grayscale

img = cv2.cvtColor(pic1, cv2.COLOR_BGR2GRAY)
plt.imshow(img,cmap='gray')
7ae34d5ac4eef5f53b9688b7bf75ed15.png

Thresholding: Binary threshold output contains only black and white colors. It maps all values ​​greater than the threshold to white and values ​​less than the threshold to black.

#Thresholding: try playing with the threshold value (144 here) to see the changes

ret, thresh1 = cv2.threshold(img, 140, 255, cv2.THRESH_BINARY)
plt.imshow(thresh1,cmap='gray')
2b3557eabbc51a3d05659010df4b25d1.png

Noise reduction: Filtering is usually performed based on the nature of the noise. Here, since we do not know the nature of the noise that may be present, we try to apply median and Gaussian filters.

#Median filter

img = cv2.cvtColor(pic1, cv2.COLOR_BGR2GRAY)
median = cv2.medianBlur(img,5)
plt.figure(figsize=(16, 16))
plt.subplot(121),plt.imshow(img,cmap = 'gray')
plt.title('Noisy Image')
plt.subplot(122),
plt.imshow(median,cmap = 'gray')
plt.title('Median filter')
plt.show()
d542d369847a420aa4a49cf553d1f7f1.png
gaussian_blur1 = cv2.GaussianBlur(img,(5,5),2,cv2.BORDER_DEFAULT)
gaussian_blur2 = cv2.GaussianBlur(img,(5,5),7,cv2.BORDER_DEFAULT)

plt.figure(figsize=(20, 20))
plt.subplot(1,3,1),plt.imshow(img,cmap = 'gray')
plt.title('Noisy Image')

plt.subplot(1,3,2),
plt.imshow(gaussian_blur1,cmap = 'gray')
plt.title('smoothing with Gaussian sigma=2')

plt.subplot(1,3,3),
plt.imshow(gaussian_blur2,cmap = 'gray')
plt.title('smoothing with Gaussian sigma=7')

99bea7f3b6d01db23c02509d5c01d1c8.png

Otsu Threshold: Here we do not specify the threshold that maps values ​​to black and white. The Otsu algorithm uses histograms to estimate what threshold is best for a given image and is therefore more useful.

#Otsu's thresholding before and after Gaussian filtering
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
plt.imshow(th2,cmap='gray')
plt.imshow(th3,cmap='gray')
ddcbc176b553a67bc70555a3d27f12ac.png

26aed0b3767cdb7ca9a39bdbbc696e7b.png

A histogram is a visual representation of the number of pixels for each image intensity value. The changes in the histogram before and after applying thresholding to the original and filtered images are shown below.

plt.figure(figsize=(16,16))
ret1,th1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
# Otsu's thresholding
ret2,th2 = cv2.threshold(img,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(img,(5,5),0)
ret3,th3 = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# plot all the images and their histograms
images = [img, 0, th1,
          img, 0, th2,
          blur, 0, th3]
titles = ['Original Noisy Image','Histogram','Global Thresholding (v=127)',
          'Original Noisy Image','Histogram',"Otsu's Thresholding",
          'Gaussian filtered Image','Histogram',"Otsu's Thresholding"]
for i in range(3):
    plt.subplot(3,3,i*3+1),plt.imshow(images[i*3],'gray')
    plt.title(titles[i*3]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+2),plt.hist(images[i*3].ravel(),256)
    plt.title(titles[i*3+1]), plt.xticks([]), plt.yticks([])
    plt.subplot(3,3,i*3+3),plt.imshow(images[i*3+2],'gray')
    plt.title(titles[i*3+2]), plt.xticks([]), plt.yticks([])
plt.show()

dec943b98df33b39582ebd39545c3128.png

Canny edge detection: It is built on the Sobel filter and essentially works when calculating the image intensity gradient of each pixel of the image, because in the image edges , the gradient is greatest when the color changes rapidly.

#Hough Line Transform
dst = cv2.Canny(img, 50, 200, None, 3)
lines = cv2.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)
# Draw the lines
if lines is not None:
    for i in range(0, len(lines)):
        rho = lines[i][0][0]
        theta = lines[i][0][1]
        a = math.cos(theta)
        b = math.sin(theta)
        x0 = a * rho
        y0 = b * rho
        pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
        pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
        cv2.line(cdst, pt1, pt2, (0,0,255), 3, cv2.LINE_AA)
cdst = cv2.cvtColor(dst, cv2.COLOR_GRAY2BGR)
plt.imshow(cdst)
ca29f361ff5a6af5b711a4875f96c7da.png

in conclusion

This article will guide you through the first few steps of image processing. It summarizes some applications used in image processing. It is designed to familiarize you with some of the techniques used in the field and their applications.

Key points of this article:

  • Image processing is an important step in improving image quality.

  • A wide range of applications include medical, satellite, object detection and recognition.

  • Filters can help remove noise from images

  • The gradient of an image helps detect edges in the image

☆ END ☆

If you see this, it means you like this article, please forward it and like it. Search "uncle_pn" on WeChat. Welcome to add the editor's WeChat "woshicver". A high-quality blog post will be updated in the circle of friends every day.

Scan the QR code to add the editor↓

73d847b97ae112fb9a0dea5a40119665.jpeg

Guess you like

Origin blog.csdn.net/woshicver/article/details/130313108