# '''在一个对象的质点绘制文字说明''' # import cv2 # import numpy as np # o = cv2.imread("D:\\img\\cat3.jpg",1) # cv2.imshow("original",o) # gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY) #首先转化为灰度图像 # ret,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) #转化为二值图像,绘制轮廓只能用二值图 # contours,heirarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # x = cv2.drawContours(o,contours,0,(0,0,255),3) # m00 = cv2.moments(contours[0])['m00'] # m01 = cv2.moments(contours[0])['m01'] # m10 = cv2.moments(contours[0])['m10'] # cx = int(m10/m00) # cy = int(m01/m00) # cv2.putText(o,'cat',(cx,cy),cv2.FONT_HERSHEY_SIMPLEX,1,(0,0,255),3) # cv2.imshow("result",o) # cv2.waitKey() # cv2.destroyAllWindows() # '''使用cv.contoursArea()计算轮廓面积''' # import cv2 # o = cv2.imread("D:\\img\\opencv.png",1) # cv2.imshow("original",o) # gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY) # ret,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # contours,heirarchy = cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # n = len(contours) # for i in range(n) : # print("contours["+str(i)+"]面积 = ",cv2.contourArea(contours[i])) # cv2.drawContours(o,contours,i,(0,0,255),3) # cv2.imshow("result",o) # cv2.waitKey() # cv2.destroyAllWindows() # '''将面积大于1000的前景图片筛选出来''' # import cv2 # o = cv2.imread("D:\\img\\opencv.png",1) # gray = cv2.cvtColor(o,cv2.COLOR_BGR2GRAY) # ret,binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) # contours ,hierarchy= cv2.findContours(binary,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE) # # contoursok=[] # for i in contours: # if cv2.contourArea(i) > 1000: # contoursok.append(i) # cv2.drawContours(o,contoursok,-1,(0,0,255),3) # cv2.imshow("result",o) # cv2.waitKey() # cv2.destroyAllWindows() # '''使用cv2.getStructuringElement函数生成不同的核''' # import cv2 # kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT,(5,5)) # kernel2 = cv2.getStructuringElement(cv2.MORPH_CROSS,(5,5)) # kernel3 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)) # print("kernel1 = \n",kernel1) # print("kernel2 = \n",kernel2) # print("kernel3 = \n",kernel3) # '''观察不同结构的核对形态学的影响''' # import cv2 # o = cv2.imread("D:\\img\\kernel.bmp",cv2.IMREAD_UNCHANGED) # kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT,(59,59)) # kernel2 = cv2.getStructuringElement(cv2.MORPH_CROSS,(59,59)) # kernel3 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(59,59)) # dst1 = cv2.dilate(o,kernel1) # dst2 = cv2.dilate(o,kernel2) # dst3 = cv2.dilate(o,kernel3) # cv2.imshow("dst1",dst1) # cv2.imshow("dst2",dst2) # cv2.imshow("dst3",dst3) # cv2.waitKey() # cv2.destroyAllWindows() # '''使用zip函数遍历多个数组''' # a = '张三','李四','王五','赵六','小七' # b = '机器视觉','python','opencv','getStructuringElement','dilate' # for n,i,j in zip(range(len(a)),a,b): # print(n,i,j) import cv2 img = cv2.imread("D:\\img\\count.jpg",1) # 图像与处理 gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #色彩空间转换:彩色-->灰度图片 ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV) # 阈值处理二值反色 kernel=cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5))#核 erotion=cv2.erode(binary,kernel,iterations=4) #腐蚀操作 dilation=cv2.dilate(erotion,kernel,iterations=3) #膨胀操作 gaussian = cv2.GaussianBlur(dilation,(3,3),0)# 高斯滤波 # 查找所有轮廓 contours,hirearchy=cv2.findContours(gaussian, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) contoursok = [] for i in contours: if cv2.contourArea(i) > 30: contoursok.append(i) # 标注出轮廓 draw = cv2.drawContours(img,contoursok,-1,(0,0,255),1) # 计算每一个细胞的质心,并标记数字 for i,j in zip(contoursok,range(len(contoursok))): M = cv2.moments(i) x = int(M["m10"] / M["m00"]) y = int(M["m01"] / M["m00"]) cv2.putText(draw,str(j),(x,y),cv2.FONT_HERSHEY_PLAIN,1.5,(0,0,255),2) # 显示图片 cv2.imshow("gaussian",gaussian) cv2.imshow("draw",draw) cv2.waitKey() cv2.destroyAllWindows()
07-29
1612
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-17
2万+
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-25
1045
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-16
754
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-15
893
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交