Working with images using OpenCV3
The following is all about image processing, where you need to modify the image, such as using an artistic filter, some parts of the extrapolation (extrapolate), splitting, pasting, or other required operations.
Conversion of different color spaces
There are hundreds of ways to convert between different color spaces in OpenCV. Currently, there are three commonly used color spaces in computer vision: Grayscale, BGR, and HSV (Hue,saturation,value)
1. The gray color space is converted to gray scale by removing color information, and the gray color space is particularly effective for intermediate processing, such as face detection.
2.BGR, that is, the blue-green-red color space, each pixel is represented by a ternary array, representing the blue, green, red three colors. Web developers may be familiar with another color space similar to this: RGB, which is only different in the order of colors.
3.hsv,h (hue) is the hue, S (saturation) is the saturation, V (Value) indicates the degree of darkness (or the brightness of the other end of the spectrum)
Fourier transform
In OpenCV, most processing of images and videos involves the concept of Fourier transform in more or less terms. Fourier observes that all waveforms can be superimposed by a series of simple sine curves with different frequencies.
In other words, the waveforms that people see are superimposed by other waveforms. This concept is also useful for manipulating images, because we can distinguish which areas of the image are particularly strong in terms of the signal (pixels), which are not so strong, and can be arbitrarily labeled with noise areas, areas of interest, foreground and background, etc. The original image is made up of many frequencies that allow people to separate these frequencies to understand the image and extract the data of interest
Note: In the OPENCV environment, there are many algorithms implemented that allow us to manipulate images and understand the meanings contained in images. These algorithms are also implemented in NumPy and are easier to use. The NumPy has a fast Fourier transform (FFT) package that contains the fft2 () function, which calculates the discrete Fourier transform (DFT) of an image
The amplitude spectrum of the image is introduced by Fourier transform. The amplitude spectrum of an image is another image, and the amplitude spectral image presents a representation of the original image in terms of change: the brightest pixel in an image is placed in the center of the image, then gradually darkens, and the pixels on the edge are darkest. This will reveal how many bright pixels and dark pixels are in the image, as well as the percentages of their distributions.
The concept of Fourier transforms is the basis for many common image processing operations, such as edge detection or line segment and shape detection.
1. High-pass filter
A high-pass filter (HPF) is a filter that detects an area of an image and then increases (boosts) the luminance of that pixel based on the luminance difference between the pixel and the surrounding pixels.
A nucleus is a set of weights that is applied to an area of the source image and thus generates a pixel of the target image. For example, a kernel of size 7 means that pixels per 7x7 source image produce one pixel of the target image. The nucleus can be thought of as a piece of frosted glass that can be moved on the source image, and the light in the covered area of the glass will be diffused and mixed in some way.
After the sum of the luminance difference between the central pixel and the surrounding neighboring pixels is computed, the luminance of the central pixel increases (or vice versa) if the brightness varies greatly. In other words, if a pixel is more prominent than the pixels around it, it will increase its brightness
This is particularly useful for edge detection, which uses a high-pass filter called a HF boost filter.
Both high-and low-pass filters have a property called radius, which determines how much area of neighboring pixels participates in the filtering operation
2. Low-pass Filter
A low-pass filter (LPF) smooths the luminance of a pixel when the luminance difference between pixels and surrounding pixels is less than a specific value. It is mainly used for denoising and blurring, for example, Gaussian Blur is one of the most commonly used fuzzy filters (smoothing filters), which is a low-pass filter that weakens the high-frequency signal strength.
Edge detection
The edge plays an important role in both human visual and computer vision. Humans can identify the type and posture of an object with only one background silhouette or a sketch.
The OPENCV provides a number of edge detection filters, including Laplacian (), Sobel (), and ScHARR (). These filter functions convert the non-edge area to black and the edge area to white or other saturated colors. However, these functions are easy to identify the noise incorrectly as an edge. The way to alleviate this problem is to blur the image before finding the edge. OPENCV also provides a number of fuzzy filter functions, including blur () (simple arithmetic averaging), Medianblur (), and Gaussianblur (). There are many parameters of edge detection filter function and fuzzy filter function, but there is always a ksize parameter, it is an odd number, which indicates the width and height of the filter core.
Here, Medianblur () is used as a fuzzy function, which is very effective for removing digitized video noise, especially the noise of color images, and using Laplacian () as The edge detection function, he produces obvious edge lines, especially for grayscale images. After you use the Medianblur () function, you need to convert the image from the BGR color space to a grayscale color space before you will use the Laplacian () function.
Convolution with custom kernels
Many of the OPENCV pre-defined filters (filter functions) will use cores. The kernel, in fact, is a set of weights that determines how new pixels are calculated by neighboring pixel points. A nucleus is also called a convolution matrix, which reconciles (mix up) or convolution operations on a region's pixels. Usually a kernel-based filter (filter function) is also called a convolution filter (filter function).
OPENCV provides a very versatile filter2d () function that uses arbitrary kernel or convolution matrices specified by the user. In order to understand how this function is used, we first understand the format of the convolution matrix. The convolution matrix is a two-dimensional array with odd rows, odd columns, the center element corresponds to the pixel of interest, and the other elements correspond to neighboring pixels around the pixel, each element has an integer or floating-point value, which is the weight applied to the pixel value.
Cv2.filter2d (src,-1, KERNEL,DST)
The second parameter specifies the bit depth for each channel of the target image (for example, bit depth cv2. CV_8U indicates that each channel is 8 bits, and if it is negative, the target image and the source image have the same bit depth.
Note: For color images, filter2d () uses the same kernel for each channel. If you want to use a different kernel for each channel, you must use the split () function and the merge () function.
Sharp, edge detection, and blur filters all use highly symmetrical cores. But sometimes asymmetric nuclei get some interesting results.
Canny edge Detection
OpenCV also provides a very handy canny function, which is very popular, not only because of its effect, but also because it is very simple to implement in a OPENCV program.
Import Cv2import NumPy asnpimg=cv2.imread ("1.jpg",0) Cv2.imwrite ("canny.jpg", Cv2. Canny (IMG, $, -)) Cv2.imshow ("Canny", Cv2.imread ("canny.jpg")) Cv2.waitkey () cv2.destroyallwindows ()
The canny edge detection algorithm is very complex, but also interesting: it has 5 steps, namely using a Gaussian filter to de-noising the image, calculate gradients, use a non-maximum suppression (NMS) on the edge, use a double threshold on the detected edge to remove false positives, and finally analyze all the edges and the connections between them. To preserve the true edges and eliminate the edges that are not noticeable.
Contour Detection
In computer vision, contour detection is another important task, not only for detecting the contours of objects in images or video frames, but also for other operations related to contour detection. These operations include calculating polygon boundaries, shape approximation cores, and calculating areas of interest. This is a simple operation when interacting with image data, because rectangular areas in NumPy can be defined using array slices (slice). This technique is used extensively when describing the concept of object detection (including human faces) and object tracking.
Outlines of bounding boxes, minimum rectangular areas, and minimum closed circles
Finding a square contour is simple, finding irregular, skewed, and rotated shapes using OpenCV's cv2.findcontours function, which gives you the best results.
Realistic applications are particularly interested in the bounding box of the target, the minimum rectangular area, and the smallest closed circle. Combining the Cv2.findcontours function with a small amount of OPENCV functionality makes it very easy to implement these features:
Import Cv2import NumPy asnpimg=cv2.pyrdown (Cv2.imread ("1.jpg", Cv2. imread_unchanged)) Ret,thresh=cv2.threshold (Cv2.cvtcolor (Img.copy (), Cv2. Color_bgr2gray),127,255, Cv2. thresh_binary) Img,contours,hier=cv2.findcontours (Thresh,cv2. Retr_external,cv2. Chain_approx_simple) forCinchcontours:x,y,w,h=Cv2.boundingrect (c) #计算出一个简单地边界框 #这个操作很简单, which converts contour information to (x, y) coordinates, plus a rectangular height and width. Cv2.rectangle (IMG, (x, y),+W,Y+H), (0,255,0),2) #画出该矩形 rect=Cv2.minarearect (c) #计算出包围目标地最小矩形区域 box=cv2.boxpoints (rect) box=np.int0 (Box) #这里用到一种非常有趣的机制: OpenCV no function can calculate the coordinates of the smallest rectangle vertex directly from the contour information. So you need to calculate the minimum rectangular area and then calculate the vertex of the rectangle. Note that the computed vertex coordinates are floating point, but the coordinate values of the resulting pixels are integers (they cannot get part of the pixel), so a conversion is required. Then draw this rectangle, which can be implemented by the Cv2.drawcontours function cv2.drawcontours (Img,[box],0,(0,0,255),3) (x, y), radius=cv2.minenclosingcircle (c) #返回一个二元组, the first element is the coordinate of the center of the Ganso, the second element is the radius value of the Circle center=(int(x),int(y)) Radius=int(RADIUS) img=cv2.circle (Img,center,radius, (0,255,0),2) cv2.drawcontours (img,contours,-1,(255,0,0),1) Cv2.imshow ("Contours", IMG) cv2.waitkey () cv2.destroyallwindows ()
OpenCV3 Computer Vision +python (iii)