fanfuhan OpenCV 教學008 ~ opencv-008-圖像通道的分離與合併 [過濾特定顏色干擾訊號]

fanfuhan OpenCV 教學008 ~ opencv-008-圖像通道的分離與合併 [過濾特定顏色干擾訊號]

fanfuhan OpenCV 教學008 ~ opencv-008-圖像通道的分離與合併 [過濾特定顏色干擾訊號]

資料來源: https://fanfuhan.github.io/

https://fanfuhan.github.io/2019/03/24/opencv-008/

GITHUB:https://github.com/jash-git/fanfuhan_ML_OpenCV

C++

#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

/*
 * 图像通道分离与合并
 */
int main() {
    Mat src = imread("../images/baboon.jpg");
    if (src.empty()) {
        cout << "could not load image.." << endl;
    }
    imshow("input", src);

    vector<Mat> mv; // mv用于存储图像分离后各通道像素
    Mat dst1, dst2, dst3;

    // 令蓝色通道为0
    split(src, mv);
    mv[0] = Scalar(0);
    merge(mv, dst1);
    imshow("blue == 0", dst1);

    // 令绿色通道为0
    split(src, mv);
    mv[1] = Scalar(0);
    merge(mv, dst2);
    imshow("green == 0", dst2);

    // 令红色通道为0
    split(src, mv);
    mv[2] = Scalar(0);
    merge(mv, dst3);
    imshow("red == 0", dst3);

    waitKey(0);
    return 0;
}

Python

import cv2 as cv

src = cv.imread("../images/baboon.jpg")
cv.namedWindow("input", cv.WINDOW_AUTOSIZE)
cv.imshow("input", src)

# 蓝色通道为零
mv = cv.split(src)
mv[0][:, :] = 0
dst1 = cv.merge(mv)
cv.imshow("output1", dst1)

# 绿色通道为零
mv = cv.split(src)
mv[1][:, :] = 0
dst2 = cv.merge(mv)
cv.imshow("output2", dst2)

# 红色通道为零
mv = cv.split(src)
mv[2][:, :] = 0
dst3 = cv.merge(mv)
cv.imshow("output3", dst3)

cv.waitKey(0)
cv.destroyAllWindows()

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *