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)

运算结果
运算前
python实现ROA算子边缘检测算法(以光学图像为例)_第1张图片
运算后
python实现ROA算子边缘检测算法(以光学图像为例)_第2张图片

你可能感兴趣的:(文件处理小程序,算法,python,opencv,边缘检测)