鱼C论坛

 找回密码
 立即注册
查看: 1595|回复: 4

请帮我将代码中的cany检测算法换成其他检测方法。

[复制链接]
发表于 2023-8-7 21:06:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码:
import cv2
import numpy as np
# 1. 分割遥感影像为多块图幅
def split_image(image, num_rows, num_cols):
    height, width = image.shape[:2]
    row_height = height // num_rows
    col_width = width // num_cols
    images = []
    for r in range(num_rows):
        for c in range(num_cols):
            start_row = r * row_height
            end_row = start_row + row_height
            start_col = c * col_width
            end_col = start_col + col_width
            sub_image = image[start_row:end_row, start_col:end_col]
            images.append(sub_image)
    return images
# 2. 分别对每块图幅转为灰度图像
def convert_to_grayscale(images):
    grayscale_images = []
    for image in images:
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        grayscale_images.append(gray_image)
    return grayscale_images
# 3. 对各个图像进行边缘检测
def edge_detection(images):
    edges = []
    for image in images:
        edges_image = cv2.Canny(image, 10, 50)  # 调整阈值根据实际情况
        edges.append(edges_image)
    return edges
   
# 4. 消除噪声干扰
def denoise(images):
    denoised_images = []
    for image in images:
        denoised_image = cv2.medianBlur(image, 5)  # 调整核大小根据实际情况
        denoised_images.append(denoised_image)
    return denoised_images
# 5. 将各个图幅合并为一个图幅
def merge_images(images, num_rows, num_cols):
    row_height, col_width = images[0].shape[:2]
    merged_image = np.zeros((row_height*num_rows, col_width*num_cols), dtype=np.uint8)
    i = 0
    for r in range(num_rows):
        for c in range(num_cols):
            start_row = r * row_height
            end_row = start_row + row_height
            start_col = c * col_width
            end_col = start_col + col_width
            merged_image[start_row:end_row, start_col:end_col] = images[i]
            i += 1
    return merged_image
# 6. 导出结果
def export_result(image, filename):
    cv2.imwrite(filename, image)
# 加载遥感影像
image = cv2.imread(r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif")
# 1. 分割遥感影像为多块图幅
num_rows = 3
num_cols = 7
sub_images = split_image(image, num_rows, num_cols)
# 2. 分别对每块图幅转为灰度图像
gray_images = convert_to_grayscale(sub_images)
# 3. 对各个图像进行边缘检测
edges = edge_detection(gray_images)
# 4. 消除噪声干扰
denoised_edges = denoise(edges)
# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_edges, num_rows, num_cols)
# 6. 导出结果
export_result(merged_image, 'path_to_output.jpg')
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-8-7 21:07:07 | 显示全部楼层
要将代码中的Canny边缘检测算法换成其他方法,你可以使用OpenCV提供的其他边缘检测算法,如Sobel算子或Laplacian算子。下面是将代码中的Canny边缘检测替换为Sobel算子的示例代码:
import cv2
import numpy as np

# 3. 对各个图像进行边缘检测(使用Sobel算子)
def edge_detection(images):
    edges = []
    for image in images:
        sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
        sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
        gradient_magnitude = cv2.magnitude(sobel_x, sobel_y)
        edges_image = cv2.convertScaleAbs(gradient_magnitude)
        edges.append(edges_image)
    return edges

# 示例代码
image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"
num_rows = 3
num_cols = 7

# 读取遥感影像
image = cv2.imread(image_path)

# 1. 分割遥感影像为多块图幅
sub_images = split_image(image, num_rows, num_cols)

# 2. 分别对每块图幅转为灰度图像
gray_images = convert_to_grayscale(sub_images)

# 3. 对各个图像进行边缘检测(使用Sobel算子)
edges = edge_detection(gray_images)

# 4. 消除噪声干扰
denoised_edges = denoise(edges)

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_edges, num_rows, num_cols)

# 6. 导出结果
export_result(merged_image, 'path_to_output.jpg')

通过替换 edge_detection 函数中的Canny算法为Sobel算子,你就可以使用Sobel算子进行边缘检测。你也可以根据实际需求进一步修改该函数中的算法或参数来使用其他边缘检测算法,如Laplacian算子等。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-7 21:07:14 | 显示全部楼层
import cv2
import numpy as np

# 1. 分割遥感影像为多块图幅
def split_image(image, num_rows, num_cols):
    height, width = image.shape[:2]
    row_height = height // num_rows
    col_width = width // num_cols
    images = []
    for r in range(num_rows):
        for c in range(num_cols):
            start_row = r * row_height
            end_row = start_row + row_height
            start_col = c * col_width
            end_col = start_col + col_width
            sub_image = image[start_row:end_row, start_col:end_col]
            images.append(sub_image)
    return images

# 2. 分别对每块图幅转为灰度图像
def convert_to_grayscale(images):
    grayscale_images = []
    for image in images:
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        grayscale_images.append(gray_image)
    return grayscale_images

# 3. 使用其他边缘检测方法进行检测
def other_edge_detection(images):
    edges = []
    for image in images:
        # 使用其他边缘检测算法,例如Sobel算子
        # 可根据实际需求选择适合的算法
        edge_image = cv2.Sobel(image, cv2.CV_64F, 1, 1, ksize=3)
        edges.append(edge_image)
    return edges
    
