PDA

View Full Version : Basic image processing on a QImage



Danny
13th December 2009, 16:39
Hello forum people,

Does anyone know of an easy way to perform basic image processing algorithms on a QImage? I figured algorithms for modifying brightness and contrast where already implemented quite a few times in Qt, but I'm unable to find Qt functions or otherwise libraries that aren't a complete overkill for the task (e.g. using openCV and converting image formats back and forth).

So far, I've implemented the basic brightness / contrast / gamma filters myself, but moving on to implementing a simple blur (convolution with a Gaussian window) is too much...

Any suggestions?

Thanks!
Danny

JohannesMunk
13th December 2009, 16:48
Maybe you want to have a look at

http://doc.qt.nokia.com/4.6/qgraphicseffect.html

I didn't use it yet, though.

HIH

Johannes

Danny
13th December 2009, 18:07
Thanks for the quickly reply!

I'm kind of new in Qt, but according to my understanding this class doesn't exactly do what I want. I think it's used as a rendering effect on a QGraphicItem, when rendered on a QGraphicView. (Works on a painter, when there is a draw() event, etc.)

I really think I actually need something a lot more basic, just some trivial image processing code that works on the pixels of a QImage directly. Sounds like it must be implemented somewhere in Qt... no?

Thanks,
Danny

JohannesMunk
13th December 2009, 18:38
If you look into the implementation of QGraphicsEffect, you can see how it is implemented.

They use some QPixMapFilters, which sadly are declared private (qpixmapfilter_p.h).. Bu you can look into those files and see how it is done:

Blurring from qpixmapfilter.cpp:



static QImage blurred(const QImage& image, const QRect& rect, int radius, bool alphaOnly = false)
{
int tab[] = { 14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 };
int alpha = (radius < 1) ? 16 : (radius > 17) ? 1 : tab[radius-1];

QImage result = image.convertToFormat(QImage::Format_ARGB32_Premul tiplied);
int r1 = rect.top();
int r2 = rect.bottom();
int c1 = rect.left();
int c2 = rect.right();

int bpl = result.bytesPerLine();
int rgba[4];
unsigned char* p;

int i1 = 0;
int i2 = 3;

if (alphaOnly)
i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3);

for (int col = c1; col <= c2; col++) {
p = result.scanLine(r1) + col * 4;
for (int i = i1; i <= i2; i++)
rgba[i] = p[i] << 4;

p += bpl;
for (int j = r1; j < r2; j++, p += bpl)
for (int i = i1; i <= i2; i++)
p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
}

for (int row = r1; row <= r2; row++) {
p = result.scanLine(row) + c1 * 4;
for (int i = i1; i <= i2; i++)
rgba[i] = p[i] << 4;

p += 4;
for (int j = c1; j < c2; j++, p += 4)
for (int i = i1; i <= i2; i++)
p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
}

for (int col = c1; col <= c2; col++) {
p = result.scanLine(r2) + col * 4;
for (int i = i1; i <= i2; i++)
rgba[i] = p[i] << 4;

p -= bpl;
for (int j = r1; j < r2; j++, p -= bpl)
for (int i = i1; i <= i2; i++)
p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
}

for (int row = r1; row <= r2; row++) {
p = result.scanLine(row) + c2 * 4;
for (int i = i1; i <= i2; i++)
rgba[i] = p[i] << 4;

p -= 4;
for (int j = c1; j < c2; j++, p -= 4)
for (int i = i1; i <= i2; i++)
p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4;
}

return result;
}
Good luck :-> !

Johannes

Danny
14th December 2009, 10:58
While not exactly what I originally wanted, your snippet was extremely useful!

So it seems like a basic (Q)image processing Qt library doesn't exist, right? That's too bad...

Thanks!
Danny

JohannesMunk
18th January 2010, 12:33
Just found a QtSolution for this: http://qt.nokia.com/products/appdev/add-on-products/catalog/4/Utilities/qtimagefilters/

HIH

Johannes

dmginc
18th January 2010, 12:39
The OpenCV library may be what you're looking for ;)
I'm using it at the moment with Qt for a computer vision application :)

toutarrive
26th January 2010, 12:47
I've posted a contrast and a brightness filter on this forum .
you can check it out .

toutarrive
26th January 2010, 12:49
Can you share you contrast/brigthen filter ?
I would like to have look at it.

ecanela
27th January 2010, 02:20
you can use fmt_filters filters. this filter are written in c++ but is compatible whit QImage. only check the examples.

homepage http://ksquirrel.sourceforge.net/subprojects.php

ecanela
27th January 2010, 02:24
sorry by the double post..

please. someone of the staf f can delte this..