在linux系统下,用c语言编写用opencv将路径/root/photo/compressphoto中的.jpg图片解压缩到路径/root/photo/decompressphoto中,给出相应程序和对应的Makefile文件,注意opencv文件处于/root/opencv-4.5.1文件夹中。并说明效果
时间: 2023-05-27 08:07:07 浏览: 46
以下是相应程序和Makefile文件:
程序(decompress.c):
```
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if (argc != 3)
{
printf("Usage: %s <input_folder> <output_folder>\n", argv[0]);
return -1;
}
String inputFolder = argv[1];
String outputFolder = argv[2];
std::vector<String> filenames;
glob(inputFolder, filenames);
for (size_t i = 0; i < filenames.size(); i++)
{
Mat image = imread(filenames[i], IMREAD_UNCHANGED);
if (image.empty())
{
printf("Failed to load image: %s\n", filenames[i].c_str());
continue;
}
String outputPath = outputFolder + "/" + filenames[i].substr(filenames[i].find_last_of("/") + 1);
imwrite(outputPath, image);
}
return 0;
}
```
Makefile文件:
```
CC = g++
CFLAGS = `pkg-config --cflags opencv4`
LIBS = `pkg-config --libs opencv4`
all: decompress
decompress: decompress.o
$(CC) $(CFLAGS) -o decompress decompress.o $(LIBS)
decompress.o: decompress.c
$(CC) $(CFLAGS) -c decompress.c
clean:
rm -f decompress decompress.o
```
使用方法:
1. 将decompress.c和Makefile文件保存在/root/photo/decompressphoto文件夹中。
2. 在终端中进入/root/photo/decompressphoto文件夹。
3. 执行命令make编译程序。
4. 执行命令./decompress /root/photo/compressphoto /root/photo/decompressphoto以解压缩/root/photo/compressphoto中的.jpg图片到/root/photo/decompressphoto中。
效果:
1. /root/photo/compressphoto中有若干个.jpg图片。
2. 执行命令./decompress /root/photo/compressphoto /root/photo/decompressphoto后,/root/photo/decompressphoto中会生成与/root/photo/compressphoto中相同的.jpg图片,且图片内容与原始图片相同。