c语言编写用opencv将路径/root/photo/compressphoto中的.jpg图片解压缩到路径/root/photo/decompressphoto中,给出相应程序和对应的Makefile文件,注意opencv文件处于/root/opencv-4.5.1文件夹中。
时间: 2023-05-27 13:06:58 浏览: 119
c语言编写 图片的压缩、解压
5星 · 资源好评率100%
以下是相应的程序和Makefile文件:
程序:
```c
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
if ( argc != 3 )
{
printf("usage: %s <compress_folder> <decompress_folder>\n", argv[0]);
return -1;
}
char* compress_folder = argv[1];
char* decompress_folder = argv[2];
// Open the compress folder
DIR* dir = opendir(compress_folder);
if (dir == NULL) {
printf("Cannot open directory %s\n", compress_folder);
return -1;
}
// Loop through all the files in the compress folder
struct dirent* entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_REG) {
const char* filename = entry->d_name;
char compress_path[200];
sprintf(compress_path, "%s/%s", compress_folder, filename);
// Read the compressed image
Mat img = imread(compress_path, IMREAD_UNCHANGED);
if (img.empty()) {
printf("Cannot read image %s\n", compress_path);
continue;
}
// Decompress the image
Mat decompressed_img;
cv::decompress(img, decompressed_img);
// Save the decompressed image
char decompress_path[200];
sprintf(decompress_path, "%s/%s", decompress_folder, filename);
imwrite(decompress_path, decompressed_img);
}
}
closedir(dir);
return 0;
}
```
Makefile文件:
```
CC = g++
CFLAGS = -Wall -I/root/opencv-4.5.1/include
LDFLAGS = -L/root/opencv-4.5.1/lib `pkg-config --libs opencv`
all: decompress_photo
decompress_photo: decompress_photo.o
$(CC) -o $@ $^ $(LDFLAGS)
decompress_photo.o: decompress_photo.cpp
$(CC) $(CFLAGS) -c -o $@ $<
clean:
rm -f *.o decompress_photo
```
注意:在Makefile文件中,需要修改相应的opencv库路径和头文件路径。
阅读全文