Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the binarization of 24-bit color image in C language

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to realize the binarization of 24-bit color images in C language. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The details are as follows

/ / huiduhua.cpp: defines the entry point for the console application. / / # include "stdafx.h" # include#include int _ tmain (int argc, _ TCHAR* argv []) {BITMAPFILEHEADER bfhead; BITMAPINFOHEADER bihead; RGBQUAD * pColorTable; unsigned char * pBmpBuf; FILE * fp1=fopen ("mouse", "rb"); if (fp1==0) return 0; fread (& bfhead,14,1,fp1); / / read the file header into memory fread (& bihead,40,1,fp1); / / read the information header into memory int LineByte= (bihead.biWidth*24/8+3) / 4x4 / / ensure that the number of bytes per row is an integral multiple of 4 pBmpBuf=new unsigned char [LineByte*bihead.biHeight]; / / allocate memory space fread (pBmpBuf,LineByte*bihead.biHeight,1,fp1) for the data area; / / read the bmp data area into memory fclose (fp1); printf ("Width:%d, Height:% d\ n", bihead.biWidth,bihead.biHeight,bihead.biBitCount) / / now the true color image will be grayed out int LineByte1= (bihead.biWidth*8/8+3) / 4 colors 4; / / since the number of bits per pixel is changed to 8 after graying, the number of bytes per line will be changed, but it is still required to be an integral multiple of 4 FILE * fp2=fopen ("mouse 2.bmp", "wb"); if (fp2==0) return 0; / / change the file header and save it bfhead.bfSize=14+40+sizeof (RGBQUAD) * 256+LineByte1*bihead.biHeight / / change the file size bfhead.bfOffBits=14+40+sizeof (RGBQUAD) * 256; / / change the offset value fwrite (& bfhead,14,1,fp2); / / change the header and save it bihead.biBitCount=8; / / change the number of bits per pixel bihead.biSizeImage=LineByte1*bihead.biHeight; / / change the data area size fwrite (& bihead,40,1,fp2); / / because the grayed image has a color table, create a color table and save pColorTable=new RGBQUAD [256]; for

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report