How to perform three-channel Canny edge detection on color images

How to perform three-channel Canny edge detection on color images

    This article mainly records how to perform three-channel Canny edge detection on color images.

0. Main steps

    The three-channel Canny edge detection of the color image mainly includes the following two steps:
    (1) Separate the color image into images of three channels;
    (2) Perform Canny edge detection on the images of the three channels;
    (3 ) to show the edge detection results.

1. Separate the color image into 3-channel images

    Separate the color image Airplane into three channel images, Airplane_B.jpg, Airplane_G.jpg, Airplane_R.jpg. The specific code is:

# coding :UTF-8
# 文件功能: 代码实现彩色图像三通道分别提取的功能
# 开发人员: XXX
# 开发时间: 2022/6/11 6:20 下午
# 文件名称: Three_Channel_Extraction_of_Color_Images.py
# 开发工具: PyCharm


# 当采用CV2时,读取到的彩色图像的三通道顺序为:BGR
# 本程序是实现将一个彩色图像分离出3个通道的图像,然后分别保存为3张图像,并用Orinal_Canny_Edge_Detection.py实现边缘检测。

import cv2
import numpy as np

img = cv2.imread('image/Airplane.tiff')  # 读取图像

# 显示彩色图像
cv2.imshow("img", img)
cv2.waitKey(0)

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 显示灰度图像
cv2.imshow("gray", gray)
cv2.waitKey(0)

# 得到图像的大小
rows, cols, _ = img.shape
print(rows, cols)

# 初始化一个大小一样的图像,元素均初始化为0
created = np.zeros((rows, cols))

# for in 语句,遍历数组,但不能修改数组
for i in img:
    for j in i:
        #print(j)
        pass

# 给数组赋值
for i in range(0, rows):
    for j in range(0, cols):
        created[i, j] = img[i, j, 2]   # 第3个参数,可以是0,1,2,分别表示B,G,R三个通道

print(created.shape)  # 输出创建图像的大小
# 必须加上下面这一条语句,否则无法正确显示图像
created = created.astype(np.uint8)  # 转换数据类型,才能正确的显示图像
cv2.imshow('created', created)
cv2.imwrite('image/Airplane_R.jpg', created)
cv2.waitKey(0)

2. Grayscale image to achieve Canny edge detection

    The images of the three channels are subjected to Canny edge detection. The specific code is as follows:

# coding :UTF-8
# 文件功能: 代码实现最简单的Canny edge detection边缘检测的功能
# 开发人员: XXX
# 开发时间: 2022/6/11 5:18 下午
# 文件名称: Orinal_Canny_Edge_Detection.py
# 开发工具: PyCharm


import cv2
import numpy as np

from PIL import Image

lower = 30  # 最小阈值
upper = 70  # 最大阈值

img_path = 'image/Airplane_R.jpg'   # 指定测试图像路径

gray = cv2.imread(img_path, 0)   # 读取灰度图像
edge = cv2.Canny(gray, lower, upper)   # Canny 图像边缘检测

contrast = np.concatenate([edge, gray], 1)   # 图像拼接
Image.fromarray(contrast).save('image/Airplane_R_edge.png', format='PNG')   # 保存图像

3. Display edge detection results

    (1) Original color image
insert image description here

    (2) Separated 3-channel images
    1, Airplane_B.jpg
insert image description here
    2, Airplane_G.jpg
insert image description here
    3, Airplane_R.jpg
insert image description here

    (3) Result of 3-channel edge detection
    1. Airplane_B_edge.jpg
insert image description here

    2、Airplane_G_edge.jpg
insert image description here

    3、Airplane_R_edge.jpg
insert image description here

Guess you like

Origin blog.csdn.net/weixin_43981621/article/details/125238665