赞
踩
- img = cv2.imread('circle.jpg')
-
- plt.imshow(img)
- plt.show()
使用两个核 Gx = [[-1,0,1], [-2,0,2], [-1,0,1]] Gy = [[-1,-2,-1], [0,0,0], [1,2,1]]
dst = cv2.Sobel(src, ddepth, dx, dy, ksize)
- # CV_64F带负数
- sobelx = cv2.Sobel(img, cv2.CV_64F,1,0,ksize = 3)
- # 取绝对值,否则左边是白-黑大于0, 右边是黑-白小于0
- sobelx = cv2.convertScaleAbs(sobelx)
-
- plt.imshow(sobelx)
- plt.show()
- sobely = cv2.Sobel(img, cv2.CV_64F,0,1,ksize = 3)
- sobely = cv2.convertScaleAbs(sobely)
-
- plt.imshow(sobely)
- plt.show()
- cat = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE)
- sobelx = cv2.convertScaleAbs(cv2.Sobel(cat, cv2.CV_64F,1,0,ksize = 3))
- sobely = cv2.convertScaleAbs(cv2.Sobel(cat, cv2.CV_64F,0, 1,ksize = 3))
- sobelxy = cv2.addWeighted(sobelx,0.5,sobely,0.5, 0)
-
- plt.figure(figsize=(20,15))
- plt.subplot(121)
- plt.imshow(cat, cmap='gray')
- plt.subplot(122)
- plt.imshow(sobelxy, cmap='gray')
- plt.show()
- img = cv2.imread('car.jpg', cv2.IMREAD_GRAYSCALE)
-
- # minval 和 maxval
- v1 = cv2.Canny(img, 30, 100)
- v2 = cv2.Canny(img, 50, 150)
-
- plt.figure(figsize=(20,25))
-
- plt.subplot(121)
- plt.imshow(v1, cmap='gray')
-
- plt.subplot(122)
- plt.imshow(v2, cmap='gray')
-
- plt.show()
- img = cv2.imread('jerry.jpg')
- up = cv2.pyrUp(img)
- down = cv2.pyrDown(img)
-
- plt.figure(figsize=(20,15))
-
- plt.subplot(131)
- plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
-
- plt.subplot(132)
- plt.imshow(cv2.cvtColor(up, cv2.COLOR_BGR2RGB))
-
- plt.subplot(133)
- plt.imshow(cv2.cvtColor(down, cv2.COLOR_BGR2RGB))
-
- plt.show()

虽然看着图片大小一样,看坐标轴上可以看出来实际大小是不一样的,清晰的也略有差异
每一层都是Li+1 = Gi - pyrUp(pyrDown(Gi))
- down_up = cv2.resize(cv2.pyrUp(cv2.pyrDown(img)),(500, 635))
- res = img - down_up
-
- plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
- plt.show()
cv2.findContours(img,mode,method)
mode:轮廓检索模式
method:轮廓逼近方法
- # 二值图像准确率更高
- img = cv2.imread('contours.jpg')
- img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- # 转为二值图像
- ret,thresh = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
-
- plt.imshow(thresh,'gray')
- plt.show()
- # 二值图像,轮廓信息,层级
- binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
- # 使用原图会导致原图改变
- img_copy = img.copy()
- # 绘制全部轮廓,BGR,线宽
- res = cv2.drawContours(img_copy, contours, -1, (0,0,255),2)
-
- plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
- plt.show()
- # 拿出星形的外圈轮廓
- cnt = contours[0]
-
- print('星形外圈包围的面积为',cv2.contourArea(cnt))
- print('星形外圈周长为',cv2.arcLength(cnt,True))
原轮廓
- img = cv2.imread('irregular.jpg')
- img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
- ret, thresh = cv2.threshold(img_gray, 127,255,cv2.THRESH_BINARY)
-
- binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
- cnt = contours[0]
- img_copy = img.copy()
- res = cv2.drawContours(img_copy, [cnt], 0, (0,0,255),2)
-
- plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
- plt.show()
有时我们不需要这么精准的轮廓
- # 0.1倍的周长作为阈值
- epsilon = 0.1 * cv2.arcLength(cnt, True)
- approx = cv2.approxPolyDP(cnt, epsilon, True)
-
- img_copy = img.copy()
- res = cv2.drawContours(img_copy, [approx], -1, (0,0,255),2)
-
- plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
- plt.show()
外接矩形
- img = cv2.imread('contours.jpg')
- img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
- ret,thresh = cv2.threshold(img_gray, 127, 255, cv2.THRESH_BINARY)
-
- binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
- cnt = contours[0]
- # 得到四条边
- x,y,w,h = cv2.boundingRect(cnt)
- img_copy = img.copy()
- res = cv2.rectangle(img_copy, (x, y), (x + w, y + h), (0,255,0), 2)
-
- plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
- plt.show()
外接圆
- (x, y), radius = cv2.minEnclosingCircle(cnt)
- center = (int(x), int(y))
- radius = int(radius)
- res = cv2.circle(img, center, radius, (0,255,0),2)
-
- plt.imshow(cv2.cvtColor(res, cv2.COLOR_BGR2RGB))
- plt.show()
就是从大图像中找出匹配的目标图像
- img = cv2.imread('car.jpg')
- template = cv2.imread('wheel.jpg')
- h,w = template.shape[:2]
- # 常用方法
- methods={'cv2.TM_CCOEFF','cv2.TM_CCOEFF_NORMED','cv2.TM_CCORR',
- 'cv2.TM_CCORR_NORMED','cv2.TM_SQDIFF','cv2.TM_SQDIFF_NORMED'}
-
- for m in methods:
- img2 = img.copy()
- # 获得匹配方法的真值
- method = eval(m)
- # 进行匹配
- res = cv2.matchTemplate(img,template, method)
- # 获得匹配的最高最低得分值及其位置
- min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
-
- # 如果是平方差匹配或者归一化平方差匹配,取最小值
- if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
- top_left = min_loc
- else:
- top_left = max_loc
- bottom_right = (top_left[0] + w, top_left[1] + h)
-
- # 绘制矩形
- cv2.rectangle(img2, top_left, bottom_right, 255, 2)
-
- plt.subplot(121)
- plt.imshow(res, 'gray')
- plt.xticks([])
- plt.yticks([])
- plt.subplot(122)
- plt.imshow(img2, 'gray')
- plt.xticks([])
- plt.yticks([])
- plt.suptitle(m)
- plt.show()

