简单生长和质心生长

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)

简单生长和质心生长
因实验课程需要写的,没有注释,如果有需要,之后有时间再后补吧。

以下是选择不同最初种子点,以及不同方法产生的部分效果图
请添加图片描述请添加图片描述
手写代码,仍是小白,时间复杂度比较大,有待改进。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值