[英]Qt - displaying unsigned char after pixel manipulation
我正在尝试显示未成功表示为无符号字符的操纵像素(将它们灰度化)。
这是代码:
#include "mainwindow.h"
#include <QApplication>
#include "qimage.h"
#include <QImage>
#include <QLabel>
#include <QColor>
#include "qcolor.h"
#include <Qdebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
int height;
int width;
unsigned char *p, *p_begin;
QImage img("C:\\Users\\Owner\\Pictures\\2013-09-26\\IMG_0836.JPG");
height = img.height();
width = img.width();
p = (unsigned char *)malloc(height * width * sizeof(unsigned char));
p_begin = p;
for (int row = 0; row < height; ++row)
{
for (int col = 0; col < width; ++col)
{
QColor clrCurrent( img.pixel( col, row ));
*p = (unsigned char)((clrCurrent.green() * 0.587) + (clrCurrent.blue() * 0.114) + (clrCurrent.red() * 0.299));
p++;
}
}
p = p_begin;
for ( int row = 0; row < height; ++row )
{
for (int col = 0; col < width; ++col)
{
QColor clrCurrent(img.pixel(col, row));
clrCurrent.setBlue((int)(*p));
clrCurrent.setGreen((int)(*p));
clrCurrent.setRed((int)(*p));
p++;
}
}
QLabel myLabel;
myLabel.setPixmap(QPixmap::fromImage(img));
myLabel.show();
return a.exec();
}
我真的不知道为什么,但是代表的图片是原始图片,而不是经过操纵的图片,应该将其灰度化。 我试图在网上找不到运气,有什么想法吗? 提前谢谢。
这部分代码:
QColor clrCurrent(img.pixel(col, row));
clrCurrent.setBlue((int)(*p));
clrCurrent.setGreen((int)(*p));
clrCurrent.setRed((int)(*p));
p++;
不会改变img。 只需将像素颜色变为临时对象,然后更改该对象,然后就可以销毁该对象,而不会影响图像。
我建议您检查QImage的功能scanLine
。 然后将像素的颜色更改为一个在该位置上循环的像素。 这样可以更快地工作,并且可以更改图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.