0

unsigned char として表される操作されたピクセル (グレー スケール) を表示しようとしましたが、失敗しました。

コードは次のとおりです。

#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();
}

理由はよくわかりませんが、表現されている画像は元の画像であり、グレースケール化する必要がある操作された画像ではありません。運がないのでネットで見つけようとしましたが、アイデアはありますか?事前にt​​hx。

4

1 に答える 1

3

コードのこの部分:

QColor clrCurrent(img.pixel(col, row));

clrCurrent.setBlue((int)(*p));
clrCurrent.setGreen((int)(*p));
clrCurrent.setRed((int)(*p));
p++;

imgは変わりません。一時的なオブジェクトにピクセルの色を取り、そのオブジェクトを変更すると、このオブジェクトは画像に影響を与えずに破棄されます。

scanLineQImageの機能を確認することをお勧めします。そして、その場所のforループでピクセルの色を変更します。これにより、はるかに高速に動作し、イメージが変わります。

于 2016-03-10T15:32:54.427 に答える