# -*- coding: utf-8 -*- import cv2 import numpy as np fn = '10093lv6-0.jpg' def get_EuclideanDistance(x,y): myx = np.array(x) myy = np.array(y) return np.sqrt(np.sum((myx-myy)*(myx-myy))) if __name__ == '__main__': print('loading %s ...' % fn) myimg1 = cv2.imread(fn) print () w = myimg1.shape[1] h = myimg1.shape[0] print (w,h) sz1 = w sz0 = h # 創建空白圖像 myimg2 = np.zeros((sz0, sz1, 3), np.uint8) #cv2.imshow('img122', myimg2) # 對比產生線條 #將當前像素與鄰接的下部和右部的像素進行比較,如果相似,則將當前像素設置為白色,否則設置為黑色。 #判定像素是否相似,使用歐氏距離算法,將一個像素的三個色彩分量映射在三維空間中,如果2個像素點的歐氏距離小於某個常數的閾值,就認為它們相似。 black = np.array([0,0,0]) white = np.array([255,255,255]) centercolor = np.array([125,125,125]) for y in range(0,sz0 - 1): for x in range(0,sz1 - 1): mydown = myimg1[y+1,x,:] myright = myimg1[y,x+1,:] myhere = myimg1[y,x,:] lmyhere = myhere lmyright = myright lmydown = mydown if get_EuclideanDistance(lmyhere, lmydown) >16 and get_EuclideanDistance(lmyhere,lmyright)>16: myimg2[y,x,:] = black elif get_EuclideanDistance(lmyhere,lmydown) <=16 and get_EuclideanDistance(lmyhere,lmyright)<=16: myimg2[y,x,:] = white else: myimg1[y,x,:]=centercolor print ('.',) cv2.namedWindow('img2') cv2.imshow('img2',myimg2) cv2.waitKey() cv2.destrdestroyAllWindows()


