python + opencv machine vision based techniques (edge detection, image filtering, edge detection operator, projection, character segmentation plate)

  Machine vision is a fast-growing branch of artificial intelligence. Briefly, machine vision is to use machines instead of human eyes do measure and judge. It is an integrated technologies, including image processing, mechanical engineering, control the illumination light source, imaging optics, sensors, analog and digital video technology, computer software and hardware (image enhancement and analysis algorithms, graphics card, I / O cards Wait).

  Here are some basic machine vision, technology is used python + opencv. python is a very convenient high-level programming language, less code, and OpenCV is based on a BSD license issued cross-platform computer vision library that can run on Linux, Windows, Android and Mac OS operating systems. It is lightweight and efficient - consists of a series of C functions and a small amount of C ++ classes, while providing an interface Python, Ruby, MATLAB language, etc., many common algorithms implemented image processing and computer vision.

A: edge detection

  In machine vision, it is a very basic image processing operations, but there is a very important knowledge is the edge extraction in image processing. Edge detection, digital image processing means for processing a contour image. For a boundary, gray value where more intense, is defined as an edge. That is the turning point, inflection point refers to a function of changes in the occurrence of irregularities. And a high number of derivatives are linked, the edge of a specified objects to be extracted. But with python opencv can easily perform edge extraction operations +.

  Proceed as follows:

1. Thresholding the image and the inverse color

  We first need to create a python file, import cv2 library (OpenCV2 the python library), and displays a picture code:

import cv2

# 读取本相对路径下的initial.bmp文件
image = cv2.imread ("initial.bmp")  
# 将image对应图像在图像窗口显示出来
cv2.imshow('initial',image)
# waitKey使窗口保持静态直到用户按下一个键
cv2.waitKey(0)

  The image segmentation threshold value, the threshold value is set to 80, to obtain a grayscale image binarized, the symbol:

# 对图像进行阈值分割,阈值设定为80,得到二值化灰度图
ret,image1 = cv2.threshold(image,80,255,cv2.THRESH_BINARY)
cv2.imshow('grayscale',image1)

  The anti-color image, as follows:

image2 = image1.copy()		# 复制图片
for i in range(0,image1.shape[0]):	#image.shape表示图像的尺寸和通道信息(高,宽,通道)
for j in range(0,image1.shape[1]):
	image2[i,j]= 255 - image1[i,j]
cv2.imshow('colorReverse',image2)

2. Edge Detection

  The following is the edge extraction, edge detecting method or a Canny Movies findContours difference method, with the contracted image by subtracting the original image corrosion, edge extraction. code show as below:

# 边缘提取
img = cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
canny_img_one = cv2.Canny(img,300,150)
canny_img_two = canny_img_one.copy()	# 复制图片
for i in range(0,canny_img_one.shape[0]):	#image.shape表示图像的尺寸和通道信息(高,宽,通道)
	for j in range(0,canny_img_one.shape[1]):
		canny_img_two[i,j]= 255 - canny_img_one[i,j]
cv2.imshow('edge',canny_img_two)

  Eventually we can get after a graphic edge extraction, as follows:

II: image filtering

  Image filtering, i.e. noise suppression target image under conditions to preserve image detail features, are indispensable image preprocessing operation, the treatment effect is good or bad will directly affect the effectiveness of subsequent image processing and analysis, and reliability.

  Noise it is due to the imperfections of the imaging system, and a transmission medium such as a recording device, a digital image is formed, by the transmission or recording process when the input image is not the object as expected in some areas of the image processing contamination.

  There are many image filtering and filtering methods, filtering methods used in this experiment is mean filtering, Gaussian filtering and median filtering, edge detection and Gaussian.

  Mean filter is a typical linear filtering algorithm, it refers to image the target pixel to a template that includes adjacent pixels surrounding (around the target pixel as the center of eight pixels, constituting a filter template, i.e., removed target pixel itself), then the average of all the pixels of the template to replace the original pixel values.

  Median filter is based on nonlinear signal processing technique ordered statistics theory can effectively suppress noise, the basic principle is the median filter value in a digital image or a digital bit sequence with a neighborhood of the point values of each point median instead of so close to the true value of the pixel values around the point of eliminating noise isolation.

  Is a linear Gaussian smoothing filter for eliminating Gaussian noise, a noise reduction process is widely used in image processing. Gaussian filtering is performed on the whole image process of the weighted average value of each pixel, and the other by its own neighborhood pixel values obtained after a weighted average.

  Purpose of edge detection is to identify significant digital image luminance change point. Edge detection is Gaussian edge detection Gaussian manner.

  Proceed as follows:

