Article directory
cv2.imread() | (file path, flags) | read image |
cv2.imshow() | (image title, image object) | display image |
cv2.imwrite() | (filename, image object) | save image |
cv2.split() | channel separation | |
cv2.merge() | channel fusion | |
cv2.waitKey() | wait for key | |
cv2.destroyAllWindows() | destroy window | |
cv2.Canny() | edge detection |
Image reading
image property read
img = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
# 行、列和通道的数量(如果图像是彩色的)
dimensions = img.shape
print(dimensions)
# 图像的大小(图像高度 × 图像宽度 × 图像通道数)
total_number_of_elements= img.size
print(total_number_of_elements)
# 图像的类型
image_dtype = img.dtype
print(image_dtype)
Original image reading
import cv2
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
cv2.imshow('bgr image', img_OpenCV)
cv2.waitKey(0)
cv2.destroyAllWindows()
Read in grayscale
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg',cv2.IMREAD_GRAYSCALE)
或
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg',2)
cv2.imread parameter description: cv2.imread(file path, flags)
File path—generally use an absolute path
flags=1—before and after reading, the image format remains unchanged
flags=2—after reading, the image format will be converted to grayscale
flags=3——after reading, the image format Convert to BGR three-channel image
RGB channel change order
b, g, r = cv2.split(img_OpenCV)
img_matplotlib = cv2.merge([r, g, b])
only show a certain channel
img_OpenCV = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
# img_OpenCV[:, :, 0]=0
img_OpenCV[:, :, 1]=0
img_OpenCV[:, :, 2]=0
Read the channel value of a pixel
img = cv2.imread(r'C:\Users\Administrator\Desktop\beginning.jpg')
(b, g, r) = img[0, 0]
print(b)
print(g)
print(r)
x=img[0, 0,0]
y=img[0, 0,1]
z=img[0, 0,2]
print(x)
print(y)
print(z)
read part of image
image preprocessing
Image grayscale
Image denoising
contour recognition
OpenCV provides the function cv2.Canny() to realize Canny edge detection, its syntax is as follows:
edges = cv.Canny( image, threshold1, threshold2[, apertureSize[, L2gradient]])
Among them:
edges is the calculated edge image.
image is an 8-bit input image.
threshold1 represents the first threshold in the process.
threshold2 represents the second threshold in the process.
apertureSize represents the aperture size of the Sobel operator.
L2gradient is the logo for calculating the gradient magnitude of the image. Its default value is False. If True, the more accurate L2 norm is used for calculations (ie, the square root of the derivatives in both directions), otherwise the L1 norm is used (directly add the absolute values of the derivatives in both directions).
import cv2 as cv
import numpy as np
def line_detect_possible_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 50, 150, apertureSize=3) # apertureSize,Canny边缘检测梯度那一步,窗口大小是3
lines = cv.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=10) #函数将通过步长为1的半径和步长为π/180的角来搜索所有可能的直线
#minLineLength-线的最短长度,比这个线短的都会被忽略
#maxLineGap-两条线之间的最大间隔,如果小于此值,这两条线就会被看成一条线。
for line in lines:
print(type(line))
x1, y1, x2, y2 = line[0]
cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv.imshow("line_detect_possible_demo", edges)
src = cv.imread(r'C:\Users\Administrator\Desktop\beginning.jpg') #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
line_detect_possible_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
Line detection
import cv2 as cv
import numpy as np
def line_detect_possible_demo(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
edges = cv.Canny(gray, 50, 150, apertureSize=3) # apertureSize,Canny边缘检测梯度那一步,窗口大小是3
lines = cv.HoughLinesP(edges, 1, np.pi / 180, 100, minLineLength=50, maxLineGap=10) #函数将通过步长为1的半径和步长为π/180的角来搜索所有可能的直线
#minLineLength-线的最短长度,比这个线短的都会被忽略
#maxLineGap-两条线之间的最大间隔,如果小于此值,这两条线就会被看成一条线。
for line in lines:
print(type(line))
x1, y1, x2, y2 = line[0]
cv.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv.imshow("line_detect_possible_demo", image)
src = cv.imread("C:/Users/lenovo/Desktop/opencv/daima/banknum/template-matching-ocr/images/sudoku.png") #读取图片位置
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
line_detect_possible_demo(src)
cv.waitKey(0)
cv.destroyAllWindows()
Line intersection detection
Knowing the point of intersection of two straight lines
def lineCrossLine(p1, p2, q1, q2):
def pointAndPointToLine(pt0, pt1): ## 由两点得直线的标准方程 ax+by=c
x0, y0 = pt0
x1, y1 = pt1
return (y1-y0, x0-x1, x0 * y1 - y0 * x1)
a0,b0,c0 = pointAndPointToLine(p1,p2)
a1,b1,c1 = pointAndPointToLine(q1,q2)
dd = a0 * b1 - a1 * b0
if abs(dd) < 1e-6: return None
return ((c0 * b1 - c1 * b0) / dd, (a0 * c1 - a1 * c0) / dd)
if __name__ == '__main__':
print(lineCrossLine((0,0),(1,1),(1,1),(1,0)))
print(lineCrossLine((0,2),(3,0),(3,1),(2,3)))
print(lineCrossLine((0,2),(0,1),(0,1),(2,3)))
print(lineCrossLine((0,2),(3,0),(0,4),(6,0)))
print(lineCrossLine((1,2),(3,5),(1,2),(7,4)))
color recognition
shape recognition
digital identification
image storage
cv2.imwrite('data/1.png',img)