Python - OpenCV - imread - 显示图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19285562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:20:34  来源:igfitidea点击:

Python - OpenCV - imread - Displaying Image

pythonimageopencvimshow

提问by Sinjin Forde

I am currently working on reading an image and displaying it to a window. I have successfully done this, but upon displaying the image, the window only allows me to see a portion of the full image. I tried saving the image after loading it, and it saved the entire image. So I am fairly certain that it is reading the entire image.

我目前正在阅读图像并将其显示到窗口中。我已经成功地做到了这一点,但是在显示图像时,窗口只允许我看到完整图像的一部分。我在加载后尝试保存图像,它保存了整个图像。所以我相当确定它正在读取整个图像。

imgFile = cv.imread('1.jpg')

cv.imshow('dst_rt', imgFile)
cv.waitKey(0)
cv.destroyAllWindows()

Image: image

图片: 图片

Screenshot: screenshot

截屏: 截屏

采纳答案by Igonato

Looks like the image is too big and the window simply doesn't fit the screen. Create window with the cv2.WINDOW_NORMALflag, it will make it scalable. Then you can resize it to fit your screen like this:

看起来图像太大,窗口根本不适合屏幕。创建带有cv2.WINDOW_NORMAL标志的窗口,它将使其具有可扩展性。然后你可以像这样调整它的大小以适合你的屏幕:

from __future__ import division
import cv2


img = cv2.imread('1.jpg')

screen_res = 1280, 720
scale_width = screen_res[0] / img.shape[1]
scale_height = screen_res[1] / img.shape[0]
scale = min(scale_width, scale_height)
window_width = int(img.shape[1] * scale)
window_height = int(img.shape[0] * scale)

cv2.namedWindow('dst_rt', cv2.WINDOW_NORMAL)
cv2.resizeWindow('dst_rt', window_width, window_height)

cv2.imshow('dst_rt', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

According to the OpenCV documentationCV_WINDOW_KEEPRATIOflag should do the same, yet it doesn't and it's value not even presented in the python module.

根据 OpenCV 文档CV_WINDOW_KEEPRATIO标志应该做同样的事情,但它没有,它的值甚至没有出现在 python 模块中。

回答by Tom J Muthirenthi

This can help you

这可以帮助你

namedWindow( "Display window", CV_WINDOW_AUTOSIZE );// Create a window for display.
imshow( "Display window", image );                   // Show our image inside it.

回答by Mahesh

In openCV whenever you try to display an oversized image or image bigger than your display resolution you get the cropped display. It's a default behaviour.
In order to view the image in the window of your choice openCV encourages to use named window. Please refer to namedWindow documentation

在 openCV 中,每当您尝试显示超大图像或大于显示分辨率的图像时,您都会得到裁剪的显示。这是默认行为。
为了在您选择的窗口中查看图像,openCV 鼓励使用命名窗口。请参考namedWindow文档

The function namedWindow creates a window that can be used as a placeholder for images and trackbars. Created windows are referred to by their names.

函数 namedWindow 创建一个窗口,可以用作图像和轨迹栏的占位符。创建的窗口以其名称引用。

cv.namedWindow(name, flags=CV_WINDOW_AUTOSIZE)where each window is related to image container by the name arg, make sure to use same name

cv.namedWindow(name, flags=CV_WINDOW_AUTOSIZE)其中每个窗口都通过名称 arg 与图像容器相关,请确保使用相同的名称

eg:

例如:

import cv2
frame = cv2.imread('1.jpg')
cv2.namedWindow("Display 1")
cv2.resizeWindow("Display 1", 300, 300)
cv2.imshow("Display 1", frame)