亲宝软件园·资讯

展开

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

劲酒奶奶 人气:0
想了解python实现ROA算子边缘检测算法的相关内容吗,劲酒奶奶在本文为您仔细讲解python边缘检测算法的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python边缘检测,ROA算子边缘检测算法,python边缘检测算法,下面大家一起来学习吧。

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)

运算结果

运算前

运算后

加载全部内容

相关教程
猜你喜欢
用户评论