简体   繁体   English

OpenCV .imshow()未显示正确大小的图像

[英]OpenCV .imshow() is not displaying a properly sized image

I am trying to read an image using opencv, do some transformations (resize and offsets), then as a last step, do a crop on the image. 我正在尝试使用opencv读取图像,进行一些转换(调整大小和偏移量),然后作为最后一步,在图像上进行裁剪。 In my final line crop_img = offset_image[0:1080, 0:1920].copy() , I expect a cropped 1920x1080 image to be created. 在我的最后一行crop_img = offset_image[0:1080, 0:1920].copy() ,我希望创建一个裁剪的1920x1080图像。 My crop_img.size print out shows that that is correct. 我的crop_img.size打印输出表明这是正确的。 But, when I do an .imshow(), it is displaying the full sized, original image. 但是,当我执行.imshow()时,它将显示完整尺寸的原始图像。

import numpy as np
import cv2 as cv
import copy

original = cv.imread("IMG_0015_guides.jpg", cv.IMREAD_UNCHANGED)
img_resize = cv.resize(original, (0,0), fx=.9, fy=.9) 


rows,cols,_ = img_resize.shape
M = np.float32([[1,0,100],[0,1,50]])
offset_image = cv.warpAffine(img_resize,M,(cols,rows))

crop_img = offset_image[0:1080, 0:1920].copy()

print('img_resize {}'.format(img_resize.shape))
print('offset_image {}'.format(offset_image.shape))
print('cropped {}'.format(crop_img.shape))

cv.imshow('image',crop_img)
cv.waitKey(0)
cv.destroyAllWindows()

>>> img_resize (3110, 4666, 3)
>>> offset_image (3110, 4666, 3)
>>> cropped (1080, 1920, 3)

I'm totally baffled. 我完全困惑。 Why is it not showing me the cropped 1920x1080 image? 为什么不向我显示裁剪后的1920x1080图像?

Working with massive images can get confusing when visualizing with OpenCV's imshow. 使用OpenCV的imshow进行可视化处理时,处理大量图像会引起混淆。 I ran your code and it seems like its doing what you expect it to do. 我运行了您的代码,它似乎按照您的期望去做。 I suggest resizing your image again for visualization purposes only. 我建议再次调整图像大小仅出于可视化目的。 The following code ran successfully on this image . 以下代码在此映像上成功运行。

import numpy as np
import cv2 as cv
import copy

original = cv.imread("4k-image-tiger-jumping.jpg", cv.IMREAD_UNCHANGED)

# resize original for visualization purposes only
print('original {}'.format(original.shape))
original_resized = cv.resize(original, (0,0), fx=.1, fy=.1) 
cv.imshow('original_resize',original_resized)


img_resize = cv.resize(original, (0,0), fx=.9, fy=.9) 

rows,cols,_ = img_resize.shape
M = np.float32([[1,0,100],[0,1,50]])
offset_image = cv.warpAffine(img_resize,M,(cols,rows))

crop_img = offset_image[0:1080, 0:1920].copy()

print('img_resize {}'.format(img_resize.shape))
print('offset_image {}'.format(offset_image.shape))
print('cropped {}'.format(crop_img.shape))


# resize cropped for visualization purposes only
vis_r,vis_c,_ = original_resized.shape
cropped_resized = cv.resize(crop_img, (vis_c, vis_r))
cv.imshow('cropped_resized',cropped_resized)


# cv.imshow('image',crop_img)

cv.waitKey(0)
cv.destroyAllWindows()

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

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