1. Read Original

  First impressions original code is as follows:

import cv2
import cv2 as cv

# 读取本相对路径下的initial.bmp文件
image = cv2.imread ("initial.png")
# 加入文本信息
cv2.putText(image,'initial',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4)
# 将image对应图像在图像窗口显示出来
cv2.imshow('initial',image)
cv2.waitKey(0)

2. Mean Filter

  Then mean filter, as follows:

# 均值滤波
image2 = cv2.blur(image,(10,5))
cv2.putText(image2,'averageFiltering',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4)
cv2.imshow('averageFiltering',image2)

3. The median filter

  Then median filter, as follows:

image3 = cv2.medianBlur(image, 5)
cv2.putText(image3,'medianFiltering',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4)
cv2.imshow('medianFiltering',image3)

4. Gaussian filtering

  After Gaussian filtering, as follows:

# 高斯滤波
image4 = cv2.GaussianBlur(image,(5,5),0)
cv2.putText(image4,'gaussianFilter',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4)
cv2.imshow('gaussianFilter',image4)

The Gaussian edge detection

  The final Gaussian edge detection, as follows:

# 高斯边缘检测
gau_matrix = np.asarray([[-2/28,-5/28,-2/28],[-5/28,28/28,-5/28],[-2/28,-5/28,-2/28]])
img = np.zeros(image.shape)
hight,width = image.shape
for i in range(1,hight-1):
	for j in range(1,width-1):
		img[i-1,j-1] = np.sum(image[i-1:i+2,j-1:j+2]*gau_matrix)
image5 = img.astype(np.uint8)
cv2.putText(image5,'gaussianEdgeDetection',(50,50),cv2.FONT_HERSHEY_SIMPLEX,1.5,(255,0, 0),4)
cv2.imshow('gaussianEdgeDetection',image5)

III: edge detection operator

  Edge detection is a fundamental problem in image processing and computer vision, object of the edge detection image identification number will be apparent luminance change point. Including discontinuities in the depth, the direction of a discontinuous surface, the material properties change and scene lighting changes. It is a computer vision field feature extraction.

  In the actual image segmentation is often used only the first-order and second-order derivatives, although on principle, you can use higher order derivatives, but because of the impact of noise, there will be noise in the second-order derivative operating purely in sensitive phenomenon, the third derivative of the above information is often lost value. The second derivative of the gradation type may also be described mutation. In some cases, such as a uniform change in grayscale image, using only the first derivative may not find a boundary, then the second derivative can provide useful information. The second derivative is also more sensitive to noise, the solution is to smoothing the image, eliminate some noise, and then edge detection. However, the use of second derivative algorithm is based on information of the detected zero-crossing, the edge points obtained thus less conducive to subsequent processing and identification work.

  Computer vision is the imitation of the process of human vision. Therefore, in the detection of object edges, rough detection of its first contour point, then the original detected contour points connected by linking rules, but also to detect and connect the missing border point and remove the false boundary points. Edge image is an important feature of the image, it is the basis of computer vision and pattern recognition, and image processing is edge detection and therefore an important part.

  In opencv also has several edge detection method, an order has Roberts Cross operator, the Prewitt operator, Sobel operator, Canny operator, operator Krisch, compass operators; the second order there Marr-Hildreth, the second derivative in the gradient direction of the zero crossings.

  Proceed as follows:

1. Display artwork

  First, use the following code to show the original image:

# 读取本相对路径下的dip_switch_02.bmp文件
src_s = cv2.imread ("dip_switch_02.bmp",0)
cv2.imshow('src_s',src_s)

2. Anti-color image

  The image anti-color, anti-color is the primary color overlay can become white color, you can use white (RGB: 255,255,255) minus the color of the original image, so for black and white picture, we first load an 8-bit gray of the image, each pixel corresponding to the gradation value from 0-255, only need to read the gradation value of each pixel a, 255-a and then written again after this operation, the image will reverse color. code show as below:

src = cv.imread("dip_switch_02.bmp")
height, width, channels = src.shape
for row in range(height):
    for list in range(width):
        for c in range(channels):
            pv = src[row, list, c]
            src[row, list, c] = 255 - pv
