<https://blog.csdn.net/sinat_21258931/article/details/61418681>
#encoding:utf-8 import cv2 import numpy as np def get_image(path): #获取图片 img=
cv2.imread(path) gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) return img, gray def
Gaussian_Blur(gray): # 高斯去噪(去除图像中的噪点) """ 高斯模糊本质上是低通滤波器:

9)表示高斯矩阵的长与宽，标准差取0时OpenCV会根据高斯矩阵的尺寸自己计算。 高斯矩阵的尺寸越大，标准差越大，处理过的图像模糊程度越大。 """
blurred= cv2.GaussianBlur(gray, (9, 9),0) return blurred def Sobel_gradient(
blurred): """ 索比尔算子来计算x、y方向梯度

= cv2.Sobel(blurred, ddepth=cv2.CV_32F, dx=1, dy=0) gradY = cv2.Sobel(blurred,
cv2.threshold(blurred, 90, 255, cv2.THRESH_BINARY) """
cv2.threshold(src,thresh,maxval,type[,dst])->retval,dst (二元值的灰度图) src： 一般输入灰度图
thresh:阈值， maxval:在二元阈值THRESH_BINARY和 逆二元阈值THRESH_BINARY_INV中使用的最大值 type:

MORPH_ELLIPSE, (25, 25)) closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE,
kernel) closed = cv2.erode(closed, None, iterations=4) closed = cv2.dilate(
closed, None, iterations=4) return closed def findcnts_and_box_point(closed): #

cv2.CHAIN_APPROX_SIMPLE) c = sorted(cnts, key=cv2.contourArea, reverse=True)[0]
# 计算最大轮廓的旋转包围盒 rect = cv2.minAreaRect(c) box = np.int0(cv2.boxPoints(rect))
return box def drawcnts_and_cut(original_img, box): #目标图像裁剪 #

[box], -1, (0, 0, 255), 3) Xs = [i[0] for i in box] Ys = [i[1] for i in box] x1
= min(Xs) x2 = max(Xs) y1 = min(Ys) y2 = max(Ys) hight = y2 - y1 width = x2 -
x1 crop_img= original_img[y1:y1+hight, x1:x1+width] return draw_img, crop_img
def walk(): img_path = r'./2010.png' save_path = r'./cat_save.png' original_img,
image_morphology(thresh) box = findcnts_and_box_point(closed) draw_img, crop_img
= drawcnts_and_cut(original_img,box) # 暴力一点，把它们都显示出来看看 cv2.imshow('original_img'
thresh) cv2.imshow('closed', closed) cv2.imshow('draw_img', draw_img) cv2.imshow
('crop_img', crop_img) cv2.waitKey(20171219) cv2.imwrite(save_path, crop_img)
walk()

https://blog.csdn.net/sinat_36458870/article/details/78825571
<https://blog.csdn.net/sinat_36458870/article/details/78825571>

30天阅读排行