# 4. 消除噪声干扰
def denoise(images):
    denoised_images = []
    for image in images:
        denoised_image = cv2.medianBlur(image, 5)  # 调整核大小根据实际情况
        denoised_images.append(denoised_image)
    return denoised_images

# 5. 将各个图幅合并为一个图幅
def merge_images(images, num_rows, num_cols):
    row_height, col_width = images[0].shape[:2]
    merged_image = np.zeros((row_height*num_rows, col_width*num_cols), dtype=np.uint8)
    i = 0
    for r in range(num_rows):
        for c in range(num_cols):
            start_row = r * row_height
            end_row = start_row + row_height
            start_col = c * col_width
            end_col = start_col + col_width
            merged_image[start_row:end_row, start_col:end_col] = images[i]
            i += 1
    return merged_image

# 6. 导出结果
def export_result(image, filename):
    cv2.imwrite(filename, image)

# 加载遥感影像
image = cv2.imread(r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif")

# 1. 分割遥感影像为多块图幅
num_rows = 3
num_cols = 7
sub_images = split_image(image, num_rows, num_cols)

# 2. 分别对每块图幅转为灰度图像
gray_images = convert_to_grayscale(sub_images)

# 3. 使用其他边缘检测方法进行检测
edges = other_edge_detection(gray_images)

# 4. 消除噪声干扰
denoised_edges = denoise(edges)

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_edges, num_rows, num_cols)

# 6. 导出结果
export_result(merged_image, 'path_to_output.jpg')
如果回答对你有帮助,请给我一个最佳答案!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-7 21:07:20 | 显示全部楼层
代码修改如下:
import cv2import numpy as np# 1. 分割遥感影像为多块图幅def split_image(image, num_rows, num_cols):    height, width = image.shape[:2]    row_height = height // num_rows    col_width = width // num_cols    images = []    for r in range(num_rows):        for c in range(num_cols):            start_row = r * row_height            end_row = start_row + row_height            start_col = c * col_width            end_col = start_col + col_width            sub_image = image[start_row:end_row, start_col:end_col]            images.append(sub_image)    return images# 2. 分别对每块图幅转为灰度图像def convert_to_grayscale(images):    grayscale_images = []    for image in images:        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)        grayscale_images.append(gray_image)    return grayscale_images# 3. 使用其他边缘检测方法代替Canny算法def edge_detection(images):    edges = []    for image in images:        # 使用其他边缘检测方法,这里以Sobel算子为例        gradient_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)        gradient_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)        edges_image = cv2.magnitude(gradient_x, gradient_y)        edges.append(edges_image)    return edges# 4. 消除噪声干扰def denoise(images):    denoised_images = []    for image in images:        # 使用其他滤波方法,这里以高斯滤波为例        denoised_image = cv2.GaussianBlur(image, (5, 5), 0)        denoised_images.append(denoised_image)    return denoised_images# 5. 将各个图幅合并为一个图幅def merge_images(images, num_rows, num_cols):    row_height, col_width = images[0].shape[:2]    merged_image = np.zeros((row_height * num_rows, col_width * num_cols), dtype=np.uint8)    i = 0    for r in range(num_rows):        for c in range(num_cols):            start_row = r * row_height            end_row = start_row + row_height            start_col = c * col_width            end_col = start_col + col_width            merged_image[start_row:end_row, start_col:end_col] = images[i]            i += 1    return merged_image# 6. 导出结果def export_result(image, filename):    cv2.imwrite(filename, image)# 加载遥感影像image = cv2.imread(r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif")# 1. 分割遥感影像为多块图幅num_rows = 3num_cols = 7sub_images = split_image(image, num_rows, num_cols)# 2. 分别对每块图幅转为灰度图像gray_images = convert_to_grayscale(sub_images)# 3. 使用其他边缘检测方法代替Canny算法edges = edge_detection(gray_images)# 4. 消除噪声干扰denoised_edges = denoise(edges)# 5. 将各个图幅合并为一个图幅merged_image = merge_images(denoised_edges, num_rows, num_cols)# 6. 导出结果export_result(merged_image, 'path_to_output.jpg')
这里使用Sobel算子作为替代的边缘检测方法,并使用高斯滤波代替了中值滤波进行噪声消除。你可以根据实际需求选择其他的边缘检测和滤波方法。

如果回答对你有帮助,请给我一个最佳答案!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-7 21:07:55 | 显示全部楼层
将代码中的Canny边缘检测算法换成其他检测方法,可以尝试使用Laplacian算子或Sobel算子进行边缘检测。

1. 使用Laplacian算子进行边缘检测:
def edge_detection(images):
    edges = []
    for image in images:
        edges_image = cv2.Laplacian(image, cv2.CV_8U)
        edges.append(edges_image)
    return edges

2. 使用Sobel算子进行边缘检测:
def edge_detection(images):
    edges = []
    for image in images:
        sobel_x = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3)
        sobel_y = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3)
        edges_image = cv2.magnitude(sobel_x, sobel_y)
        edges.append(edges_image)
    return edges

你可以将其中一种方法替换到原代码中的 edge_detection 函数中,然后重新运行程序,查看结果。记得根据实际情况调整算法的参数和阈值。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-22 01:24

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表