PDA

View Full Version : Error using qhash and qcolor processing an image



fearu
13th December 2010, 03:37
Hello,

I'm doing a small image processing application for classes and I got an error trying to process the a mode (https://secure.wikimedia.org/wikipedia/en/wiki/Mode_%28statistics%29) filter to an image.

Here is the code of this particular filter. This method calculates the new value for a given pixel.

-limit has the radius of the kernel.
-I save in the hash any color that appears inside the kernel as key, and the number of times it has appeared as value.
-Then I simply return the color that has appeared more times inside the kernel.



QColor modeFilter::process(int i, int j, const QImage &imagen) {
QPoint limit = m_kernel->getLimit();
QHash<QRgb ,int> myHash;
for (int it = -limit.x(); it <= limit.x(); it++) {
for (int jt = -limit.y(); jt <= limit.y(); jt++) {
if (imagen.valid(it+i, jt+j)) {
QColor color = imagen.pixel(it+i, jt+j);
unsigned int colorkey = color.rgb();
if (myHash.contains(colorkey))
myHash[colorkey] = myHash[colorkey] + 1;
else
myHash[colorkey] = 0;
}
}
}
unsigned int modakey = 0;
int modavalue = 0;
QHash<QRgb, int>::const_iterator ite;
for ( ite=myHash.constBegin() ; ite != myHash.constEnd(); ++ite )
{
if (ite.value() > modavalue) {
modavalue = ite.value();
modakey = ite.key();
}
}
return QColor(QRgb(modakey));
}


The problem is that the resultant image is not similar to what should be the mode filter. The class modeFilter inherits from Filter, but I have also averageFilter and it works well, and I also use Kernel with custom Filters, so the mistake shouldn't be outside this method. If I use QMap or std::Map, the results are different -but equal between them-. It makes me think this problem has to do with my little knowlege of how I should use QMap/QHash.

Or maybe the mistake could be related with the conversion from QColor to QRgb?

By the way, here I'm only using the kernel to receive the limits of it. limit should have something like (1,1) (3,3), etc. -always the same value for the whole image-.

fearu
13th December 2010, 13:19
I think I got the error -I haven't modified the code, but I think it is pretty obvious-. It is not in this function, I was applying the result in the same image, not in a new image. In the average filter the mistake was not noticiable. By the way, the difference in the results between QHash and QMap/std::Map are due to the fact that there could be more than one color with the same number of coincidences, and QHash::iterator doesn't iterate through the keys in order, as QMap does.

By the way, myHash[colorkey] = 0; should be myHash[colorkey] = 1;