图片太长了,这里只展示一部分,从结果中可以看出, 带归一化的方法比不带归一化的方法要更加精准
直方图直观的表示图片中某个(或某组)像素值的像素点个数
cv2.calcHist(images,channels,mask,histSize,ranges)
- img = cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
- hist = cv2.calcHist([img],[0],None,[256],[0,256])
-
- plt.hist(img.ravel(),256)
- plt.show()
三通道直方图
- img = cv2.imread('cat.jpg')
- color = ('b','g','r')
- for i, col in enumerate(color):
- histr = cv2.calcHist([img],[i],None,[256],[0,256])
- plt.plot(histr, color=col)
- plt.xlim([0,256])
- plt.show()
- img = cv2.imread('cat.jpg',cv2.IMREAD_GRAYSCALE)
- equ = cv2.equalizeHist(img)
- plt.hist(equ.ravel(), 256)
- plt.show()
-
- plt.subplot(121)
- plt.imshow(img, 'gray')
- plt.subplot(122)
- plt.imshow(equ, 'gray')
- plt.show()
- clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
- res_clahe = clahe.apply(img)
-
- plt.subplot(121)
- plt.imshow(img, 'gray')
-
- plt.subplot(122)
- plt.imshow(res_clahe, 'gray')
-
- plt.show()
- img = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE)
- img_float32 = np.float32(img)
-
- dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
- dft_shift = np.fft.fftshift(dft)
-
- # 转为灰度图表示形式
- magnitude_spectrum = 20 * np.log(cv2.magnitude(dft_shift[:,:,0], dft_shift[:,:,1]))
-
- plt.subplot(121)
- plt.imshow(img, 'gray')
- plt.title('input')
-
- plt.subplot(122)
- plt.imshow(magnitude_spectrum, 'gray')
- plt.title('output')
-
- plt.show()

拿到特征图后可以使用低通滤波使图像边缘模糊,或者使用高通滤波加强图像边缘
- img = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE)
- img_float32 = np.float32(img)
-
- dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
- dft_shift = np.fft.fftshift(dft)
-
- rows,cols = img.shape
- # 中心
- crow,ccol = int(rows/2), int(cols/2)
-
- # 低通滤波
- mask = np.zeros((rows,cols,2), np.uint8)
- mask[crow-30:crow+30, ccol-30:ccol+30] = 1
- fshift = dft_shift * mask
-
- # idft
- f_ishift = np.fft.ifftshift(fshift)
- img_back = cv2.idft(f_ishift)
- img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])
-
- plt.subplot(121)
- plt.imshow(img, 'gray')
- plt.title('input')
-
- plt.subplot(122)
- plt.imshow(img_back, 'gray')
- plt.title('output')
-
- plt.show()
-
-
-
-
- img = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE)
- img_float32 = np.float32(img)
-
- dft = cv2.dft(img_float32, flags=cv2.DFT_COMPLEX_OUTPUT)
- dft_shift = np.fft.fftshift(dft)
-
- rows,cols = img.shape
- # 中心
- crow,ccol = int(rows/2), int(cols/2)
-
- # 高通滤波
- mask = np.ones((rows,cols,2), np.uint8)
- mask[crow-30:crow+30, ccol-30:ccol+30] = 0
- fshift = dft_shift * mask
- # idft
- f_ishift = np.fft.ifftshift(fshift)
- img_back = cv2.idft(f_ishift)
- img_back = cv2.magnitude(img_back[:,:,0], img_back[:,:,1])
-
- plt.subplot(121)
- plt.imshow(img, 'gray')
- plt.title('input')
-
- plt.subplot(122)
- plt.imshow(img_back, 'gray')
- plt.title('output')
-
- plt.show()

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。