#coding=utf-8 import cv2 #cv2.__version__==3.2.0 import numpy as np #读取,显示,写 # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # # cv2.imshow('show', img) # print(np.shape(img)) # # cv2.waitKey() # img_crop=img[10:100,10:200] # cv2.imwrite('img_write.tif',img_crop) # exit() #颜色转换 # print([x for x in dir(cv2) if x.startswith('COLOR_')]) # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # img_gray=cv2.cvtColor(img,cv2.COLOR_RGB2GRAY) # # cv2.imshow('show', img_gray) # cv2.imshow('all',img) # cv2.imshow('R',img[:,:,0]) # cv2.imshow('G',img[:,:,1]) # cv2.imshow('B',img[:,:,2]) # r_img=img[:,:,0] # print(np.shape(r_img)) # cv2.waitKey() #Image translation # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # num_rows,num_cols=img.shape[:2] # # T=[[1,0,tx][0,1,ty]] # translation_matrix = np.float32([ [1,0,-70], [0,1,110] ]) # img_translation = cv2.warpAffine(img, translation_matrix, (num_cols,num_rows)) # cv2.imshow('Translation', img_translation) # cv2.waitKey() #Image rotation # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # num_rows, num_cols = img.shape[:2] # # R=[[cos@ -sin@],[sin@ cos@]] # rotation_matrix = cv2.getRotationMatrix2D(center=(num_cols/2, num_rows/2), angle=1.2, scale=1) # img_rotation = cv2.warpAffine(img, rotation_matrix, (num_cols, num_rows)) # cv2.imshow('Rotation', img_rotation) # cv2.waitKey() #Image scaling # img=cv2.imread('test1.jpg',cv2.IMREAD_ANYCOLOR) # img_scaled = cv2.resize(img,None,fx=1.2, fy=1.2, interpolation =cv2.INTER_LINEAR) # cv2.imshow('Scaling - Linear Interpolation', img_scaled) # img_scaled =cv2.resize(img,None,fx=1.2, fy=1.2, interpolation = cv2.INTER_CUBIC) # cv2.imshow('Scaling - Cubic Interpolation', img_scaled) # img_scaled =cv2.resize(img,(450, 400), interpolation = cv2.INTER_AREA) # cv2.imshow('Scaling - Skewed Size', img_scaled) # cv2.waitKey() #Afine transform 仿射 # img = cv2.imread('test1.jpg') # rows, cols = img.shape[:2] # # src_points = np.float32([[0,0], [cols-1,0], [0,rows-1]]) # # dst_points = np.float32([[0,0], [int(0.6*(cols-1)),0], [int(0.4*(cols-1)),rows-1]]) # # affine_matrix = cv2.getAffineTransform(src_points, dst_points) # # img_output = cv2.warpAffine(img, affine_matrix, (cols,rows)) # src_points = np.float32([[0,0], [cols-1,0], [0,rows-1], [cols-1,rows-1]]) # dst_points = np.float32([[0,0], [cols-1,0], [int(0.33*cols),rows-1],[int(0.66*cols),rows-1]]) # projective_matrix = cv2.getPerspectiveTransform(src_points, dst_points) # img_output = cv2.warpPerspective(img, projective_matrix, (cols,rows)) # cv2.imshow('Input', img) # cv2.imshow('Output', img_output) # cv2.waitKey() #2D convolution 卷积处理 # img = cv2.imread('test1.jpg') # rows, cols = img.shape[:2] # kernel_identity = np.array([[0,0,0], [0,1,0], [0,0,0]]) # kernel_3x3 = np.ones((3,3), np.float32) / 9.0 # kernel_5x5 = np.ones((5,5), np.float32) / 25.0 # cv2.imshow('Original', img) # output = cv2.filter2D(img, -1, kernel_identity) # cv2.imshow('Identity filter', output) # output = cv2.filter2D(img, -1, kernel_3x3) # cv2.imshow('3x3 filter', output) # output = cv2.filter2D(img, -1, kernel_5x5) # cv2.imshow('5x5 filter', output) # cv2.waitKey(0) #边缘检测 # img = cv2.imread('test1.jpg', cv2.IMREAD_GRAYSCALE) # rows, cols = img.shape # sobel_horizontal = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5) # sobel_vertical = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5) # kernel_1 = np.array([[-1,0,1], [-2,0,2], [-1,0,1]]) # kernel_2 = np.array([[-1,-2,-1], [0,0,0], [1,2,1]]) # output=cv2.filter2D(img, -1,kernel=kernel_1) # cv2.imshow('ker1',output) # output2=cv2.filter2D(img, -1,kernel=kernel_2) # cv2.imshow('ker2',output2) # # cv2.imshow('Original', img) # # cv2.imshow('Sobel horizontal', sobel_horizontal) # # cv2.imshow('Sobel vertical', sobel_vertical) # cv2.waitKey(0) #erode dilate 腐蚀膨胀 # img = cv2.imread('test1.jpg', cv2.IMREAD_ANYCOLOR) # kernel = np.ones((5,5), np.uint8) # img_erosion = cv2.erode(img, kernel, iterations=1) # img_dilation = cv2.dilate(img, kernel, iterations=1) # cv2.imshow('Input', img) # cv2.imshow('Erosion', img_erosion) # cv2.imshow('Dilation', img_dilation) # cv2.waitKey(0) #装饰 # img = cv2.imread('test1.jpg') # rows, cols = img.shape[:2] # # generating vignette mask using Gaussian kernels # kernel_x = cv2.getGaussianKernel(cols,200) # kernel_y = cv2.getGaussianKernel(rows,200) # kernel = kernel_y * kernel_x.T # mask = 255 * kernel / np.linalg.norm(kernel) # output = np.copy(img) # # applying the mask to each channel in the input image # for i in range(3): # output[:,:,i] = output[:,:,i] * mask # cv2.imshow('Original', img) # cv2.imshow('Vignette', output) # cv2.waitKey(0) #灰度图像:直方图均衡化 # img = cv2.imread('test1.jpg', cv2.IMREAD_GRAYSCALE) # # equalize the histogram of the input image # histeq = cv2.equalizeHist(img) # cv2.imshow('Input', img) # cv2.imshow('Histogram equalized', histeq) # cv2.waitKey(0) #连通域 # img = cv2.imread('test1.jpg', cv2.IMREAD_ANYCOLOR) # print(img.shape) # img_graycolor=cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) # ret,img_gray=cv2.threshold(img_graycolor, 127, 255, cv2.THRESH_BINARY)#cv2.THRESH_OTSU # img_contours,contours, _ = cv2.findContours(img_gray, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
第一个参数是寻找轮廓的图像;
第二个参数表示轮廓的检索模式,有四种(本文介绍的都是新的cv2接口): cv2.RETR_EXTERNAL表示只检测外轮廓 cv2.RETR_LIST检测的轮廓不建立等级关系 cv2.RETR_CCOMP建立两个等级的轮廓,上面的一层为外边界,里面的一层为内孔的边界信息。如果内孔内还有一个连通物体,这个物体的边界也在顶层。 cv2.RETR_TREE建立一个等级树结构的轮廓。
第三个参数method为轮廓的近似办法 cv2.CHAIN_APPROX_NONE存储所有的轮廓点,相邻的两个点的像素位置差不超过1,即max(abs(x1-x2),abs(y2-y1))==1 cv2.CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息 cv2.CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
# print(len(contours)) # # print(contours) # # cv2.drawContours(img, np.array(contours), -1, (0,0,255), 1) # # cv2.imshow('drawContours', img) # # cv2.waitKey() # for contour in contours: # area = cv2.contourArea(contour) # x,y,w,h=cv2.boundingRect(contour) # print(x,y,w,h,area) # cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,255)) # cv2.imshow('rectangle', img) # cv2.waitKey() #Image warping import math img = cv2.imread('test1.jpg', cv2.IMREAD_GRAYSCALE) rows, cols = img.shape ##################### # Vertical wave # img_output = np.zeros(img.shape, dtype=img.dtype) # for i in range(rows): # for j in range(cols): # offset_x = int(25.0 * math.sin(2 * 3.14 * i / 180)) # offset_y = 0 # if j+offset_x < rows: # img_output[i,j] = img[i,(j+offset_x)%cols] # else: # img_output[i,j] = 0 # cv2.imshow('Input', img) # cv2.imshow('Vertical wave', img_output) ##################### ##################### # Horizontal wave # img_output = np.zeros(img.shape, dtype=img.dtype) # for i in range(rows): # for j in range(cols): # offset_x = 0 # offset_y = int(16.0 * math.sin(2 * 3.14 * j / 150)) # if i+offset_y < rows: # img_output[i,j] = img[(i+offset_y)%rows,j] # else: # img_output[i,j] = 0 # cv2.imshow('Horizontal wave', img_output) ##################### ##################### # Concave effect img_output = np.zeros(img.shape, dtype=img.dtype) for i in range(rows): for j in range(cols): offset_x = int(30.0 * math.sin(2 * 3.14 * i / (2*cols))) offset_y = 0 if j+offset_x < cols: img_output[i,j] = img[i,(j+offset_x)%cols] else: img_output[i,j] = 0 cv2.imshow('Concave', img_output) cv2.waitKey()