BGR转RGB

以下提供了三种转换方法:
1, BGRBGRBGR 互换BR即可

void BGR2RGB(unsigned char *buf,int image_width,int image_height)
{
    unsigned char tmp;
    for(int i =0;i<image_height;i++){
        for(int j=0;j<image_width;j++){
            tmp = *buf;//缓存B
            *buf = *(buf+2);//R->B
            *(buf+2) = tmp;//B->R
            buf += 3;
        }
    }
}
static int BGR2RGB(const unsigned char* bgr_image,
                   const int image_width,
                   const int image_height,
                   unsigned char* rgb_image) {
    if (!bgr_image || !rgb_image)
        return 1;
    for (int i = 0; i < image_width * image_height; i++) {
        unsigned char b_value = *bgr_image++;
        unsigned char g_value = *bgr_image++;
        unsigned char r_value = *bgr_image++;
        *rgb_image++ = r_value;
        *rgb_image++ = g_value;
        *rgb_image++ = b_value;
    }
    return 0;
}
  1. use opencv
#include <opencv2/opencv.hpp>

int main(int argc, char* argv[]) {
    cv::Mat img_BGR = cv::imread("1.jpg");
    cv::Mat img_RGB;
    cv::cvtColor(img_BGR, img_RGB, cv::COLOR_BGR2RGB);
    cv::imwrite("2.jpg", img_RGB);
    cv::imshow("window", img_RGB);
    cv::waitKey(0);
    return 0;

}
pkg-config --modversion opencv4
g++ test.cpp -o main $(pkg-config --cflags --libs opencv4
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值