c语言编写用opencv将路径/root/photo/compressphoto中的.jpg图片解压缩到路径/root/photo/decompressphoto中,给出相应程序和对应的Makefile文件,注意opencv文件处于/root/opencv-4.5.1文件夹中。并说明如何使用以及设置参数
时间: 2023-05-27 10:07:02 浏览: 88
程序如下:
```c
#include <opencv2/opencv.hpp>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace cv;
int main(int argc, char** argv) {
if (argc != 3) {
cout << "Usage: ./decompressphoto compressed_path decompressed_path" << endl;
return -1;
}
string compressed_path = argv[1];
string decompressed_path = argv[2];
ifstream fin(compressed_path, ios::binary);
if (!fin) {
cout << "Failed to open file: " << compressed_path << endl;
return -1;
}
vector<uchar> buffer((istreambuf_iterator<char>(fin)), (istreambuf_iterator<char>()));
Mat data(buffer, true);
Mat image = imdecode(data, IMREAD_UNCHANGED);
if (image.empty()) {
cout << "Failed to decode image: " << compressed_path << endl;
return -1;
}
if (!imwrite(decompressed_path, image)) {
cout << "Failed to write image: " << decompressed_path << endl;
return -1;
}
cout << "Decompressed successfully!" << endl;
return 0;
}
```
Makefile文件如下:
```Makefile
CC = g++
CFLAGS = -g -Wall
INCLUDES = -I/root/opencv-4.5.1/include/opencv4
LFLAGS = -L/root/opencv-4.5.1/lib
LIBS = -lopencv_core -lopencv_imgcodecs
TARGET = decompressphoto
$(TARGET): $(TARGET).cpp
$(CC) $(CFLAGS) $(INCLUDES) $(LFLAGS) -o $@ $< $(LIBS)
clean:
rm -f $(TARGET)
```
使用方法:
1. 将上述程序保存为decompressphoto.cpp,Makefile文件保存为Makefile。
2. 打开终端,进入程序所在目录。
3. 执行`make`命令生成可执行文件。
4. 执行`./decompressphoto compressed_path decompressed_path`命令,其中compressed_path为压缩后的.jpg图片路径,decompressed_path为解压后存储的路径。
参数设置:
无需设置参数,直接执行即可。
阅读全文