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)
运算结果
运算前
运算后
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/qq_44813407/article/details/115416819