简体   繁体   English

Python - OpenCV - imread - 显示图像

[英]Python - OpenCV - imread - Displaying Image

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: 图片: 图片

Screenshot: 截图: 截图

Looks like the image is too big and the window simply doesn't fit the screen. 看起来图像太大,窗口根本不适合屏幕。 Create window with the cv2.WINDOW_NORMAL flag, it will make it scalable. 使用cv2.WINDOW_NORMAL标志创建窗口,它将使其可伸缩。 Then you can resize it to fit your screen like this: 然后你可以调整它以适应你的屏幕,如下所示:

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 documentation CV_WINDOW_KEEPRATIO flag should do the same, yet it doesn't and it's value not even presented in the python module. 根据OpenCV文档, CV_WINDOW_KEEPRATIO标志应该做同样的事情,但它没有,它的值甚至没有在python模块中呈现。

This can help you 这可以帮到你

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

In openCV whenever you try to display an oversized image or image bigger than your display resolution you get the cropped display. 在openCV中,当您尝试显示超出显示分辨率的超大图像或图像时,您将获得裁剪显示。 It's a default behaviour. 这是默认行为。
In order to view the image in the window of your choice openCV encourages to use named window. 为了在您选择的窗口中查看图像,openCV鼓励使用命名窗口。 Please refer to namedWindow documentation 请参阅namedWindow文档

The function namedWindow creates a window that can be used as a placeholder for images and trackbars. 名为Window的函数创建一个窗口,可用作图像和轨迹栏的占位符。 Created windows are referred to by their names. 创建的窗口由其名称引用。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM