c语言编写用opencv将路径/root/photo中的.jpg图片压缩到路径/root/compressphoto中,给出相应程序和对应的Makefile文件,注意opencv文件处于/root/opencv-4.5.1文件夹中。
时间: 2023-05-27 18:07:00 浏览: 64
程序代码:
```c
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv)
{
if (argc != 3)
{
printf("Usage: ./compressphoto <source_path> <destination_path>\n");
return -1;
}
Mat img = imread(argv[1], IMREAD_COLOR);
if (img.empty())
{
printf("Failed to read image: %s\n", argv[1]);
return -1;
}
std::vector<int> compression_params;
compression_params.push_back(IMWRITE_JPEG_QUALITY);
compression_params.push_back(50);
if (!imwrite(argv[2], img, compression_params))
{
printf("Failed to write compressed image: %s\n", argv[2]);
return -1;
}
printf("Image compressed successfully: %s -> %s\n", argv[1], argv[2]);
return 0;
}
```
Makefile文件:
```makefile
CC := gcc
CFLAGS := -Wall -Wextra -Werror
INCLUDE := -I/root/opencv-4.5.1/include
LIBS := -L/root/opencv-4.5.1/lib -lopencv_core -lopencv_imgcodecs
compressphoto: compressphoto.c
$(CC) $(CFLAGS) $(INCLUDE) $< -o $@ $(LIBS)
clean:
rm -f compressphoto
```
编译命令:
```
make
```
运行命令:
```
./compressphoto /root/photo/test.jpg /root/compressphoto/test_compressed.jpg
```
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)