使用 OpenCV 从 RGB 图像中提取单个通道

RGB 图像具有三个通道:红色、绿色和蓝色。在计算机视觉应用程序中进行图像预处理时,可能需要从 RGB 图像中提取单独的通道,例如红色、绿色和蓝色。本教程演示了如何使用 OpenCV 执行此操作。

import cv2

img = cv2.imread('test.jpg')

blueChannel = img[:, :, 0]
greenChannel = img[:, :, 1]
redChannel = img[:, :, 2]

cv2.imshow('Original image', img)
cv2.imshow('Blue channel', blueChannel)
cv2.imshow('Green channel', greenChannel)
cv2.imshow('Red channel', redChannel)
cv2.waitKey(0)
cv2.destroyAllWindows()
#include <opencv2/opencv.hpp>

using namespace cv;

int main()
{
    Mat img = imread("test.jpg");
    Mat blueChannel, greenChannel, redChannel;

    extractChannel(img, blueChannel, 0);
    extractChannel(img, greenChannel, 1);
    extractChannel(img, redChannel, 2);

    imshow("RGB image", img);
    imshow("Blue channel", blueChannel);
    imshow("Green channel", greenChannel);
    imshow("Red channel", redChannel);
    waitKey(0);
    destroyAllWindows();

    return 0;
}
package app;

import org.opencv.core.*;
import org.opencv.highgui.HighGui;
import org.opencv.imgcodecs.Imgcodecs;

public class Main
{
    static { System.loadLibrary(Core.NATIVE_LIBRARY_NAME); }

    public static void main(String[] args)
    {
        Mat img = Imgcodecs.imread("test.jpg");
        Mat blueChannel = new Mat(), greenChannel = new Mat(), redChannel = new Mat();

        Core.extractChannel(img, blueChannel, 0);
        Core.extractChannel(img, greenChannel, 1);
        Core.extractChannel(img, redChannel, 2);

        HighGui.imshow("RGB image", img);
        HighGui.imshow("Blue channel", blueChannel);
        HighGui.imshow("Green channel", greenChannel);
        HighGui.imshow("Red channel", redChannel);
        HighGui.waitKey(0);
        HighGui.destroyAllWindows();

        System.exit(0);
    }
}

效果图:

使用 OpenCV 从 RGB 图像中提取单个通道

版权声明:本文内容转自互联网,本文观点仅代表作者本人。本站仅提供信息存储空间服务,所有权归原作者所有。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至1393616908@qq.com 举报,一经查实,本站将立刻删除。

(0)

相关推荐

发表回复

登录后才能评论