如何使用C++编写提取图像中特定像素点(给定x,y坐标)的RGB信息
2个回答
展开全部
win api 函数 :getwindowpixel()别忘加分呢。
#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("不是真彩位图!");
}
}
#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("不是真彩位图!");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给你找了份示例代码
#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
*/
#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
*/
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询