bmp灰度图片二值化 ,代码不知道出什么问题了 ,求高手解答
#include<stdio.h>#include"windows.h"unsignedchar*pBmpBuf;//读入图像数据的指针intbmpWidth;//图像的...
#include <stdio.h>
#include "windows.h"
unsigned char *pBmpBuf;//读入图像数据的指针
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD *pColorTable;//颜色表指针
int biBitCount;//图像类型,每像素位数
int data;
bool readBmp(char *bmpName){
FILE *fp=fopen(bmpName,"rb");
if(fp==0) return 0;
fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER bi;
fread(&bi,sizeof(BITMAPINFOHEADER),1,fp);
bmpWidth = bi.biWidth;
bmpHeight = bi.biHeight;
biBitCount = bi.biBitCount;
int lineByte=(bmpWidth* biBitCount/8+3)/4*4;
if(biBitCount==8){
pColorTable=new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
pBmpBuf=new unsigned char[lineByte * bmpHeight];
fread(pBmpBuf,1,lineByte * bmpHeight,fp);
for(int i=0;i<bmpWidth;i++)
for(int j=0;j<bmpHeight;j++){
if(pBmpBuf[i*lineByte+j]>128)
pBmpBuf[i*lineByte+j]=255;
else
pBmpBuf[i*lineByte+j]=0;
}
return 1;
}
bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable){
if(!imgBuf)return 0;
int colorTablesize=0;
if(biBitCount==8)colorTablesize=1024;
int lineByte=(width * biBitCount/8+3)/4*4;
FILE *fp=fopen(bmpName,"wb");
if(fp==0) return 0;
BITMAPFILEHEADER bf;
bf.bfType = 0x4D42;
bf.bfSize= sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER)+ colorTablesize + lineByte*height;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
bf.bfOffBits=54+colorTablesize;
fwrite(&bf, sizeof(BITMAPFILEHEADER),1, fp);
BITMAPINFOHEADER bi;
bi.biBitCount=biBitCount;
bi.biClrImportant=0;
bi.biClrUsed=0;
bi.biCompression=0;
bi.biHeight=height;
bi.biPlanes=1;
bi.biSize=40;
bi.biSizeImage=lineByte*height;
bi.biWidth=width;
bi.biXPelsPerMeter=0;
bi.biYPelsPerMeter=0;
fwrite(&bi, sizeof(BITMAPINFOHEADER),1, fp);
if(biBitCount==8)
fwrite(pColorTable, sizeof(RGBQUAD),256, fp);
fwrite(imgBuf, lineByte*height, 1, fp);
fclose(fp);
return 1;
}
int main(){
char readPath[]="test.BMP";
readBmp(readPath);
printf("width=%d,height=%d, biBitCount=%d\n",bmpWidth,bmpHeight, biBitCount);
char writePath[]="result.BMP";
saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable);
delete []pBmpBuf;
if(biBitCount==8)
delete []pColorTable;
getchar();
} 展开
#include "windows.h"
unsigned char *pBmpBuf;//读入图像数据的指针
int bmpWidth;//图像的宽
int bmpHeight;//图像的高
RGBQUAD *pColorTable;//颜色表指针
int biBitCount;//图像类型,每像素位数
int data;
bool readBmp(char *bmpName){
FILE *fp=fopen(bmpName,"rb");
if(fp==0) return 0;
fseek(fp,sizeof(BITMAPFILEHEADER),0);
BITMAPINFOHEADER bi;
fread(&bi,sizeof(BITMAPINFOHEADER),1,fp);
bmpWidth = bi.biWidth;
bmpHeight = bi.biHeight;
biBitCount = bi.biBitCount;
int lineByte=(bmpWidth* biBitCount/8+3)/4*4;
if(biBitCount==8){
pColorTable=new RGBQUAD[256];
fread(pColorTable,sizeof(RGBQUAD),256,fp);
}
pBmpBuf=new unsigned char[lineByte * bmpHeight];
fread(pBmpBuf,1,lineByte * bmpHeight,fp);
for(int i=0;i<bmpWidth;i++)
for(int j=0;j<bmpHeight;j++){
if(pBmpBuf[i*lineByte+j]>128)
pBmpBuf[i*lineByte+j]=255;
else
pBmpBuf[i*lineByte+j]=0;
}
return 1;
}
bool saveBmp(char *bmpName, unsigned char *imgBuf, int width, int height, int biBitCount, RGBQUAD *pColorTable){
if(!imgBuf)return 0;
int colorTablesize=0;
if(biBitCount==8)colorTablesize=1024;
int lineByte=(width * biBitCount/8+3)/4*4;
FILE *fp=fopen(bmpName,"wb");
if(fp==0) return 0;
BITMAPFILEHEADER bf;
bf.bfType = 0x4D42;
bf.bfSize= sizeof(BITMAPFILEHEADER)+ sizeof(BITMAPINFOHEADER)+ colorTablesize + lineByte*height;
bf.bfReserved1 = 0;
bf.bfReserved2 = 0;
bf.bfOffBits=54+colorTablesize;
fwrite(&bf, sizeof(BITMAPFILEHEADER),1, fp);
BITMAPINFOHEADER bi;
bi.biBitCount=biBitCount;
bi.biClrImportant=0;
bi.biClrUsed=0;
bi.biCompression=0;
bi.biHeight=height;
bi.biPlanes=1;
bi.biSize=40;
bi.biSizeImage=lineByte*height;
bi.biWidth=width;
bi.biXPelsPerMeter=0;
bi.biYPelsPerMeter=0;
fwrite(&bi, sizeof(BITMAPINFOHEADER),1, fp);
if(biBitCount==8)
fwrite(pColorTable, sizeof(RGBQUAD),256, fp);
fwrite(imgBuf, lineByte*height, 1, fp);
fclose(fp);
return 1;
}
int main(){
char readPath[]="test.BMP";
readBmp(readPath);
printf("width=%d,height=%d, biBitCount=%d\n",bmpWidth,bmpHeight, biBitCount);
char writePath[]="result.BMP";
saveBmp(writePath, pBmpBuf, bmpWidth, bmpHeight, biBitCount, pColorTable);
delete []pBmpBuf;
if(biBitCount==8)
delete []pColorTable;
getchar();
} 展开
1个回答
展开全部
Option Explicit
Dim R As Byte
Dim G As Byte
Dim B As Byte
Dim aaa As Long
Dim bbb As Byte
Dim i As Integer
Dim j As Integer
Private Sub Command1_Click()
For i = 1 To Picture1.ScaleHeight Step 15
For j = 1 To Picture1.ScaleWidth Step 15
aaa = Picture1.Point(j, i)
R = (aaa Mod 256) '取得红色值
G = (aaa Mod 65536) \ 256 '取得绿色值
B = (aaa Mod 16777216) \ 65536 '取得蓝色值
bbb = R / 3 + G / 3 + B / 3 '取得灰度值
If bbb >= 171 Then '这里bbb可作适当改变以获得最佳2值化效果
Picture2.PSet (j, i), RGB(255, 255, 255)
Else
Picture2.PSet (j, i), RGB(0, 0, 0)
End If
Next j
Next i
End Sub
希望对你有帮助
Dim R As Byte
Dim G As Byte
Dim B As Byte
Dim aaa As Long
Dim bbb As Byte
Dim i As Integer
Dim j As Integer
Private Sub Command1_Click()
For i = 1 To Picture1.ScaleHeight Step 15
For j = 1 To Picture1.ScaleWidth Step 15
aaa = Picture1.Point(j, i)
R = (aaa Mod 256) '取得红色值
G = (aaa Mod 65536) \ 256 '取得绿色值
B = (aaa Mod 16777216) \ 65536 '取得蓝色值
bbb = R / 3 + G / 3 + B / 3 '取得灰度值
If bbb >= 171 Then '这里bbb可作适当改变以获得最佳2值化效果
Picture2.PSet (j, i), RGB(255, 255, 255)
Else
Picture2.PSet (j, i), RGB(0, 0, 0)
End If
Next j
Next i
End Sub
希望对你有帮助
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询