[英]How to fit image size to screen with cv2.Imshow
首先,我很抱歉使用翻译发布问题,因为我不擅长英语。
我正在用 Python 制作一个程序,该程序显示按顺序通过 ObjectDetection 的图像。
由于不同尺寸的图像逐张显示,图像尺寸失真很大。
那么,如何输出图像,以便在同一输出屏幕上调整大小并连续转换,以免失真?
下面是我现在使用的代码。
def DetectImage(_PATH_TO_IMAGE, _IMAGE_NAME, _sess, _category_index):
image = cv2.imread(_PATH_TO_IMAGE)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_expanded = np.expand_dims(image_rgb, axis=0)
(boxes, scores, classes, num) = _sess.run(
[detection_boxes, detection_scores, detection_classes, num_detections],
feed_dict={image_tensor: image_expanded})
# Draw the results of the detection (aka 'visulaize the results')
vis_util.visualize_boxes_and_labels_on_image_array(
image,
np.squeeze(boxes),
np.squeeze(classes).astype(np.int32),
np.squeeze(scores),
category_index,
use_normalized_coordinates=True,
line_thickness=5,
min_score_thresh=0.60)
cv2.namedWindow('ObjectDetection_Checker', cv2.WINDOW_NORMAL)
cv2.resizeWindow('ObjectDetection_Checker', 1280, 720)
cv2.imshow('ObjectDetection_Checker', image)
# All the results have been drawn on image. Now display the image.
cv2.imwrite('images/CheckedImages/' + _IMAGE_NAME + '_Checked.jpg', image)
cv2.waitKey(1)
time.sleep(1)
由于不同的屏幕具有不同的尺寸,您可以使用ctypes
获取屏幕的尺寸;
import ctypes
user32 = ctypes.windll.user32
screensize = user32.GetSystemMetrics(0), user32.GetSystemMetrics(1)
然后只需将图像大小调整为变量screensize
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.