1, BGRBGRBGR 互换BG即可
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;
}
- 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