Results 1 to 2 of 2

Thread: Error using qhash and qcolor processing an image

  1. #1
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Error using qhash and qcolor processing an image

    Hello,

    I'm doing a small image processing application for classes and I got an error trying to process the a mode 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.

    Qt Code:
    1. QColor modeFilter::process(int i, int j, const QImage &imagen) {
    2. QPoint limit = m_kernel->getLimit();
    3. QHash<QRgb ,int> myHash;
    4. for (int it = -limit.x(); it <= limit.x(); it++) {
    5. for (int jt = -limit.y(); jt <= limit.y(); jt++) {
    6. if (imagen.valid(it+i, jt+j)) {
    7. QColor color = imagen.pixel(it+i, jt+j);
    8. unsigned int colorkey = color.rgb();
    9. if (myHash.contains(colorkey))
    10. myHash[colorkey] = myHash[colorkey] + 1;
    11. else
    12. myHash[colorkey] = 0;
    13. }
    14. }
    15. }
    16. unsigned int modakey = 0;
    17. int modavalue = 0;
    18. QHash<QRgb, int>::const_iterator ite;
    19. for ( ite=myHash.constBegin() ; ite != myHash.constEnd(); ++ite )
    20. {
    21. if (ite.value() > modavalue) {
    22. modavalue = ite.value();
    23. modakey = ite.key();
    24. }
    25. }
    26. return QColor(QRgb(modakey));
    27. }
    To copy to clipboard, switch view to plain text mode 

    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-.
    Last edited by fearu; 13th December 2010 at 03:42.

  2. #2
    Join Date
    Aug 2010
    Posts
    15
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 2 Times in 1 Post

    Default Re: Error using qhash and qcolor processing an image

    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;

Similar Threads

  1. image processing
    By IRON_MAN in forum Qt Programming
    Replies: 4
    Last Post: 18th November 2009, 14:37
  2. Replies: 1
    Last Post: 10th February 2009, 10:42
  3. Image processing
    By NicNac in forum Newbie
    Replies: 25
    Last Post: 2nd November 2008, 11:05
  4. Image processing via matrix
    By jones.79 in forum Qt Programming
    Replies: 10
    Last Post: 22nd September 2008, 01:42
  5. Image Processing using Qt
    By danielperaza in forum Qt Programming
    Replies: 2
    Last Post: 9th March 2008, 19:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.