python實現ROA算子邊緣檢測算法

python實現ROA算子邊緣檢測算法的具體代碼,供大傢參考,具體內容如下

代碼

import numpy as np
import cv2 as cv


def ROA(image_path, save_path, threshold):
 img = cv.imread(image_path)
 image = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
 new = np.zeros((512, 512), dtype=np.float64) # 開辟存儲空間
 width = img.shape[0]
 heigh = img.shape[1]
 for i in range(width):
 for j in range(heigh):
  if i == 0 or j == 0 or i == width - 1 or j == heigh - 1:
  new[i, j] = image[i, j]
  continue
  print(image[i, j])
  if image[i, j] < 60:
  continue
  num_sum = 0.0
  u1 = (image[i - 1, j - 1] + image[i, j - 1] + image[i + 1, j - 1]) / 3
  u2 = (image[i - 1, j + 1] + image[i, j + 1] + image[i + 1, j + 1]) / 3
  r12 = 1.0
  if float(u2) - 0.0 > 1e6:
  r12 = float(u1) / float(u2)
  if float(u1) - 0.0 > 1e6:
  r12 = float(u2) / float(u1)
  num_sum += r12

  u1 = (image[i - 1, j - 1] + image[i, j - 1] + image[i - 1, j]) / 3
  u2 = (image[i + 1, j] + image[i + 1, j + 1] + image[i, j + 1]) / 3
  r12 = 1.0
  if float(u2) - 0.0 > 1e6:
  r12 = float(u1) / float(u2)
  if float(u1) - 0.0 > 1e6:
  r12 = float(u2) / float(u1)
  num_sum += r12

  u1 = (image[i - 1, j - 1] + image[i - 1, j] + image[i - 1, j + 1]) / 3
  u2 = (image[i + 1, j - 1] + image[i + 1, j] + image[i + 1, j + 1]) / 3
  r12 = 1.0
  if float(u2) - 0.0 > 1e6:
  r12 = float(u1) / float(u2)
  if float(u1) - 0.0 > 1e6:
  r12 = float(u2) / float(u1)
  num_sum += r12

  u1 = (image[i - 1, j] + image[i - 1, j + 1] + image[i, j + 1]) / 3
  u2 = (image[i, j - 1] + image[i + 1, j - 1] + image[i + 1, j]) / 3
  r12 = 1.0
  if float(u2) - 0.0 > 1e6:
  r12 = float(u1) / float(u2)
  if float(u1) - 0.0 > 1e6:
  r12 = float(u2) / float(u1)
  num_sum += r12
  new[i, j] = num_sum / 4.0
  if new[i, j] > threshold:
  new[i, j] = 100
  print(new[i, j])

 print(new)

 cv.imwrite(save_path, new)


if __name__ == "__main__":
 image_path = r""
 save_path = r""
 threshold = 
 ROA(image_path, save_path, threshold)

運算結果

運算前

運算後

以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。