c语言编写用opencv将路径/root/photo中的.jpg图片压缩到路径/root/compressphoto中,给出相应程序和对应的Makefile文件,注意opencv文件处于/root/opencv-4.5.1文件夹中。用最简洁的代码表示
时间: 2023-09-08 13:05:00 浏览: 112
### 回答1:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
int main()
{
string path = "/root/photo/";
string compress_path = "/root/compressphoto/";
vector<string> files;
glob(path, files);
for (auto f : files)
{
Mat img = imread(f);
string compress_file = compress_path + f.substr(f.find_last_of("/") + 1);
imwrite(compress_file, img, { IMWRITE_JPEG_QUALITY, 50 });
}
return 0;
}
Makefile文件:
CC=g++
CFLAGS=-I/root/opencv-4.5.1/include/
LDFLAGS=-L/root/opencv-4.5.1/lib/ -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_imgcodecs
all:
$(CC) -o compress compress.cpp $(CFLAGS) $(LDFLAGS)
clean:
rm -f compress
### 回答2:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
int main() {
string imagePath = "/root/photo/test.jpg"; // 原始图片路径
string savePath = "/root/compressphoto/compressed.jpg"; // 压缩后图片保存路径
Mat image = imread(imagePath);
if (image.empty()) {
cout << "无法读取图片" << endl;
return -1;
}
vector<int> params;
params.push_back(CV_IMWRITE_JPEG_QUALITY);
params.push_back(50); // 压缩质量设置为50,范围0-100
bool success = imwrite(savePath, image, params);
if (!success) {
cout << "图片压缩失败" << endl;
return -1;
}
cout << "图片压缩成功" << endl;
return 0;
}
Makefile文件内容:
CC=g++
CFLAGS=-Wall
LIBS=-lopencv_core -lopencv_highgui -lopencv_imgcodecs
all: compressphoto
compressphoto: compressphoto.cpp
$(CC) $(CFLAGS) compressphoto.cpp -o compressphoto $(LIBS)
程序文件保存为compressphoto.cpp,然后使用编译器进行编译和运行即可得到压缩后的图片。
### 回答3:
下面是一个用C语言编写的程序和对应的Makefile文件,用于使用OpenCV将路径`/root/photo`中的.jpg图片压缩到路径`/root/compressphoto`中。
程序代码如下(main.c):
```c
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
const char* imagePath = "/root/photo/image.jpg";
const char* compressedPath = "/root/compressphoto/compressed_image.jpg";
// 读取原始图像
Mat image = imread(imagePath, IMREAD_COLOR);
if (image.empty()) {
printf("无法读取图像文件:%s\n", imagePath);
return -1;
}
// 创建参数对象并设置压缩质量
std::vector<int> parameters;
parameters.push_back(IMWRITE_JPEG_QUALITY);
parameters.push_back(50); // 设置压缩质量为50(0-100)
// 压缩图像并保存到目标路径
bool isSuccess = imwrite(compressedPath, image, parameters);
if (isSuccess) {
printf("图像已成功压缩并保存到:%s\n", compressedPath);
}
else {
printf("图像压缩失败!\n");
}
return 0;
}
```
Makefile文件如下(Makefile):
```makefile
CC = gcc
CFLAGS = -Wall -I/root/opencv-4.5.1/include
LIBS = -L/root/opencv-4.5.1/lib -lopencv_core -lopencv_imgcodecs
TARGET = compress_image
all: $(TARGET)
$(TARGET): main.c
$(CC) $(CFLAGS) -o $(TARGET) main.c $(LIBS)
clean:
rm -f $(TARGET)
```
使用以上代码,只需将`imagePath`和`compressedPath`变量的值替换为实际的路径即可。将main.c和Makefile文件保存在同一个目录下,并确保OpenCV库文件和头文件位于`/root/opencv-4.5.1`文件夹中。
在终端中执行以下命令编译和运行程序:
```shell
make
./compress_image
```
程序将读取`/root/photo/image.jpg`文件,并将其压缩后保存为`/root/compressphoto/compressed_image.jpg`文件。压缩质量设为50,可以根据需要进行修改。若成功压缩并保存图像,程序将打印成功消息,否则会打印失败消息。
阅读全文