加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lab4_2.py 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
import numpy as np
import cv2
# zong :9 ren width: 2/9-7/9 88-312 height:20px
import matplotlib.pyplot as plt
img=cv2.imread("announcer .jpg")
print(img.shape)
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ishow=img.copy()
ret,thresh=cv2.threshold(gray,0,255,cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)
kernel=np.ones((3,3),np.uint8)
opening=cv2.morphologyEx(thresh,cv2.MORPH_OPEN,kernel,iterations=2)
sure_bg=cv2.dilate(opening,kernel,iterations=3)
dist_transform=cv2.distanceTransform(opening,cv2.DIST_L2,5)
ret,sure_fg=cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0)
sure_fg=np.uint8(sure_fg)
unknown=cv2.subtract(sure_bg,sure_fg)
ret,markers=cv2.connectedComponents(sure_fg)
markers=markers+1
markers[unknown==255]=0
markers=cv2.watershed(img,markers)
img[markers==-1]=[0,255,0]
# plt.subplot(121)
# plt.imshow(ishow)
# plt.axis('off')
# plt.subplot(122)
# plt.imshow(img)
# plt.axis('off')
# plt.waitforbuttonpress()
cv2.imshow("original iamge",ishow)
cv2.imshow("ans image",img)
cv2.waitKey(-1)
# #测试函数
# def testCovolve():
# image = cv2.imread("lena_noise.bmp")
#
# #均值滤波
# k1 = np.array([
# [1 / 9, 1 / 9, 1 / 9],
# [1 / 9, 1 / 9, 1 / 9],
# [1 / 9, 1 / 9, 1 / 9]
# ])
#
# #
# k2 = np.array([[-1, 0, 1],
# [-2, 0, 2],
# [-1, 0, 1]])
# k3=np.array([[1/16,2/16,1/16],
# [2/16,4/16,2/16],
# [1/16,2/16,1/16]])
#
# gaussK=np.array([[2/115,4/115,5/115,4/115,2/115],
# [4/115,9/115,12/115,9/115,4/115],
# [5/115,12/115,15/115,12/115,5/115],
# [4/115,9/115,12/115,9/115,4/115],
# [2/115,4/115,5/115,4/112,2/115]])
# rgihtGauss=np.array([[1/273,4/273,7/273,4/273,1/273],
# [4/273,16/273,26/273,16/273,4/273],
# [7/273,26/273,41/273,26/273,7/243],
# [4/273,16/273,26/273,16/273,4/273],
# [1/273,4/273,7/273,4/273,1/273]])
# # res1 = conv(image, k1, 'fill')
# # cv2.imshow("Convoluted picture by k1", res1)
# # res2 = conv(image, k2, 'fill')
# # cv2.imshow("Convoluted picture by k2", res2)
# res3=conv(image,rgihtGauss,"fill")
# cv2.imshow("MyGaussian",res3)
# cv2.imshow('yuan shi tu xiang', image)
#库函数的高斯滤波
# image=cv2.GaussianBlur(image,(3,3),0)
# cv2.imshow('GaussianBlur', image)
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化