| 
 | 
 
 
原圖: 
Python cv2 輪廓描繪 
 
 
 
 
完整代碼 
- import cv2
 
 - image = cv2.imread('coins2.png')
 
 - image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
 
 - cv2.imshow('Original',image)
 
 - cv2.imwrite('coin1_Original.png', image)
 
 - print(image.shape)
 
 - gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 
 - cv2.imshow('gray',gray)
 
 - cv2.imwrite('coin1_gray.png',gray)
 
 - cv2.waitKey()
 
  複製代碼- import cv2,coin1
 
 - gray = coin1.gray
 
 - image = coin1.image
 
 - print('高斯模糊處理')
 
 - blurred = cv2.GaussianBlur(gray,(13,13),0) # 高斯模糊的目的是把雜訊過濾掉
 
 - cv2.imshow('blurred',blurred)
 
 - cv2.imwrite('coin2_GaussianBlur.png', blurred)
 
 - print('高斯模糊的目的是把雜訊過濾掉')
 
 - print('邊緣偵測,邊緣偵測不代表輪廓處理')
 
 - edged = cv2.Canny(blurred, 30, 150)
 
 - cv2.imshow('Canny',edged)
 
 - cv2.imwrite('coin2_Canny.png', edged)
 
 - cv2.waitKey()
 
  複製代碼- import cv2,coin_2
 
 - gray = coin_2.gray
 
 - image = coin_2.image
 
 - blueeed = coin_2.blurred
 
 - edged = coin_2.edged
 
 - #edged2 = coins_2.edged2
 
 - print('輪廓處理')
 
 - contours , hierarchy = cv2.findContours(
 
 -     edged.copy(),cv2.RETR_EXTERNAL,
 
 -     cv2.CHAIN_APPROX_SIMPLE
 
 - )
 
 - print('count1:',len(contours))
 
  
- # contours , hierarchy = cv2.findContours(
 
 - #     edged2.copy(),cv2.RETR_EXTERNAL,
 
 - #     cv2.CHAIN_APPROX_SIMPLE
 
 - # )
 
 - print('count2:',len(contours))
 
 - print('contours:',contours)
 
 - print('hierarchy:',hierarchy)
 
 - coins = image.copy()
 
 - cv2.drawContours(coins, contours,-1,(0,255,0),1)
 
 - cv2.imshow('coins',coins)
 
 - cv2.waitKey()
 
  複製代碼 灰階圖 
Python cv2 輪廓描繪 
 
 
 
 
高斯模糊圖 
Python cv2 輪廓描繪 
 
 
 
 
Canny圖 
Python cv2 輪廓描繪 
 
 
 
輪廓描繪圖 
Python cv2 輪廓描繪 
 
 
 
文章出處:網頁設計,網站架設 ,網路行銷,網頁優化,SEO - NetYea 網頁設計 
 |   
 
 
 
 |