C++:将六个tif分色片写成bmp格式图片(8位深)

#include <cv.h>
#include <highgui.h>
#include <iostream>
//#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream>
#include<opencv2/opencv.hpp>


//必须得加上,才可以使用imread、MAT等opencv功能
using namespace cv;
using namespace std;


#pragma pack(2)//必须得写,否则sizeof得不到正确的结果
typedef unsigned char  BYTE;
typedef unsigned short WORD;
typedef unsigned long  DWORD;
typedef long  LONG;


typedef struct {
WORD    bfType;
DWORD   bfSize;
WORD    bfReserved1;
WORD    bfReserved2;
DWORD   bfOffBits;
} BITMAPFILEHEADER;


typedef struct {
DWORD      biSize;
LONG       biWidth;
LONG       biHeight;
WORD       biPlanes;
WORD       biBitCount;
DWORD      biCompression;
DWORD      biSizeImage;
LONG       biXPelsPerMeter;
LONG       biYPelsPerMeter;
DWORD      biClrUsed;
DWORD      biClrImportant;
} BITMAPINFOHEADER;


typedef struct tagPALETTEENTRY { // pe 

// bmp中RGB的存放顺序是BGR,如果此处不写对的话,颜色会错

BYTE peBlue;
BYTE peRed;
BYTE peGreen;

BYTE peFlags;
} PALETTEENTRY;


//void saveBitmap()
int main()
{
//FILE *fptest = fopen("F:/Desktop/写bmp/test.bmp", "wb+");
//if (fptest == NULL)
//{
// printf("file open failed.");
// exit(1);
//}
//BYTE *test = (BYTE *)malloc(8);
//test[0] = 255;
//test[1] = 128;
//test[2] = 0;
//test[3] = 255;
//test[4] = 255;
//test[5] = 255;
//test[6] = 255;
//test[7] = 255;


//fwrite(test, 8, 1, fptest);
//fclose(fptest);



//原来图片的大小
Mat KH = imread("F:/Desktop/写bmp/1KH.tif", 0);
Mat KL = imread("F:/Desktop/写bmp/1KL.tif", 0);
Mat CH = imre

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我是一个语言模型,无法提供编程功能。以下是一个C语言的示例程序,可以将BMP格式图片二值化: ```c #include <stdio.h> #include <stdlib.h> #pragma pack(push, 1) typedef struct BMPHeader { char id[2]; int size; int reserved; int offset; int header_size; int width; int height; short planes; short bit_count; int compression; int image_size; int x_pixels_per_meter; int y_pixels_per_meter; int colors_used; int colors_important; } BMPHeader; #pragma pack(pop) int main(int argc, char *argv[]) { if (argc != 3) { printf("Usage: %s input_file output_file\n", argv[0]); return 1; } FILE *input_file = fopen(argv[1], "rb"); if (!input_file) { printf("Error: failed to open input file %s\n", argv[1]); return 1; } BMPHeader header; fread(&header, sizeof(header), 1, input_file); if (header.bit_count != 24) { printf("Error: input file is not a 24-bit BMP image\n"); return 1; } FILE *output_file = fopen(argv[2], "wb"); if (!output_file) { printf("Error: failed to open output file %s\n", argv[2]); return 1; } fwrite(&header, sizeof(header), 1, output_file); int padding = (4 - (header.width * 3) % 4) % 4; for (int y = 0; y < header.height; y++) { for (int x = 0; x < header.width; x++) { unsigned char pixel[3]; fread(pixel, sizeof(pixel), 1, input_file); // convert to grayscale unsigned char gray = (pixel[0] + pixel[1] + pixel[2]) / 3; // thresholding unsigned char binary = gray > 128 ? 255 : 0; // write to output file fwrite(&binary, sizeof(binary), 1, output_file); fwrite(&binary, sizeof(binary), 1, output_file); fwrite(&binary, sizeof(binary), 1, output_file); } // skip padding fseek(input_file, padding, SEEK_CUR); // write padding to output file for (int i = 0; i < padding; i++) { fputc(0, output_file); } } fclose(input_file); fclose(output_file); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值