算法"/>
opencv打卡64: Hilditch 细化算法
1、介绍
2、代码
import cv2
import numpy as np
import matplotlib.pyplot as plt# hilditch thining
def hilditch(img):# get shapeH, W, C = img.shape# prepare out imageout = np.zeros((H, W), dtype=np.int)out[img[..., 0] > 0] = 1# inverse pixel valuetmp = out.copy()_tmp = 1 - tmpcount = 1while count > 0:count = 0tmp = out.copy()_tmp = 1 - tmptmp2 = out.copy()_tmp2 = 1 - tmp2# each pixelfor y in range(H):for x in range(W):# skip black pixelif out[y, x] < 1:continuejudge = 0## condition 1if (tmp[y, min(x+1, W-1)] * tmp[max(y-1,0 ), x] * tmp[y, max(x-1, 0)] * tmp[min(y+1, H-1), x]) == 0:judge += 1## condition 2c = 0c += (_tmp[y, min(x+1, W-1)] - _tmp[y, min(x+1, W-1)] * _tmp[max(y-1, 0), min(x+1, W-1)] * _tmp[max(y-1, 0), x])c += (_tmp[max(y-1, 0), x] - _tmp[max(y-1, 0), x] * _tmp[max(y-1, 0), max(x-1, 0)] * _tmp[y, max(x-1, 0)])c += (_tmp[y, max(x-1, 0)] - _tmp[y, max(x-1, 0)] * _tmp[min(y+1, H-1), max(x-1, 0)] * _tmp[min(y+1, H-1), x])c += (_tmp[min(y+1, H-1), x] - _tmp[min(y+1, H-1), x] * _tmp[min(y+1, H-1), min(x+1, W-1)]
更多推荐
opencv打卡64: Hilditch 细化算法
发布评论