cv.imshow("AfterDeal", src)

3. The image edge detection sobel Method

  Sobel operator is a discrete differential operator for edge detection, which combines differential and Gaussian smoothing derivative. The operator for calculating a degree of image shading approximations. According to the degree of light and shade next to the image edge to the region more than a certain number of specific points denoted edge. Sobel operator Prewitt operator on the basis of the weight increase on the concept that the distance from the adjacent points of impact of the current pixel is different from the corresponding pixels near the greater influence of the current pixel, thereby realizing the image sharpen and highlight the edge profile.

  Sobel edge operator more accurate positioning sub, commonly used in large noise, the image gray gradation
Sobel operator comprises two 3x3 matrices, respectively horizontal and vertical templates, the convolution of the image as a plane, respectively, to obtain the horizontal and vertical luminance difference approximation.

  code show as below:

# 用sobel方法进行边缘检测
x = cv2.Sobel(src,cv2.CV_16S,1,0)
y = cv2.Sobel(src,cv2.CV_16S,0,1)
absX = cv2.convertScaleAbs(x)   # 转回uint8
# cv2.imshow("absX", absX)

# 截图后反转
def inverse_color(image):
	height, width, channels = image.shape
	for row in range(height):
			for list in range(width):
				for c in range(channels):
					pv = image[row, list, c]
					image[row, list, c] = 255 - pv

	cv.imshow("result", image)
one = cv.imread("2.jpg")
inverse_color(one)

4. The image edge detecting method robert

  Roberts operator, also known as cross-differential operator, which is based on the differential cross gradient algorithm calculates the edge lines detected by topical differential. It used to process an image having a steep low noise, when the positive image edge close to 45 degrees or -45 degrees, the algorithm is better treatment effect. The disadvantage is less accurate positioning of the edge of the extracted edge thicker lines.

# 用robert方法进行边缘检测
dst = cv2.addWeighted(absX,0.5,absY,0.5,0)
# cv2.imshow("dst", dst)
# 截图后反转
def inverse_color(image):
	height, width, channels = image.shape
	for row in range(height):
			for list in range(width):
				for c in range(channels):
					pv = image[row, list, c]
					image[row, list, c] = 255 - pv
cv.imshow("result", image)
three = cv.imread ("3.jpg")
inverse_color(three)

Four: projection

  Projection is a scene that is projected onto the camera image plane. Type a perspective projection, affine projection, perspective projection of the weak perspective projection-based and the like.

  In opencv, the projection is divided into horizontal projection and vertical projection. A two-dimensional horizontal projection image is projected on the y-axis; vertical projection image projected is a two-dimensional x-axis.

  In the horizontal and vertical direction of the projection, the projected image into a first grayscale, and then binarizing separation threshold value, after the projection in the horizontal and vertical directions. The operation of cv2 database with data analysis and matplotlib library.
  Proceed as follows:

1. Display artwork

import cv2 
import numpy as np 
from matplotlib import pyplot as plt 

img=cv2.imread('123.jpg')

2. The vertical direction of the projection

img=cv2.imread('123.jpg') #读取图片,装换为可运算的数组
GrayImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #将BGR图转为灰度图
ret,thresh1=cv2.threshold(GrayImage,130,255,cv2.THRESH_BINARY)#将图片进行二值化(130,255)之间的点均变为255(背景)
(h,w)=thresh1.shape #返回高和宽
a = [0 for z in range(0, w)] 
#记录每一列的波峰
for j in range(0,w): #遍历一列 
	for i in range(0,h): #遍历一行
		if thresh1[i,j]==0: #如果改点为黑点
			a[j]+=1 #该列的计数器加一计数
			thresh1[i,j]=255 #记录完后将其变为白色 

for j in range(0,w): #遍历每一列
	for i in range((h-a[j]),h): #从该列应该变黑的最顶部的点开始向最底部涂黑
		thresh1[i,j]=0 #涂黑
plt.imshow(thresh1,cmap=plt.gray())
plt.show()
cv2.imshow('one',thresh1) 
cv2.waitKey(0)

3. The horizontal projection

for j in range(0,h): 
	for i in range(0,w): 
		if thresh1[j,i]==0: 
			a[j]+=1 
			thresh1[j,i]=255

for j in range(0,h): 
	for i in range(0,a[j]): 
		thresh1[j,i]=0 

plt.imshow(thresh1,cmap=plt.gray())
plt.show()

