Results 1 to 14 of 14

Thread: grayscaled image from number matrix

  1. #1
    Join Date
    Sep 2012
    Location
    Ankara
    Posts
    15
    Qt products
    Platforms
    Unix/X11
    Thanks
    3

    Angry grayscaled image from number matrix

    Hi everyone.
    I have a problem with imaging.
    I have a huge(40MB) 8 bit(0-255) text file. There is 3712 rows and 3712 columns. (Actually there is two files. One of them is image and the other one contant two numbers known as zscale coefficient)
    This file is actually an image and I want to paint it.
    I tried with qGraphicsView. As you understood there is 3712x3712 pixels so my script crashed.
    How can I do that? Help me...
    You can reach to scale and image files here.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: grayscaled image from number matrix

    Quote Originally Posted by mshemuni View Post
    As you understood there is 3712x3712 pixels so my script crashed.
    It's not that much. If it crashes then it means your code is incorrect. Handling a 14Mpix image is not that big of a deal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: grayscaled image from number matrix

    Right, at a pixel depth of 8 bits (for greyscale), this works out to about 80 MB. Even at 32 bits, that's still only 320 MB which is well within even 32-bit OS limits. So, I also think there's a bug in the code.

    Creating a greyscale image from 8-bit data is easy. The QImage should be a member variable of your QWidget class so you don't have to recalculate it for every paintEvent.

    Qt Code:
    1. long index= 0;
    2. QImage image( 3172, 3172, QImage::Format_RGB32 ); // should be a member variable of your widget class
    3. for ( int i = 0; i < 3172; i++ )
    4. {
    5. for ( j = 0; j < 3172; j++ )
    6. {
    7. QRgb color = qRgb( data[ index ], data[ index ], data[ index ] );
    8. image.setPixel( i, j, color );
    9. index++;
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

    In the paintEvent():

    Qt Code:
    1. QPainter painter;
    2. painter.drawImage( QPoint( 0, 0 ), image );
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: grayscaled image from number matrix

    At 8 bits it works out to 14MB as there is one byte per pixel
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,348
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    Default Re: grayscaled image from number matrix

    Oh right, I was counting bits. Duh.

    I did not see a format for either QPixmap or QImage that has 8 bits per pixel, only 1, 16, or 32 bits, unless I'm reading the docs incorrectly. So he'd still need about 50 MB for a 32-bit pixmap, which is certainly do-able.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: grayscaled image from number matrix

    Quote Originally Posted by d_stranz View Post
    Oh right, I was counting bits. Duh.

    I did not see a format for either QPixmap or QImage that has 8 bits per pixel, only 1, 16, or 32 bits, unless I'm reading the docs incorrectly. So he'd still need about 50 MB for a 32-bit pixmap, which is certainly do-able.
    Qt Code:
    1. long index= 0;
    2. QImage image( 3172, 3172, QImage::Format_Indexed8 );
    3. QVector<QRgb> colorTable(256);
    4. for(int i=0;i<256;++i) { colorTable[i] = qRgb(i,i,i); }
    5. image.setColorTable(colorTable);
    6.  
    7. for ( int i = 0; i < 3172; i++ )
    8. {
    9. for ( j = 0; j < 3172; j++ )
    10. {
    11. image.setPixel( i, j, data[index] );
    12. index++;
    13. }
    14. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. The following user says thank you to wysota for this useful post:

    d_stranz (4th April 2013)

  8. #7
    Join Date
    Sep 2012
    Location
    Ankara
    Posts
    15
    Qt products
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: grayscaled image from number matrix

    Checked my code. That was absolutely wrong.
    Found the code blow here:


    Qt Code:
    1. #!/usr/bin/env python
    2.  
    3. from PyQt4 import QtCore
    4. from PyQt4 import QtGui
    5.  
    6. class Canvas(QtGui.QPixmap):
    7. """ Canvas for drawing"""
    8. def __init__(self, parent=None):
    9. QtGui.QPixmap.__init__(self, 64, 64)
    10. self.parent = parent
    11. self.imH = 64
    12. self.imW = 64
    13. self.fill(QtGui.QColor(0, 255, 255))
    14. self.color = QtGui.QColor(0, 0, 0)
    15.  
    16. def paintEvent(self, point=False):
    17. if point:
    18. p = QtGui.QPainter(self)
    19. p.setPen(QtGui.QPen(self.color, 1, QtCore.Qt.SolidLine))
    20. p.drawPoints(point)
    21.  
    22. def clic(self, mouseX, mouseY):
    23. self.paintEvent(QtCore.QPoint(mouseX, mouseY))
    24.  
    25. class GraphWidget(QtGui.QGraphicsView):
    26. """ Display, zoom, pan..."""
    27. def __init__(self):
    28. QtGui.QGraphicsView.__init__(self)
    29. self.im = Canvas(self)
    30. self.imH = self.im.height()
    31. self.imW = self.im.width()
    32. self.zoomN = 1
    33. self.scene = QtGui.QGraphicsScene(self)
    34. self.scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
    35. self.scene.setSceneRect(0, 0, self.imW, self.imH)
    36. self.scene.addPixmap(self.im)
    37. self.setScene(self.scene)
    38. self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
    39. self.setResizeAnchor(QtGui.QGraphicsView.AnchorViewCenter)
    40. self.setMinimumSize(400, 400)
    41. self.setWindowTitle("pix")
    42.  
    43. def mousePressEvent(self, event):
    44. if event.buttons() == QtCore.Qt.LeftButton:
    45. pos = self.mapToScene(event.pos())
    46. self.im.clic(pos.x(), pos.y())
    47. #~ self.scene.update(0,0,64,64)
    48. #~ self.updateScene([QtCore.QRectF(0,0,64,64)])
    49. self.scene.addPixmap(self.im)
    50. print('items')
    51. print(self.scene.items())
    52. else:
    53. return QtGui.QGraphicsView.mousePressEvent(self, event)
    54.  
    55. def wheelEvent(self, event):
    56. if event.delta() > 0:
    57. self.scaleView(2)
    58. elif event.delta() < 0:
    59. self.scaleView(0.5)
    60.  
    61. def scaleView(self, factor):
    62. n = self.zoomN * factor
    63. if n < 1 or n > 16:
    64. return
    65. self.zoomN = n
    66. self.scale(factor, factor)
    67.  
    68. if __name__ == '__main__':
    69. import sys
    70. app = QtGui.QApplication(sys.argv)
    71. widget = GraphWidget()
    72. widget.show()
    73. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 
    Modified it as this:

    Qt Code:
    1. #!/usr/bin/env python
    2.  
    3. from PyQt4 import QtCore
    4. from PyQt4 import QtGui
    5. import h5py
    6. import numpy
    7.  
    8.  
    9. class Canvas(QtGui.QPixmap):
    10. """ Canvas for drawing"""
    11. def __init__(self, parent=None):
    12. QtGui.QPixmap.__init__(self, 1000, 1000)
    13. self.parent = parent
    14. self.imH = 1000
    15. self.imW = 1000
    16. self.fill(QtGui.QColor(0, 255, 255))
    17. self.color = QtGui.QColor(0, 0, 0)
    18.  
    19. def paintEvent(self, c, point=False):
    20. if point:
    21. p = QtGui.QPainter(self)
    22. p.setPen(QtGui.QPen(QtGui.QColor(c, c, c), 1, QtCore.Qt.SolidLine))
    23. p.drawPoints(point)
    24.  
    25. def clic(self, mouseX, mouseY, c):
    26. self.paintEvent(c, QtCore.QPoint(mouseX, mouseY))
    27.  
    28.  
    29. class GraphWidget(QtGui.QGraphicsView):
    30. """ Display, zoom, pan..."""
    31. def __init__(self):
    32. QtGui.QGraphicsView.__init__(self)
    33. self.im = Canvas(self)
    34. self.imH = self.im.height()
    35. self.imW = self.im.width()
    36. self.zoomN = 1
    37. self.scene = QtGui.QGraphicsScene(self)
    38. self.scene.setItemIndexMethod(QtGui.QGraphicsScene.NoIndex)
    39. self.scene.setSceneRect(0, 0, self.imW, self.imH)
    40. self.scene.addPixmap(self.im)
    41. self.setScene(self.scene)
    42. self.setTransformationAnchor(QtGui.QGraphicsView.AnchorUnderMouse)
    43. self.setResizeAnchor(QtGui.QGraphicsView.AnchorViewCenter)
    44. self.setMinimumSize(400, 400)
    45. self.setWindowTitle("pix")
    46.  
    47. f = h5py.File('MSG3_201303312045.H5','r')
    48. g=f['image4/image_data']
    49. for i in range(50,3000):
    50. for u in range(50,3000):
    51. print(str(i) + " - " + str(u))
    52. #self.im.setPen(QPen(QColor(qRgb(g[i,u],g[i,u],g[i,u])),1,Qt.SolidLine))
    53. self.im.clic(i-50,u-50, g[i,u])
    54. #self.fill(QtGui.QColor(g[i,u], g[i,u], g[i,u]))
    55. self.scene.addPixmap(self.im)
    56.  
    57. def mousePressEvent(self, event):
    58. if event.buttons() == QtCore.Qt.LeftButton:
    59. pos = self.mapToScene(event.pos())
    60. self.im.clic(pos.x(), pos.y())
    61. #~ self.scene.update(0,0,64,64)
    62. #~ self.updateScene([QtCore.QRectF(0,0,64,64)])
    63. self.scene.addPixmap(self.im)
    64. print('items')
    65. else:
    66. return QtGui.QGraphicsView.mousePressEvent(self, event)
    67.  
    68. def wheelEvent(self, event):
    69. if event.delta() > 0:
    70. self.scaleView(2)
    71. elif event.delta() < 0:
    72. self.scaleView(0.5)
    73.  
    74. def scaleView(self, factor):
    75. n = self.zoomN * factor
    76. if n < 1 or n > 16:
    77. return
    78. self.zoomN = n
    79. self.scale(factor, factor)
    80.  
    81. if __name__ == '__main__':
    82. import sys
    83. app = QtGui.QApplication(sys.argv)
    84. widget = GraphWidget()
    85. widget.show()
    86. sys.exit(app.exec_())
    To copy to clipboard, switch view to plain text mode 

    p.s: The file I attached is a group of a hdf5 file. So I read it directly from hdf5 for display.

    Now there is another problem. It took ~19.5 minutes to read and display the file.
    what should I do? Maybe using opengl would be a solution for this case.

    p.s: It's not an 8 bit file. There is some number bigger than 255...

  9. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: grayscaled image from number matrix

    Quote Originally Posted by mshemuni View Post
    Now there is another problem. It took ~19.5 minutes to read and display the file.
    what should I do? Maybe using opengl would be a solution for this case.
    I think reading is the problem, not display.

    p.s: It's not an 8 bit file. There is some number bigger than 255...
    Yeah, well... so how does that relate to:

    I have a huge(40MB) 8 bit(0-255) text file.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #9
    Join Date
    Sep 2012
    Location
    Ankara
    Posts
    15
    Qt products
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: grayscaled image from number matrix

    Well now I get it.
    I thought there was numbers on the file ranged 0-255 so that meant 8 bit depth to me. It's about my English. I can't write what I meant yet.

    Reading is not problem because I'm using h5py. I can display a number of the matrix in milliseconds.
    The reading lines are:

    f = h5py.File('MSG3_201303312045.H5','r')
    g=f['image4/image_data']

    Now g is a matrix.

  11. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: grayscaled image from number matrix

    So where does this 19 minutes come from?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. #11
    Join Date
    Sep 2012
    Location
    Ankara
    Posts
    15
    Qt products
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: grayscaled image from number matrix

    When I try to get all numbers from matrix and paint a dot for each on screen, it takes 19 minutes...
    Last edited by mshemuni; 4th April 2013 at 22:12.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: grayscaled image from number matrix

    So don't paint each dot separately.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  14. #13
    Join Date
    Sep 2012
    Location
    Ankara
    Posts
    15
    Qt products
    Platforms
    Unix/X11
    Thanks
    3

    Default Re: grayscaled image from number matrix

    I tried to calculate every 9 pixels value's average and paint it on one pixel. It made script works faster. But it still takes 12 minutes for complete painting.

  15. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: grayscaled image from number matrix

    No, you didn't understand me. You are currently painting one pixel at a time instead of setting all the pixels of the image at once (we have shown you how to do that faster using QImage::setPixel() and you can do it even faster with QImage::scanLine() or QImage::bits()). And also get rid of all the print statements, they are slowing down your script significantly.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 5th March 2012, 07:34
  2. LED Matrix
    By jwflammer in forum Newbie
    Replies: 1
    Last Post: 23rd February 2011, 02:23
  3. matrix for QBrush
    By navi1084 in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2010, 12:27
  4. Dot Matrix Printer
    By estanisgeyer in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2009, 20:30
  5. Image processing via matrix
    By jones.79 in forum Qt Programming
    Replies: 10
    Last Post: 22nd September 2008, 01:42

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.