#include <fstream>
#include <string>
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
char buffer[50];
char buffer1[50];
//图片格式为1.jpg,2.jpg....
for (int i = 1; i<201; i++) {
//将数字字母拼接在一起得到读取灰度图的路径
sprintf(buffer, "H:\\VOC2007\\train3\\%d.jpg", i);
//从指定路径buffer中读取图片
Mat img = imread(buffer);
//cout << img.channels() << endl;
//拼接RGB图存储路径
sprintf(buffer1, "H:\\VOC2007\\train3_ad\\%d.jpg", i);
//Mat rgbImg(img.size(),CV_8UC3);
//cvtColor(img, rgbImg, COLOR_GRAY2BGR);
//保存灰度图像到指定路径
imwrite(buffer1, img);
}
// cvWaitKey();
return 0;
}
imread与imwrite直接将单通道图片转换为3通道图片。