我在OpenCV C ++中有一个代码,我必须应用于二十个或更多图像。现在我逐个拆分,每次都改变图像的名称。有没有办法可以调用所有图像并将它们提交给该过程并保存所有图像?
我正在使用它:
Mat input_image = imread("img1.jpg");
用于加载图片。这个:
imwrite("output_image.jpg", output_image);
用于保存项目文件夹中的图像。
更新
我这样做但是没有用:
int main(int argc, char **argv){
char name[50];
int i=0;
Mat output_image;
while(1){
sprintf(name, "cam%d.jpg", i);
//Load the image
Mat input_image = imread(name, 1);
if(!input_image.data) break;
//Image RGB to Grayscale
cvtColor(input_image, output_image, CV_RGB2GRAY);
//Applying Gaussian
GaussianBlur(output_image, output_image, Size(21.0, 21.0), 50.0);
//applying adaptive threshold
adaptiveThreshold(output_image, output_image, 255, ADAPTIVE_THRESH_MEAN_C, THRESH_BINARY_INV, 111, -20);
sprintf(name,"cam%d-bin.jpg",i);
//save the image
imwrite(name, output_image);
i++;
}
}
答案 0 :(得分:1)
您可以尝试以下代码。假设您的图像名称如img0.jpg,img1.jpg,img2.jpg等...
char name[10];
int i=0;
Mat gray;
while(1){
sprintf(name,"img%d.jpg",i);
Mat src= imread(name,1);
if(!src.data ) break;
cvtColor(src,gray,CV_BGR2GRAY);
sprintf(name,"gray%d.jpg",i);
imwrite(name, gray);
imshow("src",src);
imshow("result",gray);
i++;
waitKey();
}
答案 1 :(得分:-1)
试试这个,
const char* DATASET("D:/Data/input_image_directory/");
const int total_images = 20;
int main(int argc, char** argv)
{
std::string file_directory;
int count;
if (argc > 1)
{
file_directory = argv[1];
count = argv[2];
}
else
{
file_directory = DATASET->c_str();
count = total_images;
}
for (int i=0; i<count; ++i)
{
// This is assuming your file names are in the structure image000.png, image001.png,...
cv::Mat input_Mat = cv::imread( cv::format( "%s/image%03d.png", file_directory, i ), CV_LOAD_IMAGE_ANYCOLOR );
cv::namedWindow ("input_image", CV_WINDOW_AUTOSIZE);
cv::imshow( "input_image", input_Mat);
// Do processing
// Some more processing
}
return EXIT_SUCCESS;
}