❶ 如何用c語言讀取點陣圖的像素點RGB信息
可以自己去查一下BMP文件的文件結構,這種文件的格式最簡單。當然,其他常用的圖片格式也可以去查一下。查到了之後,就能根據文件,用fopen打開圖片再fread讀取了
❷ c語言數字圖像處理怎麼顯示一張照片的像素值
#include <stdio.h>
#include <windows.h>
int main()
{
//變數
char title[255];//用於存放控制台窗口標題
HWND hwnd;//窗口的句柄,H:Handle
HDC hdc, hmemdc;//設備上下文句柄,DC:driver context
HBITMAP hbm;//圖片的句柄
BITMAP bm;//圖片結構體
RECT rect;//矩形 rectangle
//把控制台窗口調成100字元寬度,40行
system("mode con cols=100 lines=40");
//1.取得窗口的句柄
GetConsoleTitleA(title, 255);//獲取控制台窗口的標題
hwnd = FindWindowA(NULL, title);//通過窗口標題取得該窗口的句柄
//獲取窗口的高度和寬度
GetWindowRect(hwnd, &rect);
//2. 獲取畫筆
hdc = GetDC(hwnd);
/* if(!hdc)
printf("No val\n");
else
printf("have\n");
TextOutA(hdc, 300, 400, "Hello world", strlen("Hello world"));*/
hmemdc = CreateCompatibleDC(hdc);//創建一個兼容的DC
//3.載入圖片
hbm = (HBITMAP)LoadImageA(NULL, "123.bmp", IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE);
GetObject(hbm, sizeof(BITMAP), &bm);//得到圖片信息
//4.把圖片放入兼容DC中
SelectObject(hmemdc, hbm);
//5.在窗口上畫出圖片
BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, hmemdc, 0, 0, SRCCOPY);
DeleteObject(hbm);
DeleteObject(hmemdc);
ReleaseDC(hwnd, hdc);
return 0;
}
❸ 如何用c語言讀取圖片
#include
using namespace std;
#define Twoto1(i,j,w) i*w+j
void createimage(unsigned char *&img, int w, int h)
{img = new unsigned char[w*h];}
void delateimage(unsigned char*img)
{delete []img;}
void readimage(unsigned char*img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(&fp,fname, "rb");
if (fp == NULL){ cout << "error" << endl; return; }
size_t result;
result=fread(img , sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout << "Reading error" << endl;
return;
}
else
cout << "Reading Ok!" << endl;
fclose(fp);
}
void mobanjuanji(unsigned char image, unsigned char*image1, int w, int h, float moban[5][5])
{
for (int i = 0; i for (int j = 0; j if (iw - 3 || j>h - 3)
image1[Twoto1(i,j,w)] = 0;
else
{
float temp = 0;
for (int m = 0; m<5; m++)
for (int n = 0; n<5; n++)
{
temp += (image[Twoto1(i-2+m,j-2+n,w)] moban[m][n]);
}
if (temp>255) image1[Twoto1(i, j, w)] = 255;
else if (temp<0) image1[Twoto1(i, j, w)] = 0;
else image1[Twoto1(i, j, w)] = temp;
}
}
void saveimage(unsigned char *img, int w, int h, char *fname)
{
FILE *fp;
fopen_s(&fp, fname, "wb");
if (fp == NULL) { cout << "error" << endl; return; }
size_t result;
result = fwrite(img, sizeof(unsigned char), w*h, fp);
if (result != w*h)
{
cout << "Write error" << endl;
return;
}
else
cout << "Write Ok!" << endl;
fclose(fp);
}
void main()
{
unsigned char *img;
unsigned char *img1;
float moban[5][5] = { {0,0,0,0,0},{0, -1, 0, 1, 0 }, { 0, -2, 0, 2, 0 }, { 0, -1, 0, 1, 0 }, { 0,0,0,0,0 } };
//float moban[5][5] = { 0 };
int w = 512, h = 512;
createimage(img, w, h);
createimage(img1, w, h);
readimage(img, w, h, "E:ss.raw");
mobanjuanji(img, img1,w, h, moban);
saveimage(img, w, h, "E:ss_1.raw");
saveimage(img1, w, h, "E:ss_2.raw");
delateimage(img);
delateimage(img1);
}
(3)c語言讀取圖片像素點擴展閱讀
C語言實現一個圖片的讀出和寫入
#include <stdlib.h>
#include <windows.h>
int file_size(char* filename)//獲取文件名為filename的文件大小。
{
FILE *fp = fopen(filename, "rb");//打開文件。
int size;
if(fp == NULL) // 打開文件失敗
return -1;
fseek(fp, 0, SEEK_END);//定位文件指針到文件尾。
size=ftell(fp);//獲取文件指針偏移量,即文件大小。
fclose(fp);//關閉文件。
return size;
}
int main ()
{
int size=0;
size=file_size("qw");
printf("%d ",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d==
",pFile);
printf("%d ",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
❹ c語言,怎樣讀取一個BMP圖片
#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改變圖像亮度
typedef struct BMP
{
//14位元組
unsigned short bfType; //文件標識 2位元組 必須為BM
unsigned int bfSize; //文件大小 4位元組
unsigned short bfReserved1; //保留,每位元組以"00"填寫 2位元組
unsigned short bfReserved2; //同上 2位元組
unsigned int bfOffBits; //記錄圖像數據區的起始位置(圖象數據相對於文件頭位元組的偏移量)。 4位元組
//40位元組
unsigned int biSize; //表示本結構的大小 4位元組
int biWidth; //點陣圖的寬度 4位元組
int biHeight; //點陣圖的高度 4位元組
unsigned short biPlanes; //永遠為1 , 2位元組
unsigned short biBitCount; //點陣圖的位數 分為1 4 8 16 24 32 2位元組
unsigned int biCompression; //壓縮說明 4位元組
unsigned int biSizeImage; //表示點陣圖數據區域的大小以位元組為單位 4位元組
int biXPelsPerMeter; //用象素/米表示的水平解析度 4位元組
int biYPelsPerMeter; //用象素/米表示的垂直解析度 4位元組
unsigned int biClrUsed; //點陣圖使用的顏色索引數 4位元組
unsigned int biClrImportant; //對圖象顯示有重要影響的顏色索引的數目 4位元組
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif
//image_rw.c文件
#include<stdio.h>
#include<stdlib.h>
#include"image.h"
void image_info(FILE *file)
{
int times=3; //輸入文件名次數。
char bmp_name[10]; //文件名
printf("\nplease enter a file name for reading:");
do
{
if (times<3)
{
printf("\nplease enter a file name for reading again:");
}
fflush(stdin);
gets(bmp_name);
//printf("\n%s",bmp_name);
file=fopen(bmp_name,"rb+"); //打開一個文件進行讀寫操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for reading! ",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//讀取圖像信息
fseek(file,0L,0); //讀取圖像文件類型
fread(&bmp,sizeof(BMP),1,file);
printf("\n bmp tpye: %u",bmp.bfType);
printf("\n bmp size: %u",bmp.bfSize);
printf("\n bmp reserved1: %u",bmp.bfReserved1);
printf("\n bmp reserved2: %u",bmp.bfReserved2);
printf("\n bmp offBits: %u",bmp.bfOffBits);
printf("\n bmp bisize: %u",bmp.biSize);
printf("\n bmp biWidth: %d",bmp.biWidth);
printf("\n bmp biHeight: %d",bmp.biHeight);
printf("\n bmp biplans: %u",bmp.biPlanes);
printf("\n bmp biBitCount: %u",bmp.biBitCount);
printf("\n bmp biCompression: %u",bmp.biCompression);
printf("\n bmp biSizeImage: %u",bmp.biSizeImage);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
printf("\n bmp biClrUsed: %u",bmp.biClrUsed);
printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);
line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //獲得圖像數據每行的數據個數
//printf("dfsa%u",bmp.line_byte);
//bmp.imagedata=NULL;
imagedata=(unsigned char*)malloc(bmp.biSizeImage);
fseek(file,(long)bmp.bfOffBits,0);
fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//保存圖像
void image_save(FILE *file)
{
int times=3; //輸入文件名次數。
char bmp_name[10]; //文件名
//int i; //記錄數據區個數
printf("\nplease enter a file name for writeing:");
do
{
if (times<3)
{
printf("\nplease enter a file name for writeing again:");
}
fflush(stdin);
gets(bmp_name);
printf("\n%s",bmp_name);
file=fopen(bmp_name,"wb+"); //打開一個文件進行讀寫操作。
--times;
if (file==NULL)
{
printf("\nerror opening %s for writing",bmp_name);
}
else
{
break;
}
}
while(times!=0);
if (times==0)
{
printf("\nsorry, shutdown!");
exit(1);
}
//寫文件頭
printf("\n%s",bmp_name);
fseek(file,0L,0); //圖像文件類型
fwrite(&(bmp.bfType),sizeof(short),1,file);
printf("\n bmp tpye: %d",bmp.bfType);
fseek(file,2L,0); //圖像文件大小
fwrite(&(bmp.bfSize),sizeof(int),1,file);
printf("\n bmp size: %d",bmp.bfSize);
fseek(file,6L,0); //圖像文件保留字1
fwrite(&(bmp.bfReserved1),sizeof(short),1,file);
printf("\n bmp reserved1: %d",bmp.bfReserved1);
fseek(file,8L,0); //圖像文件保留字2
fwrite(&(bmp.bfReserved2),sizeof(short),1,file);
printf("\n bmp reserved2: %d",bmp.bfReserved2);
fseek(file,10L,0);//數據區的偏移量
fwrite(&(bmp.bfOffBits),sizeof(short),1,file);
printf("\n bmp offBits: %d",bmp.bfOffBits);
fseek(file,14L,0);//文件頭結構大小
fwrite(&(bmp.biSize),sizeof(int),1,file);
printf("\n bmp bisize: %d",bmp.biSize);
fseek(file,18L,0);//圖像的寬度
fwrite(&(bmp.biWidth),sizeof(int),1,file);
printf("\n bmp biWidth: %d",bmp.biWidth);
fseek(file,22L,0);//圖像的高度
fwrite(&(bmp.biHeight),sizeof(int),1,file);
printf("\n bmp biHeight: %d",bmp.biHeight);
fseek(file,24L,0);//圖像的面數
fwrite(&(bmp.biPlanes),sizeof(short),1,file);
printf("\n bmp biplans: %d",bmp.biPlanes);
fseek(file,28L,0);//圖像一個像素的位元組數
fwrite(&(bmp.biBitCount),sizeof(short),1,file);
printf("\n bmp biBitCount: %d",bmp.biBitCount);
fseek(file,30L,0);//圖像壓縮信息
fwrite(&(bmp.biCompression),sizeof(short),1,file);
printf("\n bmp biCompression: %d",bmp.biCompression);
fseek(file,34L,0);//圖像數據區的大小
fwrite(&(bmp.biSizeImage),sizeof(int),1,file);
printf("\n bmp biSizeImage: %d",bmp.biSizeImage);
fseek(file,38L,0);//水平解析度
fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);
fseek(file,42L,0);//垂直解析度
fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);
printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);
fseek(file,46L,0);//顏色索引數
fwrite(&(bmp.biClrUsed),sizeof(int),1,file);
printf("\n bmp biClrUsed: %d",bmp.biClrUsed);
fseek(file,50L,0);//重要顏色索引數
fwrite(&(bmp.biClrImportant),sizeof(int),1,file);
printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);
fseek(file,(long)(bmp.bfOffBits),0);
fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);
fclose(file);
}
//pixProcess.c文件
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"
//灰度化
void image_gray()
{
int i,j;
unsigned char tmp;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));
imagedata[i*line_byte+j*3+0]=tmp;
imagedata[i*line_byte+j*3+1]=tmp;
imagedata[i*line_byte+j*3+2]=tmp;
//printf("\nnidsfh%d %d",i,j);
}
}
}
//二值化
void image_binarization()
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if ((*(imagedata+i*line_byte+j))<128)
{
imagedata[i*line_byte+j]=0;
}
else
{
imagedata[i*line_byte+j]=255;
}
}
}
}
void image_opposite() //反相
{
int i,j;
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);
}
}
}
void image_channel() //抽取RGB通道
{
int i,j;
char rgb;
printf("\nplease enter a char(r/g/b): ");
fflush(stdin);
scanf("%c",&rgb);
if (rgb=='b')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j+1]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else if(rgb=='g')
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+2]=0;
}
}
}
else
{
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte/3;j++)
{
imagedata[i*line_byte+3*j]=0;
imagedata[i*line_byte+3*j+1]=0;
}
}
}
}
void image_bright()//改變圖像亮度
{
int level;
int i,j;
printf("\n please enter the level of brightness[-255 to 255] :");
fflush(stdin);
scanf("%d",&level);
for (i=0;i<bmp.biHeight;i++)
{
for (j=0;j<line_byte;j++)
{
if (level>=0)
{
if ((imagedata[i*line_byte+j]+level)>255)
imagedata[i*line_byte+j]=255;
else
imagedata[i*line_byte+j]+=level;
}
else
{
if ((imagedata[i*line_byte+j]-abs(level))<0)
imagedata[i*line_byte+j]=0;
else
imagedata[i*line_byte+j]+=level;
}
}
}
}
//void image_create() //創建一幅24位BMP圖像文件。
//{
//main.c文件
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include"image.h"
BMP bmp;
int main()
{
FILE *file=NULL;
int choose;
char gono;
do
{
image_info(file); //imagedata已經分配了動態內存,但是沒有釋放
printf("\n 1.image_opposite");
printf("\n 2.image_gray");
printf("\n 3.image_binarization");
printf("\n 4.image_channel");
printf("\n 5.image_brightness");
//printf("6.image_opposite");
//printf("7.image_opposite");
printf("\nchoose your options:");
fflush(stdin);
scanf("%d",&choose);
switch(choose)
{
case 1:
image_opposite();
image_save(file);
free(imagedata);
break;
case 2:
image_gray();
image_save(file);
free(imagedata);
break;
case 3:
image_binarization();
image_save(file);
free(imagedata);
break;
case 4:
image_channel();
image_save(file);
free(imagedata);
break;
case 5:
image_bright();
image_save(file);
free(imagedata);
break;
default:
printf("\n wrong choose!");
}
printf("\nlet's go on?(y/n):");
fflush(stdin);
scanf("%c",&gono);
if (gono=='n')
{
printf("\nbye bye!");
break;
}
}
while(1);
return 0;
}
❺ 用C語言讀取16位bmp圖片的每個像素的信息~
沒有什麼不同。
讀出 BITMAPINFO 結構:
typedef struct tagBITMAPINFO {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[1];
} BITMAPINFO;
以後,
BITMAPINFOHEADER 結構 里就有 biBitCount
biBitCount 等於 16 就是16位,等於24 就是24位,等於32 就是32位。
顏色在哪,要考慮 biCompression 壓縮方法,若等於 BI_RGB, bmiColors 就等於 NULL. 在 bitmap 數組里 每個WORD 就是 一個像素點. 5個 bits 蘭,5個 bits 綠,再5個 bits 紅,最高位不用。
其它方法自己看資料吧。
❻ 如何用C語言程序從bmp格式的圖片中讀取圖片的灰度值
1、首先要了解bmp點陣圖的格式,搜索些技術支持文檔,bmp點陣圖基本上是分4大部分,文件信息結果部分,文件頭信息結果部分,調色板結果部分,後面就是數據實體部分。及其每個部分對應有用的信息。比如長寬。當然長寬信息你自己可以從window系統下看得到。打開bmp文件,把前面三部分的位元組總數給固定下來,逐個字元讀取,然後讀取數據實體部分,輸出就可以了。
2、常式:
#include<stdio.h>
#include<stdlib.h>
#pragmapack(2)
/*定義WORD為兩個位元組的類型*/
typedefunsignedshortWORD;
/*定義DWORD為e四個位元組的類型*/
typedefunsignedlongDWORD;
/*點陣圖文件頭*/
typedefstructBMP_FILE_HEADER
{
WORDbType;/*文件標識符*/
DWORDbSize;/*文件的大小*/
WORDbReserved1;/*保留值,必須設置為0*/
WORDbReserved2;/*保留值,必須設置為0*/
DWORDbOffset;/*文件頭的最後到圖像數據位開始的偏移量*/
}BMPFILEHEADER;
/*點陣圖信息頭*/
typedefstructBMP_INFO
{
DWORDbInfoSize;/*信息頭的大小*/
DWORDbWidth;/*圖像的寬度*/
DWORDbHeight;/*圖像的高度*/
WORDbPlanes;/*圖像的位面數*/
WORDbBitCount;/*每個像素的位數*/
DWORDbCompression;/*壓縮類型*/
DWORDbmpImageSize;/*圖像的大小,以位元組為單位*/
DWORDbXPelsPerMeter;/*水平解析度*/
DWORDbYPelsPerMeter;/*垂直解析度*/
DWORDbClrUsed;/*使用的色彩數*/
DWORDbClrImportant;/*重要的顏色數*/
}BMPINF;
/*彩色表*/
typedefstructRGB_QUAD
{
WORDrgbBlue;/*藍色強度*/
WORDrgbGreen;/*綠色強度*/
WORDrgbRed;/*紅色強度*/
WORDrgbReversed;/*保留值*/
}RGBQUAD;
intmain()
{
FILE*fp;
BMPFILEHEADERfileHeader;
BMPINFinfoHeader;
longoffset,bmpImageSize,width,height,bytesPerPixel,size,bitCount;
//inti,j;
//unsignedchar**p;
WORDc;
if((fp=fopen("5.bmp","rb"))==NULL)
{
printf("Cann'topenthefile! ");
exit(0);
}
fseek(fp,0,0);
fread(&fileHeader,sizeof(fileHeader),1,fp);
fread(&infoHeader,sizeof(infoHeader),1,fp);
//計算並輸出點陣圖數據的偏移量,圖像的大小,寬度和高度,每個像素點所佔的位元組
size=fileHeader.bSize;
offset=fileHeader.bOffset;
bmpImageSize=infoHeader.bmpImageSize;
width=infoHeader.bWidth;
height=infoHeader.bHeight;
bitCount=infoHeader.bBitCount;
bytesPerPixel=infoHeader.bBitCount/8;
printf("%d%d%d%d%d%d ",size,offset,bmpImageSize,width,height,bitCount,bytesPerPixel);
//輸出每個像素點所佔位元組中的內容
c=fgetc(fp);
while(!feof(fp))
{
printf("%x",c);
c=fgetc(fp);
}
printf(" ");
fclose(fp);
return0;
}
❼ 已有一個bmp圖片用標准c獲取其任意點(給定坐標)像素信息
/*
你還是聽聽高先生的建議吧,
前面我給你的回答除了使用了c++的『
//』注釋以外,使用純C語言編寫的。
你如果僅僅是想完你說的目的,
雖然一小段代碼可以就完成但是卻不太容易讀懂 。
圖像右下角坐標為(0,0)
*/
#include<stdio.h>
int main()
{
int width,height,x,y;
unsigned short bitCount;
int offbits;
int bitPerLine;
unsigned char data;
FILE* bmpfp = fopen("E:\\風景\\風景1.bmp","rb");
fseek(bmpfp,18,SEEK_SET);
fread(&width,sizeof(int),1,bmpfp);
fread(&height,sizeof(int),1,bmpfp);
printf("width : %d , height : %d\n",width,height);
fseek(bmpfp,2,SEEK_CUR);
fread(&bitCount,sizeof(bitCount),1,bmpfp);
fseek(bmpfp,10,SEEK_SET);
fread(&offbits,sizeof(int),1,bmpfp);
if(bitCount==24){
bitPerLine = ( (width*3)%4==0 ) ? width*3 : ( (width*3)/4 )*4 + 4;
while(1){
printf("請輸出坐標:");
scanf("%d%d",&x,&y);
if(x>width||y>height) return 0;
fseek(bmpfp, 18 + offbits + bitPerLine * y + 3*x , SEEK_SET);
fread(&data,sizeof(data),1,bmpfp);
printf("該點藍色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("該點綠色分量:%d",data);
fread(&data,sizeof(data),1,bmpfp);
printf("該點紅色分量:%d\n",data);
}
}else{
printf("不是真彩點陣圖!");
}
}
/*
運行結果:
width : 700 , height : 382
請輸出坐標:0 0
該點藍色分量:68該點綠色分量:82該點紅色分量:80
*/
❽ 如何用C語言(C++)讀取點陣圖的像素點RGB信息
pData裡面保存的就是一個一個的COLORREF結構,你只需要通過BITMAPINFOHEADER中的寬高等信息,計算位移,就可以讀取某個點的RGB值了。
還有一個簡單的辦法,你之前已經有memBitmap這個CBitmap了,通過這個做更方便。通過SelectObject將memBitmap放到一個CDC中,直接使用函數GetPixel函數就可以獲取指定某個點的RGB值了,這個不需要計算和位移。