import cv2 as cv
import numpy as np
import pyautogui
imgo = cv.imread('C:/Users/ALW/Pictures/gr.jpg')
imgc = cv.cvtColor(imgo, cv.COLOR_BGR2GRAY)
def cv_show(img,imgc):
cv.imshow(img,imgc)
cv.waitKey(0)
cv.destroyAllWindows()
cv_show('img',imgc)
h,w= pyautogui.position()
print(h,w)
T = 5
xbian = imgc.shape[0]
ybian = imgc.shape[1]
print(xbian,ybian)
new_img = np.zeros((xbian,ybian))
ab, new_imgc = cv.threshold(new_img, 127,255, cv.THRESH_BINARY)
test_img = np.zeros((xbian,ybian))
abcd, test_imgc = cv.threshold(test_img, 127,255, cv.THRESH_BINARY)
count = 1
new_imgc[w][h] = 1
way = input("What is your choice?")
if way == 1:
while (count):
count = 0
for i in range(1,xbian-1):
for j in range(1,ybian-1):
if ((new_imgc[i][j]==1) and (test_imgc[i][j] == 0)):
test_imgc[i][j] = 1
for x in range(i-1,i+2):
for y in range(j-1,j+2):
if ((new_imgc[x][y]==0)and( abs(imgc[x][y]- imgc[i][j]) <= T)):
new_imgc[x][y] = 1
count = count+1
cv_show('new_imgc',new_imgc)
else:
seeds=[]
aver = imgc[w][h]
seeds.append(aver)
while (count):
count = 0
for i in range(1,xbian-1):
for j in range(1,ybian-1):
if ((new_imgc[i][j]==1) and (test_imgc[i][j] == 0)):
test_imgc[i][j] = 1
aver = int(np.mean(seeds))
for x in range(i-1,i+2):
for y in range(j-1,j+2):
if ((new_imgc[x][y]==0)and( abs(imgc[x][y]- aver) <= T)):
seeds.append(imgc[x][y])
new_imgc[x][y] = 1
count = count+1
cv_show('new_imgc',new_imgc)
简单生长和质心生长
因实验课程需要写的,没有注释,如果有需要,之后有时间再后补吧。
以下是选择不同最初种子点,以及不同方法产生的部分效果图
手写代码,仍是小白,时间复杂度比较大,有待改进。