Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python opencv to put multiple pictures in one window

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces how to use python opencv to put multiple pictures in a window related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that after reading this article on how to use python opencv to put multiple pictures in a window article will have a harvest, let's take a look at it.

Programming requirements: use python+opencv to process the image and display the two pictures together

Method: stack all the images horizontally or vertically in the numpy module, and finally display them as a whole picture.

Note: if you display pictures of different sizes (width and height) or different color channels together, a black screen will occur, so either the height of the two images is set to the same height or the width is the same. So that it can be displayed properly.

Take a chestnut:

1. Only set two pictures of the same height, stitching two pictures, display; only setting two pictures of the same width, stitching two pictures, display.

Import cv2import numpy as np# read figure 1 and figure 2img1 = cv2.imread (r "C:\ Users\ thorne\ PycharmProjects\ biyesheji\ image\ 3.jpg") img2 = cv2.imread (r "C:\ Users\ thorne\ PycharmProjects\ biyesheji\ image\ 1.jpeg") # reset the width and height of figure 1 img1 = cv2.resize (img1, (400,400)) # set the height of figure 2 to 400, the same as figure 1, and set the width at will I set it here as 200img2 = cv2.resize (img2, (200,400)) # set the width of figure 2 to 300. it is the same as figure 1, and the height is arbitrarily set. Here, I set it to 200img3 = cv2.resize (img2, (400,200)) # images of the same height, horizontal mosaics imgStackH = np.hstack ((img1, img2)) # images of the same width Vertical horizontal mosaic imgStackV = np.vstack ((img1, img3)) # display image imgStackH horizontal horizontal mosaic cv2.imshow ("DemoStackH", imgStackH) # display image imgStackV vertical horizontal mosaic cv2.imshow ("DemoStackV", imgStackV) key = cv2.waitKey (0) # wait for key command

Running result: the width is the same as the splicing.

Splicing at the same height

As can be seen from the above results, although the two pictures are spliced together, their respective proportions are completely wrong, so when using this method, you should know that this method is only suitable for two pictures / multiple pictures with the same or similar width-height ratio.

Originally, the scale of the two pictures is similar, but in order to understand, I have changed the scale of figure 1 in the above example.

The following shows the splicing results of two images with similar scale: (only if the stitching height is the same, and the width is similar to this, do not write)

Attached code:

Import cv2import numpy as np# img1 = cv2.imread (r "C:\ Users\ thorne\ PycharmProjects\ biyesheji\ image\ 3.jpg") img2 = cv2.imread (r "C:\ Users\ thorne\ PycharmProjects\ biyesheji\ image\ 1.jpeg") # 1. Set the height of the two figures to the same height, then splice # print to understand the height of figure 1: 634 and width: 400 (the first parameter of the shape method is high) print (img1.shape) # print to understand the width and height of figure 2: (1000) print (img2.shape) # set the height of figure 2 to: 634 resize sets the width and height of the second parameter is the height, the width of figure 2 is customized Here I set 400img2 = cv2.resize (img2, (400,634)) # the same height image can be spliced horizontally imgStackH = np.hstack ((img1, img2)) # display the horizontal mosaic image imgStackHcv2.imshow ("DemoStackH", imgStackH) key = cv2.waitKey (0) # waiting for the key command on "how to use python opencv to put multiple images in one window" this article is introduced here, thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to use python opencv to put multiple pictures in one window". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 274

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report