用C语言写一个图像缩放的代码
时间: 2023-12-29 08:32:01 浏览: 188
当使用C语言进行图像处理时,可以使用图像处理库来实现图像缩放。下面是一个使用OpenCV库进行图像缩放的示例代码:
```c
#include <opencv2/opencv.hpp>
int main() {
// 读取原始图像
cv::Mat image = cv::imread("input.jpg");
// 指定目标尺寸
cv::Size targetSize(800, 600);
// 执行图像缩放
cv::Mat resizedImage;
cv::resize(image, resizedImage, targetSize);
// 保存缩放后的图像
cv::imwrite("output.jpg", resizedImage);
return 0;
}
```
在这个示例中,我们首先使用`imread`函数读取原始图像。然后,我们指定目标尺寸为800x600,可以根据实际需求进行修改。接下来,我们使用`resize`函数对原始图像进行缩放操作,并将结果存储在`resizedImage`中。最后,我们使用`imwrite`函数将缩放后的图像保存到磁盘上。
请确保在编译和运行代码之前,已经正确安装了OpenCV库,并将输入
相关问题
使用c语言写一个图片缩放算法
下面是一个简单的图片缩放算法的 C 语言实现,使用了双线性插值法来进行图像的缩放:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef unsigned char BYTE;
// 读取 BMP 图像文件
BYTE* ReadBMP(char* bmpFileHeader, char* bmpInfoHeader, char* bmpPixelData) {
FILE* fp = fopen("image.bmp", "rb");
if (fp == NULL) {
printf("Failed to open the file!\n");
return NULL;
}
BYTE* bmpData = NULL;
BYTE* bmpPixel = NULL;
int width = *(int*)(bmpInfoHeader + 4);
int height = *(int*)(bmpInfoHeader + 8);
int bitCount = *(int*)(bmpInfoHeader + 14);
int lineByte = (width * bitCount / 8 + 3) / 4 * 4;
bmpData = (BYTE*)malloc(lineByte * height);
memset(bmpData, 0, lineByte * height);
bmpPixel = (BYTE*)malloc(bitCount / 8);
memset(bmpPixel, 0, bitCount / 8);
for (int i = height - 1; i >= 0; i--) {
for (int j = 0; j < width; j++) {
fread(bmpPixel, bitCount / 8, 1, fp);
bmpData[i * lineByte + j * bitCount / 8 + 0] = bmpPixel[0];
bmpData[i * lineByte + j * bitCount / 8 + 1] = bmpPixel[1];
bmpData[i * lineByte + j * bitCount / 8 + 2] = bmpPixel[2];
}
fseek(fp, lineByte - width * bitCount / 8, SEEK_CUR);
}
fclose(fp);
free(bmpPixel);
return bmpData;
}
// 写入 BMP 图像文件
void WriteBMP(char* bmpFileHeader, char* bmpInfoHeader, BYTE* bmpData) {
FILE* fp = fopen("output.bmp", "wb");
if (fp == NULL) {
printf("Failed to open the file!\n");
return;
}
int width = *(int*)(bmpInfoHeader + 4);
int height = *(int*)(bmpInfoHeader + 8);
int bitCount = *(int*)(bmpInfoHeader + 14);
int lineByte = (width * bitCount / 8 + 3) / 4 * 4;
fwrite(bmpFileHeader, 1, 14, fp);
fwrite(bmpInfoHeader, 1, 40, fp);
for (int i = height - 1; i >= 0; i--) {
fwrite(bmpData + i * lineByte, lineByte, 1, fp);
}
fclose(fp);
}
// 图像缩放
BYTE* Resize(BYTE* bmpData, int width, int height, float ratio) {
int newWidth = (int)(width * ratio);
int newHeight = (int)(height * ratio);
int bitCount = 24;
int lineByte = (newWidth * bitCount / 8 + 3) / 4 * 4;
BYTE* newBmpData = (BYTE*)malloc(lineByte * newHeight);
memset(newBmpData, 0, lineByte * newHeight);
for (int i = 0; i < newHeight; i++) {
for (int j = 0; j < newWidth; j++) {
int x = (int)(j / ratio);
int y = (int)(i / ratio);
float u = j / ratio - x;
float v = i / ratio - y;
int pos1 = y * width + x;
int pos2 = pos1 + 1;
int pos3 = (y + 1) * width + x;
int pos4 = pos3 + 1;
BYTE b1 = bmpData[pos1 * 3 + 0];
BYTE g1 = bmpData[pos1 * 3 + 1];
BYTE r1 = bmpData[pos1 * 3 + 2];
BYTE b2 = bmpData[pos2 * 3 + 0];
BYTE g2 = bmpData[pos2 * 3 + 1];
BYTE r2 = bmpData[pos2 * 3 + 2];
BYTE b3 = bmpData[pos3 * 3 + 0];
BYTE g3 = bmpData[pos3 * 3 + 1];
BYTE r3 = bmpData[pos3 * 3 + 2];
BYTE b4 = bmpData[pos4 * 3 + 0];
BYTE g4 = bmpData[pos4 * 3 + 1];
BYTE r4 = bmpData[pos4 * 3 + 2];
BYTE b = (BYTE)((1 - u) * (1 - v) * b1 + u * (1 - v) * b2 + (1 - u) * v * b3 + u * v * b4);
BYTE g = (BYTE)((1 - u) * (1 - v) * g1 + u * (1 - v) * g2 + (1 - u) * v * g3 + u * v * g4);
BYTE r = (BYTE)((1 - u) * (1 - v) * r1 + u * (1 - v) * r2 + (1 - u) * v * r3 + u * v * r4);
newBmpData[i * lineByte + j * bitCount / 8 + 0] = b;
newBmpData[i * lineByte + j * bitCount / 8 + 1] = g;
newBmpData[i * lineByte + j * bitCount / 8 + 2] = r;
}
}
return newBmpData;
}
int main() {
char bmpFileHeader[14];
char bmpInfoHeader[40];
char bmpPixelData[100000];
BYTE* bmpData = ReadBMP(bmpFileHeader, bmpInfoHeader, bmpPixelData);
int width = *(int*)(bmpInfoHeader + 4);
int height = *(int*)(bmpInfoHeader + 8);
float ratio = 0.5;
BYTE* newBmpData = Resize(bmpData, width, height, ratio);
WriteBMP(bmpFileHeader, bmpInfoHeader, newBmpData);
free(bmpData);
free(newBmpData);
return 0;
}
```
这个示例代码使用了双线性插值算法,可以将原始图像缩小到原来的一半。你可以根据实际需求来修改代码,以适应更多的图像缩放需求。
在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。
阅读全文