图像处理100问——1.通道交换
Posted costanza
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图像处理100问——1.通道交换相关的知识,希望对你有一定的参考价值。
任务:读取图像将RGB通道转换为BGR通道
代码:
python
import cv2
# function: BGR -> RGB
def BGR2RGB(img):
b = img[:, :, 0].copy()
g = img[:, :, 1].copy()
r = img[:, :, 2].copy()
# RGB > BGR
img[:, :, 0] = r
img[:, :, 1] = g
img[:, :, 2] = b
return img
# Read image
img = cv2.imread("./imgs/lena.jpg")
# BGR -> RGB
img = BGR2RGB(img)
# Save result
cv2.imshow("result", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
C++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std:
using namespace cv;
// Channel swap
Mat channel_swap(Mat img){
// get height and width
int width = img.cols;
int height = img.rows;
// prepare output
Mat out = Mat::zeros(height, width, CV_8UC3);
// each y, x
for (int y = 0; y < height; y++){
for (int x = 0; x < width; x++){
// R -> B
out.at<Vec3b>(y, x)[0] = img.at<Vec3b>(y, x)[2];
// B -> R
out.at<Vec3b>(y, x)[2] = img.at<Vec3b>(y, x)[0];
// G -> G
out.at<Vec3b>(y, x)[1] = img.at<Vec3b>(y, x)[1];
}
}
return out;
}
int main(int argc, const char* argv[]){
// read image
Mat img = imread("./imgs/lena.jpg", IMREAD_COLOR);
// channel swap
Mat out = channel_swap(img);
imshow("sample", out);
waitKey(0);
destroyAllWindows();
return 0;
}
以上是关于图像处理100问——1.通道交换的主要内容,如果未能解决你的问题,请参考以下文章
SpringCloud系列十一:SpringCloudStream(SpringCloudStream 简介创建消息生产者创建消息消费者自定义消息通道分组与持久化设置 RoutingKey)(代码片段