鱼C论坛

 找回密码
 立即注册
查看: 1013|回复: 3

[已解决]将其中的canny检测算子,更改为sobel检测算子。请在原代码更改谢谢

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

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

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

x
import cv2
import numpy as np
# 1. 分割遥感影像为多块图幅
def split_image(image, block_size):
    height, width = image.shape[:2]
    block_images = []
    for i in range(0, height, block_size):
        for j in range(0, width, block_size):
            block = image[i:i+block_size, j:j+block_size]
            block_images.append(block)
    return block_images

# 2. 分别对每块图幅转为灰度图像
def convert_to_gray(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return gray_image

# 3. 对各个图像进行边缘检测
def edge_detection(image):
    edges = cv2.Canny(image, 0.5, 2)
    return edges


# 4. 消除噪声干扰
def remove_noise(image):
    denoised_image = cv2.medianBlur(image, 5)
    return denoised_image

# 5. 将各个图幅合并为一个图幅

def merge_images(images, block_size):
    num_blocks = len(images)
    rows = int(np.ceil(np.sqrt(num_blocks)))  # 向上取整
    cols = rows
    merged_image = np.zeros((rows * block_size, cols * block_size), dtype=np.uint8)
    for i, image in enumerate(images):
        x = (i % cols) * block_size
        y = (i // cols) * block_size
        merged_image[y:y+image.shape[0], x:x+image.shape[1]] = image
    return merged_image

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

# 示例代码
image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"
block_size = 800

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

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

# 2. 分别对每块图幅转为灰度图像
gray_images = [convert_to_gray(block) for block in block_images]

# 3. 对各个图像进行边缘检测
edges_images = [edge_detection(gray) for gray in gray_images]

# 4. 消除噪声干扰
denoised_images = [remove_noise(edges) for edges in edges_images]

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_images, block_size)

# 6. 导出结果
export_result(merged_image, "output.jpg")
最佳答案
2023-8-7 16:57:57
import cv2import numpy as np# 1. 分割遥感影像为多块图幅def split_image(image, block_size):    height, width = image.shape[:2]    block_images = []    for i in range(0, height, block_size):        for j in range(0, width, block_size):            block = image[i:i+block_size, j:j+block_size]            block_images.append(block)    return block_images# 2. 分别对每块图幅转为灰度图像def convert_to_gray(image):    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)    return gray_image# 3. 对各个图像进行边缘检测(使用Sobel算子)def edge_detection(image):    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 = cv2.magnitude(sobel_x, sobel_y)    edges = cv2.convertScaleAbs(edges)    return edges# 4. 消除噪声干扰def remove_noise(image):    denoised_image = cv2.medianBlur(image, 5)    return denoised_image# 5. 将各个图幅合并为一个图幅def merge_images(images, block_size):    num_blocks = len(images)    rows = int(np.ceil(np.sqrt(num_blocks)))  # 向上取整    cols = rows    merged_image = np.zeros((rows * block_size, cols * block_size), dtype=np.uint8)    for i, image in enumerate(images):        x = (i % cols) * block_size        y = (i // cols) * block_size        merged_image[y:y+image.shape[0], x:x+image.shape[1]] = image    return merged_image# 6. 导出结果def export_result(image, file_path):    cv2.imwrite(file_path, image)# 示例代码image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"block_size = 800# 读取遥感影像image = cv2.imread(image_path)# 1. 分割遥感影像为多块图幅block_images = split_image(image, block_size)# 2. 分别对每块图幅转为灰度图像gray_images = [convert_to_gray(block) for block in block_images]# 3. 对各个图像进行边缘检测edges_images = [edge_detection(gray) for gray in gray_images]# 4. 消除噪声干扰denoised_images = [remove_noise(edges) for edges in edges_images]# 5. 将各个图幅合并为一个图幅merged_image = merge_images(denoised_images, block_size)# 6. 导出结果export_result(merged_image, "output.jpg")
注意,在第3步中,我将边缘检测算子从Canny改为了Sobel。使用Sobel算子可以获得基于梯度的边缘信息。

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

使用道具 举报

发表于 2023-8-7 16:57:57 | 显示全部楼层    本楼为最佳答案   
import cv2import numpy as np# 1. 分割遥感影像为多块图幅def split_image(image, block_size):    height, width = image.shape[:2]    block_images = []    for i in range(0, height, block_size):        for j in range(0, width, block_size):            block = image[i:i+block_size, j:j+block_size]            block_images.append(block)    return block_images# 2. 分别对每块图幅转为灰度图像def convert_to_gray(image):    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)    return gray_image# 3. 对各个图像进行边缘检测(使用Sobel算子)def edge_detection(image):    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 = cv2.magnitude(sobel_x, sobel_y)    edges = cv2.convertScaleAbs(edges)    return edges# 4. 消除噪声干扰def remove_noise(image):    denoised_image = cv2.medianBlur(image, 5)    return denoised_image# 5. 将各个图幅合并为一个图幅def merge_images(images, block_size):    num_blocks = len(images)    rows = int(np.ceil(np.sqrt(num_blocks)))  # 向上取整    cols = rows    merged_image = np.zeros((rows * block_size, cols * block_size), dtype=np.uint8)    for i, image in enumerate(images):        x = (i % cols) * block_size        y = (i // cols) * block_size        merged_image[y:y+image.shape[0], x:x+image.shape[1]] = image    return merged_image# 6. 导出结果def export_result(image, file_path):    cv2.imwrite(file_path, image)# 示例代码image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"block_size = 800# 读取遥感影像image = cv2.imread(image_path)# 1. 分割遥感影像为多块图幅block_images = split_image(image, block_size)# 2. 分别对每块图幅转为灰度图像gray_images = [convert_to_gray(block) for block in block_images]# 3. 对各个图像进行边缘检测edges_images = [edge_detection(gray) for gray in gray_images]# 4. 消除噪声干扰denoised_images = [remove_noise(edges) for edges in edges_images]# 5. 将各个图幅合并为一个图幅merged_image = merge_images(denoised_images, block_size)# 6. 导出结果export_result(merged_image, "output.jpg")
注意,在第3步中,我将边缘检测算子从Canny改为了Sobel。使用Sobel算子可以获得基于梯度的边缘信息。

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

使用道具 举报

发表于 2023-8-7 16:58:00 | 显示全部楼层
在原代码中,将Canny边缘检测算子更改为Sobel边缘检测算子,请看下面的修改后的代码:
import cv2
import numpy as np

# 1. 分割遥感影像为多块图幅
def split_image(image, block_size):
    height, width = image.shape[:2]
    block_images = []
    for i in range(0, height, block_size):
        for j in range(0, width, block_size):
            block = image[i:i+block_size, j:j+block_size]
            block_images.append(block)
    return block_images

# 2. 分别对每块图幅转为灰度图像
def convert_to_gray(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return gray_image

# 3. 对各个图像进行边缘检测
def edge_detection(image):
    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 = np.sqrt(np.square(sobel_x) + np.square(sobel_y))
    edges = edges.astype(np.uint8)
    return edges

# 4. 消除噪声干扰
def remove_noise(image):
    denoised_image = cv2.medianBlur(image, 5)
    return denoised_image

# 5. 将各个图幅合并为一个图幅
def merge_images(images, block_size):
    num_blocks = len(images)
    rows = int(np.ceil(np.sqrt(num_blocks)))  # 向上取整
    cols = rows
    merged_image = np.zeros((rows * block_size, cols * block_size), dtype=np.uint8)
    for i, image in enumerate(images):
        x = (i % cols) * block_size
        y = (i // cols) * block_size
        merged_image[y:y+image.shape[0], x:x+image.shape[1]] = image
    return merged_image

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

# 示例代码
image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"
block_size = 800

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

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

# 2. 分别对每块图幅转为灰度图像
gray_images = [convert_to_gray(block) for block in block_images]

# 3. 对各个图像进行边缘检测
edges_images = [edge_detection(gray) for gray in gray_images]

# 4. 消除噪声干扰
denoised_images = [remove_noise(edges) for edges in edges_images]

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_images, block_size)

# 6. 导出结果
export_result(merged_image, "output.jpg")

这段修改后的代码将使用Sobel边缘检测算子进行图像边缘检测。希望对你有所帮助!如果还有其他问题,请随时提问。
如果问题已经解决,请设置最佳答案
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-8-7 16:58:00 | 显示全部楼层
import cv2
import numpy as np

# 1. 分割遥感影像为多块图幅
def split_image(image, block_size):
    height, width = image.shape[:2]
    block_images = []
    for i in range(0, height, block_size):
        for j in range(0, width, block_size):
            block = image[i:i+block_size, j:j+block_size]
            block_images.append(block)
    return block_images

# 2. 分别对每块图幅转为灰度图像
def convert_to_gray(image):
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    return gray_image

# 3. 对各个图像进行边缘检测
def edge_detection(image):
    edges = cv2.Sobel(image, cv2.CV_64F, 1, 1, ksize=3)
    edges = cv2.convertScaleAbs(edges)
    return edges

# 4. 消除噪声干扰
def remove_noise(image):
    denoised_image = cv2.medianBlur(image, 5)
    return denoised_image

# 5. 将各个图幅合并为一个图幅
def merge_images(images, block_size):
    num_blocks = len(images)
    rows = int(np.ceil(np.sqrt(num_blocks)))  # 向上取整
    cols = rows
    merged_image = np.zeros((rows * block_size, cols * block_size), dtype=np.uint8)
    for i, image in enumerate(images):
        x = (i % cols) * block_size
        y = (i // cols) * block_size
        merged_image[y:y+image.shape[0], x:x+image.shape[1]] = image
    return merged_image

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

# 示例代码
image_path = r"C:\Users\WINDOWS\Desktop\taiyuan\python\dune\dune.tif"
block_size = 800

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

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

# 2. 分别对每块图幅转为灰度图像
gray_images = [convert_to_gray(block) for block in block_images]

# 3. 对各个图像进行边缘检测
edges_images = [edge_detection(gray) for gray in gray_images]

# 4. 消除噪声干扰
denoised_images = [remove_noise(edges) for edges in edges_images]

# 5. 将各个图幅合并为一个图幅
merged_image = merge_images(denoised_images, block_size)

# 6. 导出结果
export_result(merged_image, "output.jpg")

将边缘检测算子从Canny更改为Sobel的方法是使用cv2.Sobel函数来进行边缘检测。在原代码中的edge_detection函数中,将cv2.Canny替换为cv2.Sobel,并指定相关参数即可。Sobel函数的第一个参数是要进行边缘检测的图像,第二个参数是输出图像的数据类型,第三和第四个参数是指定x和y方向的导数的阶数,第五个参数是指定卷积核的大小。在本例中,我们使用了3x3的卷积核。最后,我们使用cv2.convertScaleAbs函数将结果转换为无符号8位整数类型。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-12-25 09:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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