欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python实现ROA算子边缘检测算法

程序员文章站 2022-06-21 23:19:37
python实现roa算子边缘检测算法的具体代码,供大家参考,具体内容如下代码import numpy as npimport cv2 as cvdef roa(image_path, save_pa...

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)

运算结果

运算前

python实现ROA算子边缘检测算法

运算后

python实现ROA算子边缘检测算法

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: python 边缘检测