Record_ 08_使用opencv将彩色图像的RGB形式转化为HSV形式,并将HSV各个通道图像提取,显示和保存

代码目的

将彩色图像的RGB形式转化为HSV形式,并提取HSV模式下H通道、V通道和S通道的图像并保存。

代码如下

import cv2
import os
import time
import numpy as np

def channel_extract(im,root,rows,cols,channel):
    
    image_temp = np.zeros((rows, cols))

    # for in 语句,遍历数组,但不能修改数组
    for i in im:
        for j in i:
            pass

    # 给created数据赋值
    for i in range(0, rows):
        for j in range(0, cols):
            image_tempd[i, j] = im[i, j, channel]

    # 必须加这一条语句, 否则无法正确显示图像
    image_temp = image_temp.astype(np.uint8)

    extract_path = os.path.join(root, "{}.jpg".format(time.time() * 10000))
    cv2.imwrite(extract_path, image_temp)

    return image_temp

def image_extract():
    image_rgb = "C:/0101/test/01_image_rgb"
    image_gray = "C:/0101/02_image_gray"
    image_hsv = "C:/0101/03_image_hsv"
    image_rgb_B = "C:/0101/test/image_extract_rgb/channel_B_channel_0"
    image_rgb_G = "C:/0101/test/image_extract_rgb/channel_G_channel_1"
    image_rgb_R = "C:/0101/test/image_extract_rgb/channel_R_channel_2"
    image_hsv_H = "C:/0101/test/image_extract_hsv/0_channel_H_channel"
    image_hsv_S = "C:/0101/test/image_extract_hsv/1_channel_S_channel"
    image_hsv_V = "C:/0101/test/image_extract_hsv/2_channel_V_channel"

    for parent, _, fnames in os.walk(image_rgb):
        for fname in fnames:
            if fname.endswith("png") or fname.endswith("jpg"):
                fname = os.path.join(parent, fname)

                # 显示彩色图像
                im = cv2.imread(fname)
                cv2.imshow("image_RGB",im)
                cv2.waitKey(0)
                cv2.destroyAllWindows()

                # 获得图像大小
                rows, cols, _ = im.shape
                #
                # # B通道:通道0图像
                # rgb_0 = channel_extract(im, image_rgb_B, rows, cols, channel=0)
                # cv2.imshow("RGB_channel_0", rgb_0)
                # # cv2.waitKey(1000)
                # # cv2.destroyAllWindows()
                #
                # # G通道:通道1图像
                # rgb_1 = channel_extract(im, image_rgb_G, rows, cols, channel=1)
                # cv2.imshow("RGB_channel_1", rgb_1)
                # # cv2.waitKey(1000)
                # # cv2.destroyAllWindows()
                #
                # # R通道:通道2图像
                # rgb_2 = channel_extract(im, image_rgb_R, rows, cols, channel=2)
                # cv2.imshow("RGB_channel_2", rgb_2)
                # # cv2.waitKey(1000)
                # # cv2.destroyAllWindows()
                #
                # # # 显示灰度图像
                # im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
                # cv2.imshow("image_gray",im_gray)
                # cv2.waitKey(1000)
                # cv2.destroyAllWindows()
                # extract_path = os.path.join(image_gray, "{}.jpg".format(time.time() * 10000))
                # cv2.imwrite(extract_path, im_gray)

                im_hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
                # cv2.imshow("image_HSV", im_hsv)
                # cv2.waitKey(0)
                # cv2.destroyAllWindows()
                extract_path = os.path.join(image_hsv, "{}.jpg".format(time.time() * 10000))
                cv2.imwrite(extract_path, im_hsv)

                # H通道:通道0图像
                hsv_0 = channel_extract(im_hsv, image_hsv_H, rows, cols, channel=0)
                # cv2.imshow("HSV_channel_0", hsv_0)
                # cv2.waitKey(1000)
                # cv2.destroyAllWindows()

                # S通道:通道1图像
                hsv_1 = channel_extract(im_hsv, image_hsv_S, rows, cols, channel=1)
                # cv2.imshow("HSV_channel_1", hsv_1)
                # cv2.waitKey(1000)
                # cv2.destroyAllWindows()

                # V通道:通道2图像
                hsv_2 = channel_extract(im_hsv, image_hsv_V, rows, cols, channel=2)
                # cv2.imshow("HSV_channel_2", hsv_2)
                # cv2.waitKey(1000)
                # cv2.destroyAllWindows()


if __name__ == '__main__':
    image_extract()

文章最后有微信公众号:惟CPP,欢迎关注,一起交流学习进步~

你可能感兴趣的:(opencv,cv)