想学习图像处理
本来一心准备学opencv的。但无奈,用mingw编译了多次opencv2.1。均出现各种错误
于是换QT来试一试
QT的图像类有QImage,QPixmap,QBitmap,QPicture。
不过QPixmap和QBitmap是用来优化图像显示的
QPicture貌似可以记录QPixmap的操作记录
这个还没仔细看
QImage可以处理每个像素,这正是我所需要的
查看帮助文档,发现QImage貌似没有直接读取为灰度图的(难道是我没发现?)
于是采用最傻的方法:把RGB值设为相同的值,貌似很浪费空间的样子,呵呵
思路就是读取每个像素的RGB值,再由此算出灰度值
最大众化的计算公式是Gray = 0.3*R + 0.59*G + 0.11*B
QT中提供的方法有 int qGray ( QRgb rgb )
其计算公式是 Gray = (R * 11 + G * 16 + B * 5)/32
1
void
Picture::paintEvent(QPaintEvent
*
){
2 QImage image;
3 image = QImage( " airplane.jpg " ).scaled( 200 , 200 );
4 int w,h;
5 w = image.width();
6 h = image.height();
7 QImage iGray(w,h,QImage::Format_RGB32);
8
9 for ( int i = 0 ; i < 200 ; i ++ ){
10 for ( int j = 0 ;j < 200 ; j ++ ){
11 QRgb pixel = image.pixel(i,j);
12 int gray = qGray(pixel);
13 QRgb grayPixel = qRgb(gray,gray,gray);
14 iGray.setPixel(i,j,grayPixel);
15 }
16 }
17
18 QPainter painter( this );
19 painter.drawImage( 0 , 0 ,image);
20 painter.drawImage( 200 , 0 ,iGray);
21 }
2 QImage image;
3 image = QImage( " airplane.jpg " ).scaled( 200 , 200 );
4 int w,h;
5 w = image.width();
6 h = image.height();
7 QImage iGray(w,h,QImage::Format_RGB32);
8
9 for ( int i = 0 ; i < 200 ; i ++ ){
10 for ( int j = 0 ;j < 200 ; j ++ ){
11 QRgb pixel = image.pixel(i,j);
12 int gray = qGray(pixel);
13 QRgb grayPixel = qRgb(gray,gray,gray);
14 iGray.setPixel(i,j,grayPixel);
15 }
16 }
17
18 QPainter painter( this );
19 painter.drawImage( 0 , 0 ,image);
20 painter.drawImage( 200 , 0 ,iGray);
21 }
网上找到一种设置八位颜色索引表的方法
貌似能省很多空间
但我用此方法时,drawImage 得到的是空白
可能我忽略了一些东西
闲话少说
看运行结果