基于c++实现可见光与红外图像的融合的代码
时间: 2023-03-02 13:02:47 浏览: 157
以下是使用C语言实现可见光与红外图像融合的代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIDTH 640 //图像宽度
#define HEIGHT 480 //图像高度
//加载可见光图像
void loadRGBImage(const char* filename, unsigned char* R, unsigned char* G, unsigned char* B)
{
FILE* fp = fopen(filename, "rb");
if (!fp)
{
printf("Failed to open file %s\n", filename);
exit(1);
}
fseek(fp, 54, SEEK_SET); //跳过文件头
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
B[(HEIGHT - i - 1) * WIDTH + j] = fgetc(fp);
G[(HEIGHT - i - 1) * WIDTH + j] = fgetc(fp);
R[(HEIGHT - i - 1) * WIDTH + j] = fgetc(fp);
}
}
fclose(fp);
}
//加载红外图像
void loadIRImage(const char* filename, unsigned char* IR)
{
FILE* fp = fopen(filename, "rb");
if (!fp)
{
printf("Failed to open file %s\n", filename);
exit(1);
}
fseek(fp, 512, SEEK_SET); //跳过文件头
for (int i = 0; i < HEIGHT; i++)
{
for (int j = 0; j < WIDTH; j++)
{
IR[(HEIGHT - i - 1) * WIDTH + j] = fgetc(fp);
}
}
fclose(fp);
}
//可见光与红外图像融合
void fuseImages(unsigned char* R, unsigned char* G, unsigned char* B, unsigned char* IR, unsigned char* output)
{
for (int i = 0; i < WIDTH * HEIGHT; i++)
{
output[i] = 0.3 * R[i] + 0.59 * G[i] + 0.11 * B[i] + 0.4 * IR[i];
}
}
int main(int argc, char** argv)
{
if (argc < 4)
{
printf("Usage: %s RGB_image IR_image output_image\n", argv[0]);
exit(1);
}
const char* rgbFilename = argv[1];
const char* irFilename = argv[2];
const char* outputFilename = argv[3];
unsigned char R[WIDTH * HEIGHT];
unsigned char G[WIDTH * HEIGHT];
unsigned char B[WIDTH * HEIGHT];
unsigned char IR[WIDTH * HEIGHT];
unsigned char output[WIDTH * HEIGHT];
loadRGBImage(rgbFilename, R, G, B);
loadIRImage(irFilename, IR);
fuseImages(R, G, B, IR, output);
FILE* fp = fopen(outputFilename, "wb");
if (!fp)
{
printf("Failed to open file %s\n", outputFilename);
exit(1);
}
//写文件头
unsigned char header[54] = { 0x42, 0x4d, 0x36, 0x00, 0x0c, 0x01, 0x00, 0x00, 0x
阅读全文