#include <stdio.h> #include <stdlib.h> #include <math.h> #define STB_IMAGE_IMPLEMENTATION #include "/usr/include/stb/stb_image.h" #define STB_IMAGE_WRITE_IMPLEMENTATION #include "/usr/include/stb/stb_image_write.h" int main(int argc, char* argv[]) { if (argc < 4) { printf("Usage: %s input_file output_file scale\n", argv[0]); return 1; } char* input_file = argv[1]; char* output_file = argv[2]; float scale = atof(argv[3]); int width, height, channels; unsigned char* image_data = stbi_load(input_file, &width, &height, &channels, STBI_rgb_alpha); if (!image_data) { printf("Error: Failed to load image file %s\n", input_file); return 1; } int new_width = (int)round(scale * width); int new_height = (int)round(scale * height); unsigned char* new_image_data = (unsigned char*)malloc(new_width * new_height * 4); for (int y = 0; y < new_height; y++) { for (int x = 0; x < new_width; x++) { int src_x = (int)round(x / scale); int src_y = (int)round(y / scale); src_x = src_x >= width ? width - 1 : src_x; src_y = src_y >= height ? height - 1 : src_y; int src_index = (src_y * width + src_x) * 4; int dst_index = (y * new_width + x) * 4; new_image_data[dst_index] = image_data[src_index]; new_image_data[dst_index + 1] = image_data[src_index + 1]; new_image_data[dst_index + 2] = image_data[src_index + 2]; new_image_data[dst_index + 3] = image_data[src_index + 3]; } } stbi_write_jpeg(output_file, new_width, new_height, STBI_rgb_alpha, new_image_data, new_width * 4); stbi_image_free(image_data); free(new_image_data); return 0; }
时间: 2024-04-28 12:22:59 浏览: 19
这是一个用 C 语言编写的图像缩放程序,可以将输入的图片按照指定的比例缩小,并输出为新的图片。程序使用了 stb 库来加载和保存图片。具体的实现过程是先通过 stbi_load 函数加载输入图片,然后根据缩放比例计算出新图片的尺寸,创建一个新的图片数据数组 new_image_data,遍历新图片的每个像素,计算出对应的原图像素的位置,将其复制到新的数据数组中,最后通过 stbi_write_jpeg 函数将新的图片保存为 JPEG 格式。
相关问题
在ubuntu中用c语言写一个实现图片缩放的代码
好的,下面是一个简单的图片缩放的C语言代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
int main(int argc, char* argv[])
{
if (argc < 4) {
printf("Usage: %s input_file output_file scale\n", argv[0]);
return 1;
}
char* input_file = argv[1];
char* output_file = argv[2];
float scale = atof(argv[3]);
int width, height, channels;
unsigned char* image_data = stbi_load(input_file, &width, &height, &channels, STBI_rgb_alpha);
if (!image_data) {
printf("Error: Failed to load image file %s\n", input_file);
return 1;
}
int new_width = (int)round(scale * width);
int new_height = (int)round(scale * height);
unsigned char* new_image_data = (unsigned char*)malloc(new_width * new_height * 4);
for (int y = 0; y < new_height; y++) {
for (int x = 0; x < new_width; x++) {
int src_x = (int)round(x / scale);
int src_y = (int)round(y / scale);
src_x = src_x >= width ? width - 1 : src_x;
src_y = src_y >= height ? height - 1 : src_y;
int src_index = (src_y * width + src_x) * 4;
int dst_index = (y * new_width + x) * 4;
new_image_data[dst_index] = image_data[src_index];
new_image_data[dst_index + 1] = image_data[src_index + 1];
new_image_data[dst_index + 2] = image_data[src_index + 2];
new_image_data[dst_index + 3] = image_data[src_index + 3];
}
}
stbi_write_png(output_file, new_width, new_height, STBI_rgb_alpha, new_image_data, new_width * 4);
stbi_image_free(image_data);
free(new_image_data);
return 0;
}
```
这个代码依赖于STB的图像处理库,可以使用以下命令安装:
```
sudo apt-get install libstb-dev
```
然后使用以下命令编译:
```
gcc -o image_scale image_scale.c -lm -lstb_image -lstb_image_write
```
使用方式如下:
```
./image_scale input_file output_file scale
```
其中,input_file为原始图片路径,output_file为缩放后图片路径,scale为缩放比例。例如:
```
./image_scale input.png output.png 0.5
```
会将input.png缩放为原始图片的一半大小,并保存为output.png。
用C语言写高斯模糊代码
以下是使用C语言实现高斯模糊的代码示例:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define PI 3.14159265358979323846
//高斯分布的计算函数
float gaussian(float x, float sigma) {
return exp(-x*x / (2*sigma*sigma)) / (sqrt(2*PI)*sigma);
}
//高斯模糊函数
void gaussian_blur(unsigned char *image, int width, int height, int channels, float sigma) {
int radius = (int)(3*sigma); //高斯核半径
int size = 2*radius + 1; //高斯核大小
float *kernel = (float*)malloc(size * sizeof(float)); //高斯核数组
float sum = 0; //高斯核元素和
//计算高斯核数组
for (int i = 0; i < size; i++) {
kernel[i] = gaussian(i - radius, sigma);
sum += kernel[i];
}
//归一化高斯核数组
for (int i = 0; i < size; i++) {
kernel[i] /= sum;
}
//逐行逐列进行高斯模糊
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (int c = 0; c < channels; c++) {
float sum = 0;
//遍历高斯核
for (int i = -radius; i <= radius; i++) {
//边缘像素处理:超出图像范围的像素值为0
int px = x + i;
if (px < 0) px = 0;
else if (px >= width) px = width - 1;
//计算高斯核权重,并累加乘积
float weight = kernel[i+radius];
sum += weight * image[(y*width + px)*channels + c];
}
//将乘积结果写入当前像素值
image[(y*width + x)*channels + c] = (unsigned char)sum;
}
}
}
free(kernel);
}
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage: %s input_image [output_image] [sigma]\n", argv[0]);
return 0;
}
char *input_filename = argv[1];
char *output_filename = argc >= 3 ? argv[2] : "output.jpg";
float sigma = argc >= 4 ? atof(argv[3]) : 1.0f;
int width, height, channels;
unsigned char *image = stbi_load(input_filename, &width, &height, &channels, 0);
if (image == NULL) {
printf("Error loading image %s\n", input_filename);
return 1;
}
gaussian_blur(image, width, height, channels, sigma);
stbi_write_jpg(output_filename, width, height, channels, image, 100);
stbi_image_free(image);
return 0;
}
相关推荐
![image/gif](https://img-home.csdnimg.cn/images/20210720083352.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![text/x-c](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)