赞
踩
#图像边缘检测
import numpy as np
import cv2
def cv_show(name,image):
cv2.imshow(name,image)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread("D:/6.jpg",cv2.IMREAD_GRAYSCALE)
plant = cv2.imread("D:/4.jpg")
res = cv2.Canny(img,50,120)
cv_show("res",res)
#高斯金字塔 import numpy as np import cv2 def cv_show(name,image): cv2.imshow(name,image) cv2.waitKey(0) cv2.destroyAllWindows() #可连续上采样或者下采样 img = cv2.imread("D:/6.jpg") plant = cv2.imread("D:/4.jpg") print(img.shape) up = cv2.pyrUp(img) print(up.shape) down = cv2.pyrDown(img) print(down.shape) cv_show("img",img) cv_show("up",up) cv_show("down",down)
原图不能直接减去先下采样再上采样的图,处理过的图比原图多一行,多一列,直接相减会报错
#拉普拉斯金字塔 import numpy as np import cv2 def cv_show(name,image): cv2.imshow(name,image) cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("D:/4.jpg") down = cv2.pyrDown(img) down_up = cv2.pyrUp(down) print(img.shape) print(down_up.shape) print(down.shape) lap = img - down_up #会报错 print(lap.shape) cv_show("img",img) cv_show("up",up) cv_show("down",down) cv_show("lap",lap)
#轮廓提取 import numpy as np import cv2 def cv_show(name,image): cv2.imshow(name,image) cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("D:/4.jpg") #为了更高的准确率,使用二值图像 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) cv_show("thresh",thresh) #提取轮廓 #binary 返回的图像 contours轮廓点 hierarchy 层级 binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) #contours轮廓为list类型 print(np.array(contours).shape) #绘制轮廓 #传入绘制轮廓,轮廓,轮廓索引,颜色模式,线条宽度 #注意需要copy图像,否则原图会变 draw_img = img.copy() res = cv2.drawContours(draw_img,contours,-1,(0,0,255),2) #轮廓特征,要先将轮廓提取出来才可以用来计算, #即先执行cnt = contours[i],再进行面积和周长计算 cnt = contours[10] #面积 area = cv2.contourArea(cnt) #周长,True表示闭合的 len1 = cv2.arcLength(cnt,True) print(area) print(len1) cv_show("res",res)
cnt = contours[0]
print(cv2.contourArea(cnt))
print(cv2.arcLength)
#轮廓近似 import numpy as np import cv2 def cv_show(name,image): cv2.imshow(name,image) cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("D:/contours2.png") #为了更高的准确率,使用二值图像 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cnt = contours[0] draw_img = img.copy() res = cv2.drawContours(draw_img,[cnt],-1,(0,0,255),2) #周长,True表示闭合的 len1 = cv2.arcLength(cnt,True) epsilon = 0.1*len1 #比例系数越小越接近原始轮廓 #轮廓近似 approx = cv2.approxPolyDP(cnt,epsilon,True) res = cv2.drawContours(draw_img,[approx],-1,(0,255,0),2) cv_show("res",res)
#边界矩形 import numpy as np import cv2 def cv_show(name,image): cv2.imshow(name,image) cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("D:/contours2.png") #为了更高的准确率,使用二值图像 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) binary,contours,hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE) cnt = contours[0] #得到外接矩形 x,y,w,h = cv2.boundingRect(cnt) #画出外接矩形 img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2) cv_show("img",img) #轮廓面积和边界矩形比 area = cv2.contourArea(cnt) rect_area = w*h i = float(area)/rect_area print("比值",i) #外接圆 (x,y),radius = cv2.minEnclosingCircle(cnt) center = (int(x),int(y)) radius = int(radius) img = cv2.circle(img,center,radius,(255,0,0),2) cv_show("img",img)
import cv2 import numpy as np #圆检测 可通过更改150,120这两个参数检测出所有圆 image = cv2.imread("E:/3.jpg") gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) img = cv2.medianBlur(gray,5) circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,150,param1 = 120,param2 = 30,minRadius = 0,maxRadius = 0) circles = np.uint16(np.around(circles)) for i in circles[0,:]: cv2.circle(image,(i[0],i[1]),i[2],(0,0,255),2) cv2.circle(image,(i[0],i[1]),2,(0,255,0),2) cv2.imshow("img",image) cv2.waitKey(0) cv2.destroyAllWindows()
#模板匹配 import numpy as np import cv2 def cv_show(name,image): cv2.imshow(name,image) cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("D:/原图.png") template = cv2.imread("模板.png") gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY) h,w = template.shape[:2] #匹配 #TM_SQDIFF 匹配方法,归一化的方法更好用 res = cv2.matchTemplate(gray1,gray2,cv2.TM_SQDIFF) #得到极值坐标 min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(res) top_left = min_loc bottom_right = (top_left[0]+w,top_left[1]+h) #画出人脸 cv2.rectangle(img,top_left,bottom_right,255,2) cv_show("img",img)
#模板匹配,匹配多个对象 import numpy as np import cv2 def cv_show(name,image): cv2.imshow(name,image) cv2.waitKey(0) cv2.destroyAllWindows() img = cv2.imread("D:/mario.jpg") img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) template = cv2.imread("D:/mario_coin.jpg",0) #gray1 = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #gray2 = cv2.cvtColor(template,cv2.COLOR_BGR2GRAY) h,w = template.shape[:2] #匹配 #TM_SQDIFF 匹配方法,归一化的方法更好用 res = cv2.matchTemplate(img_gray,template,cv2.TM_CCOEFF_NORMED) #设置匹配阈值 threshold = 0.8 #np.where() 它有三个参数:第一个参数为数组,该数组的元素值为真或假。函数会返回同样维度的数组;输入数组的元素值为真时,输出数组的相应元素为函数的第二个参数,否则,输入数组的元素为假时输出数组相应的元素为函数的第三个参数 loc = np.where(res>=threshold) for pt in zip(*loc[::-1]): # *号表示可选参数 #得到极值坐标 bottom_right = (pt[0]+w,pt[1]+h) #画出人脸 cv2.rectangle(img,pt,bottom_right,(0,255,255),2) cv_show("img",img)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。