Results 1 to 15 of 15

Thread: QPixmap blur when QGraphicsView scale

  1. #1
    Join Date
    Oct 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default QPixmap blur when QGraphicsView scale

    Hello

    I am developing a tools to visualize for the image processing result. The problem is that I add a pixmap on the QGraphicsScene and other items on the scene. My user found the position of the items are moving during zooming from this pixel to the neighbour and then back.

    My user need to zoom the screen to get exact pixel view of the image.
    I use the QGraphicsView->scale(factor, factor) to zoom in and out.
    When the factor is 2, there is no problem, but we need the factor as 1.2 or 1.5, then the pixmap will blur like the picture. The line or other items are not at the correct position relative to the pixmap. ( the lines position is correct, the pixmap is not)

    qGraphicsView.jpg

    I tried the settings about
    setRenderHint setOptimizationFlag
    none of them works
    I cannot setTransformationMode( Qt::SmoothTransformation ), because this is not what we want. we need subpixel level image to be shown correctly.

    Please tell me how to set the QGraphicsView to get the correct pixmap.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QPixmap blur when QGraphicsView scale

    What "exact pixel" is at position (0.83, 0.83) in the source image, because that is what you would expect at (1,1) at scale 1.2? There isn't one, so the image is scaled and pixels interpolated to get an estimate of what should be there. This seems like normal operation.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap blur when QGraphicsView scale

    Please tell me how to set the QGraphicsView to get the correct pixmap.
    As ChrisW67 said, Qt is doing what it should do. What Qt is giving you is a magnified but pixellated image. This is the same thing you get when you use a digital camera to zoom in. The problem in both cases is that your image still has the same number of pixels, while the view that you are projecting onto has many more. Thus, the image pixels are simply scaled to cover bigger rectangles.

    What you are asking for is an interpolated image. Google for "bilinear interpolation". There are lots of examples and implementations. You will need to create a new pixmap each time the zoom level changes, by sampling and interpolating the zoom region of the original image to cover the new view size.

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

    Default Re: QPixmap blur when QGraphicsView scale

    I think the Smooth pixmap transform mode might be doing bilinear interpolation. Fast mode does the nearest neighbour algorithm, I guess. If one wants to preserve colors and at the same time have pixel perfect image scaling, one should only allow zoom levels that allow for such attributes.
    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
    Oct 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap blur when QGraphicsView scale

    I think what you said is correct, Smoothtransform get the picture totally blurred when you zoom in in certain level. Fast mode looks like the pixel is divided into several smaller ones, and some get the nearest neighboured color. That is why I cannot get the whole bigger pixel with the same color at the correct position.

    How you mean to allow zoom levels that allow for such attributes, that I can only set the scale factor to 2 or set a maximum saled value? But that cannot fullfill our users request!


    Added after 6 minutes:


    The problem is that "the image pixels are simply scaled to cover bigger rectangles." This is exactly what I need but cannot get.

    You can check on my image that the pixmap is with the size( 1024,768) and the line is drawed at the postion (1010, 767) to (1024,767). I also tracked the cursor's position in QGraphicsScene. The line is drawed at the correct position, but the pixmap is not. The line should be exactly between two big pixel lines, but it is in the middle. Then if I zoom in further, the pixmap is correct and then that is how my user find that the line is moving and then back.

    If as you said the pixmap has the same number of pixels, but just cover bigger rectangles, why in the middle of my imag, the rectangles get blurred, That is not what I expect as the upper side of the pixels, that are correctly shown.
    Last edited by shanshan; 17th October 2014 at 08:54.

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

    Default Re: QPixmap blur when QGraphicsView scale

    Quote Originally Posted by shanshan View Post
    How you mean to allow zoom levels that allow for such attributes, that I can only set the scale factor to 2 or set a maximum saled value?
    Use scale factors that will result in integer number of pixels forming from the original pixel (thus integer scale factors).

    But that cannot fullfill our users request!
    If you have an image consisting of one white and one black pixel (1+1) and you zoom it up by 1.5x then you end up with 1.5 white and 1.5 black pixels however that cannot be represented on three pixels as each one needs to have only one color. Therefore you end up with either one white, one black and one gray pixel in the middle (interpolation) or 2 pixels of one color and one pixel of the other color (nearest neightbour). The latter means the proportions of pixel colors in your image changed from 1:1 to 2:1, falsifying the image greatly. You cannot eat the cake and still keep the cake.
    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. #7
    Join Date
    Oct 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap blur when QGraphicsView scale

    That is clear enough, thanks!
    The 1.5 pixel example is simple and clear, I have the rough idea but I still thought that qt can automatically solve the problem, because when we zoom in in certain level to saw one pixel size in screen, the pixel size in image is hundreds of times of the screen resolution.
    If I still need to fix on 1.5 scalefactor I need to sample the img by myself in some deep level, or just simply set integer zoom factor.
    Thanks a lot!


    Added after 55 minutes:


    Hello,

    I explain my user your example, but he is not satisfied and ask more questions.

    If it is scale factor not integer problem, that the radom sampling only should be one original image pixel size. But what we got is clearly much bigger than one image pixel.

    If the resample of the image pixel( interpolation) is taking one nearest, what is the qt algorithm to choose the color, why there is not staight cut for certain interpolation but sometimes choose left sometimes choose right. Why sometimes we get the straight rectangles and sometimes it is not. And when we drag the scene, the colors changed?
    Last edited by shanshan; 17th October 2014 at 10:35.

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

    Default Re: QPixmap blur when QGraphicsView scale

    Quote Originally Posted by shanshan View Post
    why there is not staight cut for certain interpolation
    Because you cannot cut in the middle of a pixel. If you scale up n times, you still take colours from the original image so with our example you still sample from one black and one white pixel. All interpolated pixels closer to the original black one will be black and all those closer to the white one will be white.

    Tell your client you cannot upscale an image without losing quality. That happens only in the movies. If your client wants better results he should provide you with higher-resolution image or settle down for an interpolated result.

    If you want to support zoom factor resolution of 0.1 (e.g 1.2, 1.3, 1.4, ...) without losing quality, the resolution of the original image should be 10 times larger (that is every physical separate point in the destination (displayed) image of scale 1 should be represented by 10 samples of the original physical "point" represented by that pixel. In all other cases the quality will be affected.
    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.


  9. #9
    Join Date
    Oct 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap blur when QGraphicsView scale

    The example is as picture
    in some step it is ok, some is not, and the interpolation pixel ( incorrect color) is more than 1.
    The insteresting thing is that when we saw the distortion, if we drag the scene, and the distortion changes. Is this a bug in qt or we need to do some settings?
    qt.jpg

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

    Default Re: QPixmap blur when QGraphicsView scale

    What is the size of the original pixmap?

    BTW. 1.5^8 = 25.628906
    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.


  11. #11
    Join Date
    Oct 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap blur when QGraphicsView scale

    Hello,

    thanks for your fast reply. But this explains that one step scale, that 1.2, 1.3, 1.4... only one image pixel is interpolated. But our case is that we scaled many steps, the image is zoomed 1.5^9 times to 38.4434, but not only one pixel at 39 is interpolated, but multiple of them.
    Thant means that when the picture of (1*2) is zoomed to (1,77), (1-38) should be black, but they are not. about( 20-38) are all random and they change when we drag the image. That is why we thought it is a bug of qt


    Added after 7 minutes:


    The pixmap is 1024*768, the line is at 767,
    the line stays between two pixel when zoom 1.5^8 = 25.6289, get higher at 1.5^9 = 38.4434 and move back between two pixel at 1.5^10=57.665

    this is just an sample that to point out the problem, our users need to see the circles or results inside exact each image pixel, cannot accept them move around and back.

    qt1.jpg
    Last edited by shanshan; 17th October 2014 at 11:23.

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

    Default Re: QPixmap blur when QGraphicsView scale

    I have no idea what you do with your image but it seems Qt behaves correctly, here is a sample app that demonstrates this:

    Qt Code:
    1. #include <QtWidgets>
    2.  
    3. class GraphicsView : public QGraphicsView {
    4. public:
    5. GraphicsView() { m_scale = 1; }
    6. protected:
    7. void wheelEvent(QWheelEvent *ev) {
    8. if(ev->delta() > 0) {
    9. m_scale *= 1.5;
    10. qDebug() << "+1.5" << m_scale;
    11. scale(1.5, 1.5);
    12. } else {
    13. m_scale /= 1.5;
    14. qDebug() << "-1.5" << m_scale;
    15. scale(1/1.5, 1/1.5);
    16. }
    17. }
    18. private:
    19. qreal m_scale;
    20. };
    21.  
    22. int main(int argc, char **argv) {
    23. QApplication app(argc, argv);
    24.  
    25. QPixmap px(2,2);
    26. px.fill(Qt::white);
    27. QPainter p(&px);
    28. p.setPen(Qt::black);
    29. p.drawPoint(0,0);
    30. p.drawPoint(1,1);
    31. p.end();
    32. scene.addPixmap(px);
    33. GraphicsView view;
    34. view.setScene(&scene);
    35. view.show();
    36. return app.exec();
    37. }
    To copy to clipboard, switch view to plain text mode 

    You can see that with small scales the squares are not even but the situation improves as the scales get larger.
    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.


  13. #13
    Join Date
    Oct 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap blur when QGraphicsView scale

    What I did is a big project and which handled image is like this

    Qt Code:
    1. MLGraphicsView( MLGraphicsScene * scene, QWidget * parent = 0, Qt::WindowFlags f = 0 )
    2. :QGraphicsView( scene )
    3. ,m_scale(1.0)
    4. {
    5. setDragMode( QGraphicsView::ScrollHandDrag );
    6. }
    7. ~MLGraphicsView()
    8. {}
    9. protected:
    10. //Take over the interaction
    11. virtual void wheelEvent(QWheelEvent* event)
    12. {
    13. setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    14. setResizeAnchor(QGraphicsView::AnchorUnderMouse);
    15. // Scale the view / do the zoom
    16. double scaleFactor = 1.5;
    17.  
    18. if(event->delta() > 0) {
    19. // Zoom in
    20. scale(scaleFactor, scaleFactor);
    21. m_scale *= scaleFactor;
    22.  
    23. } else {
    24. scale(1.0 / scaleFactor, 1.0 / scaleFactor);
    25. m_scale /= scaleFactor;
    26. }
    27.  
    28. // some other part
    29. QGraphicsScene* gScene = this->getView()->scene();
    30.  
    31. gScene->clear();
    32.  
    33. QPixmap pixmap = m_visDocument->getImag( camId );
    34. if( !pixmap )
    35. {
    36. ART_LOG_ERROR( logger, "Camera " << camId << " is empty. " );
    37. continue;
    38. }
    39. QGraphicsPixmapItem * qpixmap = gScene->addPixmap( pixmap );
    40. QGraphicsLineItem* line = new QGraphicsLineItem( -size, 0, size, 0 );
    41. gScene->addItem(line);
    To copy to clipboard, switch view to plain text mode 

    what you did is just a 2*2 pixmap and with the same color, you can try load a bigger windows picture, and then zoom it in in bigger levels, maybe you will see what I get.

    Like the 1.5^9 = 38.4434 times, that if qt is doing right, 1-38 pixel should be same color, but 39 is random. But in my img you saw, it lookes like 1-25 is sampled correct, 25-38is not. It is obviously wrong!

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

    Default Re: QPixmap blur when QGraphicsView scale

    Can you post a minimal compilable example reproducing the problem?
    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.


  15. #15
    Join Date
    Oct 2014
    Posts
    7
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPixmap blur when QGraphicsView scale

    Hello, the code is as followed,

    I work with the windows default pixtures and
    here is the screenshot
    when scale factor 25.6289, no problem, when 38.** or 57.6**, the line is not in the correct postion, and when drag the scene with mouse, the pixels blured and changed.

    Qt Code:
    1. #include <QtWidgets>
    2. #include <QGraphicsView>
    3. #include <QWheelEvent>
    4. #include <QApplication>
    5.  
    6.  
    7. class GraphicsView : public QGraphicsView
    8. {
    9.  
    10. public:
    11. GraphicsView ( QWidget* parent = 0 )
    12. :QGraphicsView( parent )
    13. ,m_scale(1.0)
    14. {
    15. setDragMode( QGraphicsView::ScrollHandDrag );
    16. }
    17. protected:
    18. void wheelEvent(QWheelEvent *ev)
    19. {
    20. setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
    21. setResizeAnchor(QGraphicsView::AnchorUnderMouse);
    22. if(ev->delta() > 0) {
    23. m_scale *= 1.5;
    24. qDebug() << "+1.5" << m_scale;
    25. scale(1.5, 1.5);
    26. } else {
    27. m_scale /= 1.5;
    28. qDebug() << "-1.5" << m_scale;
    29. scale(1/1.5, 1/1.5);
    30. }
    31. }
    32. private:
    33. qreal m_scale;
    34. };
    35. int main(int argc, char **argv)
    36. {
    37. QApplication app(argc, argv);
    38. QPixmap px;
    39.  
    40. QString fileName = QFileDialog::getOpenFileName(0, QString("Open File"), QDir::currentPath(), "Image Files (*.png *.jpg *.bmp)");
    41. if (!fileName.isEmpty())
    42. {
    43. QFileInfo fi( fileName );
    44. if( fi.exists())
    45. {
    46. bool err = px.load( fi.absoluteFilePath());
    47. if (!err)
    48. return 0;
    49. }
    50. }
    51.  
    52. scene.addPixmap(px);
    53. QGraphicsLineItem* line = new QGraphicsLineItem( px.width()-10,px.height()-1,px.width(),px.height()-1 );
    54. QPen pen(Qt::red, 1.0 );
    55. pen.setCosmetic( true );
    56. line->setPen( pen );
    57. scene.addItem( line );
    58. GraphicsView view;
    59. view.setScene(&scene);
    60. view.show();
    61. return app.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 

    qttest1.jpg qttest2.jpgqttest3.jpgqttest4.jpg

Similar Threads

  1. scale a QGraphicsView, and it's issues..
    By been_1990 in forum Qt Programming
    Replies: 7
    Last Post: 14th November 2016, 20:25
  2. Replies: 5
    Last Post: 8th July 2011, 08:06
  3. QGraphicsView scale question.
    By hickscorp in forum Qt Programming
    Replies: 20
    Last Post: 15th April 2007, 02:28
  4. QGraphicsView scale function
    By forrestfsu in forum Qt Programming
    Replies: 2
    Last Post: 12th October 2006, 14:05
  5. QPixmap xx.scaledToWidth dont Scale but load...
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 25th June 2006, 23:21

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.