Five: the license plate character segmentation

  License plate recognition system (Vehicle License Plate Recognition, VLPR) is an application in a computer video image recognition technique of vehicle license plate recognition.

  License plate recognition technology requires the license plate movement can be extracted from the complex background and identified by plate extraction, image preprocessing, feature extraction, license plate character recognition technology to identify the vehicle grade, color and other information, the latest technology letters and numbers for the recognition rate of 99.7% can be achieved, Character recognition rate can reach 99%.

  To license plate character segmentation, character recognition to operate. This is done to identify and block directly enclosed. Step is required to let the color image into a grayscale image of the license plate, and then anti-color, threshold segmentation, re-use horizontal and vertical projection taken successively smaller blocks of desired length and width, are identified. The technology used is python + opencv which cv2 libraries and data analysis numpy library.
  Proceed as follows:

1. Read Original

import cv2
import cv2 as cv
import numpy as np

image1 = cv.imread('123456.jpg',1)
cv.imshow('image1', image1)

2. gradation conversion

  It is then converted to grayscale images, where image reading is provided to read grayscale image.

image2 = cv.imread('123456.jpg',0)
cv.imshow('image2', image2)

3. Anti-color

  Next, the anti-color image, the recording width and height and depth, and width and height of the 255 grayscale subtracting color.

height, width, deep = image1.shape
dst = np.zeros((height,width,1), np.uint8)
for i in range(0, height):
    for j in range(0, width):
        grayPixel = image2[i, j]
        dst[i, j] = 255-grayPixel
cv2.imshow('image3', dst)

4. Thresholding

  Then the anti-color image after dividing the threshold value, the threshold value is set to 100.

ret, thresh = cv2.threshold(dst, 100, 255, cv2.THRESH_TOZERO)
cv2.imshow('image4', thresh)

5. Projection

  After the initial length of the array units in horizontal and vertical direction of the projection, the projection shown in FIG, black specks and record projection is set two functions, feature points in each divided small rectangle returned after use to the coordinate values .

# 水平方向投影
def hProject(binary):
    h, w = binary.shape
    # 水平投影
    hprojection = np.zeros(binary.shape, dtype=np.uint8)
    # 创建h长度都为0的数组
    h_h = [0]*h
    for j in range(h):
        for i in range(w):
            if binary[j,i] == 0:
                h_h[j] += 1
    # 画出投影图
    for j in range(h):
        for i in range(h_h[j]):
            hprojection[j,i] = 255
    return h_h

# 垂直反向投影
def vProject(binary):
    h, w = binary.shape
    # 垂直投影
    vprojection = np.zeros(binary.shape, dtype=np.uint8)
    # 创建 w 长度都为0的数组
    w_w = [0]*w
    for i in range(w):
        for j in range(h):
            if binary[j, i ] == 0:
                w_w[i] += 1
    for i in range(w):
        for j in range(w_w[i]):
            vprojection[j,i] = 255
    return w_w

6. The segmentation character recognition matches

  The returned array, to determine the division position, character recognition matching segmentation.

th = thresh
h,w = th.shape
h_h = hProject(th)
start = 0
h_start, h_end = [], []
position = []
# 根据水平投影获取垂直分割
for i in range(len(h_h)):
    if h_h[i] > 0 and start == 0:
        h_start.append(i)
        start = 1
    if h_h[i] ==0 and start == 1:
        h_end.append(i)
        start = 0
for i in range(len(h_start)):
    cropImg = th[h_start[i]:h_end[i], 0:w]
    if i ==0:
        pass
    w_w = vProject(cropImg)
    wstart , wend, w_start, w_end = 0, 0, 0, 0
    for j in range(len(w_w)):
        if w_w[j] > 0 and wstart == 0:
            w_start = j
            wstart = 1
            wend = 0
        if w_w[j] ==0 and wstart == 1:
            w_end = j
            wstart = 0
            wend = 1
        # 当确认了起点和终点之后保存坐标   
        if wend == 1:
            position.append([w_start, h_start[i], w_end, h_end[i]])
            wend = 0
# 确定分割位置
for p in position:
    cv2.rectangle(thresh, (p[0], p[1]), (p[2], p[3]), (0, 0, 255), 2)
cv2.imshow('image7', thresh)
cv.waitKey(0)

Guess you like

Origin www.cnblogs.com/ITXiaoAng/p/12593782.html