图像处理与分析 实验四

115次阅读
没有评论

共计 3143 个字符,预计需要花费 8 分钟才能阅读完成。

实验内容:图像的边缘轮廓提取算法:canny 算子、sobel 算子、拉普拉斯算子等

导入图像

import cv2 as cv
import matplotlib.pyplot as plt

img = cv.imread("1.png", 1)
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
# gray = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
plt.imshow(img)

图像处理与分析 实验四

canny 算子

canny_img = cv.Canny(img, 30, 150)
plt.imshow(canny_img, cmap='gray')

图像处理与分析 实验四

sobel 算子

# gray_img = cv.cvtColor(img, cv.COLOR_RGB2GRAY)
sobel_x = cv.Sobel(img, cv.CV_16S, 1, 0, ksize=3)
sobel_y = cv.Sobel(img, cv.CV_16S, 0, 1, ksize=3)

scale_abs_x = cv.convertScaleAbs(sobel_x)
scale_abs_y = cv.convertScaleAbs(sobel_y)
sobel_img = cv.addWeighted(scale_abs_x, 0.5, scale_abs_y, 0.5, 0)
sobel_img = cv.cvtColor(sobel_img, cv.COLOR_RGB2GRAY)
# sobel_img = cv.Sobel(src=gray_img, ddepth=cv.CV_64F, dx=1, dy=1, ksize=1)
plt.imshow(sobel_img, cmap='gray')

图像处理与分析 实验四

拉普拉斯算子

laplacian_img = cv.Laplacian(img, cv.CV_16S, ksize=3)
laplacian_img = cv.convertScaleAbs(laplacian_img)
laplacian_img = cv.cvtColor(laplacian_img, cv.COLOR_RGB2GRAY)
plt.imshow(laplacian_img, cmap='gray')

图像处理与分析 实验四

基于相似度和区分度的边缘检测

原创

import numpy as np

def get_unit_similarity(a_4, b_4, k):
    # a_4 = sorted(a_4)
    # b_4 = sorted(b_4)
    result = 0
    for i in range(4):
        result = result + (abs(int(a_4[i]) - int(b_4[i]))) ** k
    result = result ** (1/k)
    return int(result)

def input_flag(flag, x, y, similarity):
    for i in range(4):
        if flag[x][y][i] == 0:
            flag[x][y][i] = similarity
            break
    return flag

def get_channels_similarity(channel, k, rank):
    row, col = channel.shape
    flag = np.zeros((row - 1, col - 1, 4))
    result = np.zeros((row - 1, col - 1))
    for i in range(row - 2):
        for j in range(col - 2):
            array = [channel[i][j], channel[i][j + 1], channel[i + 1][j + 1], channel[i + 1][j]]
            r_array = [channel[i][j + 1], channel[i + 1][j + 1], channel[i][j + 2], channel[i + 1][j + 2]]
            d_array = [channel[i + 1][j + 1], channel[i + 1][j], channel[i + 2][j + 1], channel[i + 2][j]]
            r_similarity = get_unit_similarity(array, r_array, k)
            d_similarity = get_unit_similarity(array, d_array, k)
            flag = input_flag(flag, i, j, r_similarity)
            flag = input_flag(flag, i, j, d_similarity)
            flag = input_flag(flag, i+1, j, r_similarity)
            flag = input_flag(flag, i, j+1, d_similarity)
            # result[i][j] = (r_similarity + d_similarity) / 2
    for i in range(row-2):
        for j in range(col-2):
            flag4 = sorted(flag[i][j])
            result[i][j] = flag4[4-rank]
    return result

def get_qufendu(array):
    return int(max(array)) - int(min(array))

def get_channels_qufendu(channel):
    row, col = channel.shape
    result = np.zeros((row - 1, col - 1))
    for i in range(row-1):
        for j in range(col-1):
            array = [channel[i][j], channel[i][j + 1], channel[i + 1][j + 1], channel[i + 1][j]]
            qufendu = get_qufendu(array)
            result[i][j] = qufendu
    return result

def mark(channel, similarity, qufendu):
    row, col = similarity.shape
    for i in range(row):
        for j in range(col):
            if similarity[i][j] > 50 and qufendu[i][j] > 20:
                channel[i][j] = 0
            else:
                channel[i][j] = 255
    return channel

def mark_similarity(img, k, rank):
    p = cv.imread(img, 1)
    b, g, r = cv.split(p)
    b_similarity = get_channels_similarity(b, k, rank)
    b_qufendu = get_channels_qufendu(b)
    b = mark(b, b_similarity, b_qufendu)
    g_similarity = get_channels_similarity(g, k, rank)
    g_qufendu = get_channels_qufendu(g)
    g = mark(g, g_similarity, g_qufendu)
    r_similarity = get_channels_similarity(r, k, rank)
    r_qufendu = get_channels_qufendu(r)
    r = mark(r, r_similarity, r_qufendu)
    p = cv.merge((b, g, r))
    # cv.imwrite(img[0:-4] + '_mark.png', p)
    # plt.imshow(p)
    return p
img_addr = '1.png'
p_img = mark_similarity(img_addr, 1, 1)
p_img = cv.cvtColor(p_img, cv.COLOR_BGR2GRAY)
plt.imshow(p_img,cmap='gray')


图像处理与分析 实验四

正文完
 0
icvuln
版权声明:本站原创文章,由 icvuln 于2024-10-06发表,共计3143字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)