#!/usr/bin/env python#-*- coding: utf-8 -*-#code:myhaspl@qq.comimport cv2import numpy as npfn="test1.jpg"if __name__ == '__main__': print 'http://blog.csdn.net/myhaspl' print 'myhaspl@qq.com' print print 'loading %s ...' % fn print 'working', myimg1 = cv2.imread(fn) w=myimg1.shape[1] h=myimg1.shape[0] sz1=w sz0=h #建立空白映像 myimg2=np.zeros((sz0,sz1,3), np.uint8) #對比產生線條 black=np.array([0,0,0]) white=np.array([255,255,255]) centercolor=np.array([125,125,125]) for y in xrange(0,sz0-1): for x in xrange(0,sz1-1): mydown=myimg1[y+1,x,:] myright=myimg1[y,x+1,:] myhere=myimg1[y,x,:] lmyhere=sum(myhere)/3 lmyright=sum(myright)/3 lmydown=sum(mydown)/3 if abs(lmyhere-lmydown)>20 and abs(lmyhere-lmyright)>20: myimg2[y,x,:]=black elif abs(lmyhere-lmydown)<=20 and abs(lmyhere-lmyright)<=20: myimg2[y,x,:]=white else: myimg2[y,x,:]=centercolor print '.', cv2.namedWindow('img2') cv2.imshow('img2', myimg2) cv2.waitKey() cv2.destroyAllWindows()
本部落格所有內容是原創,未經書面許可,嚴禁任何形式的轉載http://blog.csdn.net/u010255642
python opencv邊緣檢測的簡單演算法,直接與下面和右邊的像素進行比較