pgm 图片灰度转换问题

xiaoluc 2006-10-26 10:04:33
问题:
pgm文件就是可输出灰度值的图,
打开后是数字显示。比如一个8位的图,就是有256(2的8次方)个灰度,用以显示深
浅。我们老师要我们写一个程序可以改变图象的灰度值,比如把8位的改成7位的或者6
位的。
比如,一个pgm图是B=4位的,那么它的灰度值就是0 1 2 3 4 。。。。。。15
我们要把它变成A=2位的图,那么它的灰度值就是0 1 2 3
但是这样和原图有了太大的对比,那么我们就把输出图的灰度值乘以 2的(B-A)次方
,所以得到输出图的灰度为0 4 8 12
也就是说,原先图的0 1 2 3 变成0
原先图的4 5 6 7 变成4
原先图的8 9 10 11 变成8
原先图的12 13 14 15 变成12 lena.pgm是256个灰度的,变成128和64灰度的?
要用c++写的。
转换算法很简单:if(A>B) Out=In;
else
for all pixels m,n
Out[m,n]=2*(B-A)次方 *(In[m,n]/2*(B-A)次方)


现在有一个类
class image{
private:
char *filetype;
char *comment;
int column;
int row;
int greylevel;//max greylevel
float *matrix;//pixel data

Public:
image();//default constructor
image(int,int);//conostrucot
~image();//destructor
int read();
int output();
int getwidth();
int getheight();
float getvalue(int);
int getmaxgrey();
void setvalue(float,int);
void binarize();

};
好久没写c++,很多不熟悉了,大家一起讨论讨论阿,把这个类写完整
我写了这点
#include <stdio.h>
class image{
private:
char *filetype;
char *comment;
int column;
int row;
int greylevel;//max greylevel
float *matrix;//pixel data

Public:
image();//default constructor
image(int,int);//conostrucot
~image();//destructor
int read();
int output();
int getwidth();
int getheight();
float getvalue(int);
int getmaxgrey();
void setvalue(float,int);
void binarize();

};

image Quantize(image In,image Out,int B, int A){

if(A>B)
Out=In;
else
{
int times=1;
int distance=B-A;
for(int k=0;k<distance;k++)
times*=2;

int width=In.getwidth();
int height=In.getheight();
for(k=0;k<width;k++)
{
for(distance=0;distance<height;distance++)
{

getalue: get a byte from the In image
use the arithmetic transporting the current data to the result data
setvalue: the result data to the Out image

}

}

}


return Out;
}
void main(int argc, char *argv[])
{
image In,Out;
In.read();
Out=Quantize(In,Out,In.getmaxgrey(),128);
//Out=Quantize(In,Out,In.getmaxgrey(),64);
}
写不下去了,兄弟姐妹们,帮我杀了这个敌人。
...全文
504 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
睡在床板下_ 2006-10-27
  • 打赏
  • 举报
回复
灰度压缩 问题 ...
可以用线性 和非线性的 方式...
xiaoluc 2006-10-26
  • 打赏
  • 举报
回复
研究一下拉,读图片,写图片,统计宽度,高度......
csShooter 2006-10-26
  • 打赏
  • 举报
回复
学习
OOPhaisky 2006-10-26
  • 打赏
  • 举报
回复
我进来了,但是遗憾地告诉你:这个问题我不会阿:(
xiaoluc 2006-10-26
  • 打赏
  • 举报
回复
咋没人进来呢1

64,322

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