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 mix images with Python and OpenCV

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use Python and OpenCV for image mixing", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to mix images with Python and OpenCV.

Brief introduction

Photoshop and other image processing programs (for example, free software alternatives such as GIMP or Paint.net) offer a wide range of possibilities for editing and manipulating images and photos. Sometimes, however, people want to perform operations that are not available in standard software, or just want to know how specific photo editing programs work. Both goals can be achieved by trying to convert a photo editor into programming code. Here, in this text, I will do this and provide code for merging part of the image (or the entire image) with another image.

To understand the content of this tutorial, you should have a basic knowledge of Python (Python 3) and how images are represented on your computer (that is, the RGB or BGR model). However, because the code is not overly complex, it should also be accessible to people with backgrounds in other programming languages. If you plan to execute or use the code I loaded, you need to install OpenCv and NumPy (using the pip command or Anaconda Navigator) and, of course, Python on your machine. OpenCV is a freely available image processing library (interface can be used for C++, Python, etc.), including many functions to help with visual input such as video and images. Here, in this code, I only use some basic methods. I use commands to load and display images on the screen and set up a while loop to enable some interaction with the program (but this is also very practical). As mentioned earlier, NumPy is also required. NumPy is the largest library for Python to deal with numbers, arrays and matrixes. It is very convenient and can be used to manipulate and modify OpenCv image data using NumPy methods.

Background mixed image

Sample code on how to use OpenCV to merge information stored in one image with information in a second image is widely available on the network. For example, the cv2.addWeighted (image1, weighting_factor1, image2, weighting_factor2) OpenCV function is an one-line function that can perform image blending in a simple way. If both weighting factors choose 0.5, the function returns the equal weight (or mean) mixing of the two images. Selecting 0.7 and 0.3 as input weights causes the image to consist of 70% pixel color of the first image and 30% pixel color of the second image. To give another example, selecting 1 and 0 will return the unchanged version of image 1, while 0 and 1 will return the unchanged version of image 2. I mentioned this because it became important below when I went into the details of the code I wrote.

Insert an image using a mask

The cv2.addWeighted () function is particularly useful when the information of two images of the same size (or, more accurately, the region of interest) is combined and the same weighting factor is applied to all pixels. However, sometimes people plan to move a particular area from one image to another. This can be done in different ways, but a common way is to use masks. The mask is usually a binary image (containing only black and white pixels) and is used as a template to surround the insertion or blending area. In order to clarify this point, I give an example. If you want to replace the sky of the image with the background of the second image, the mask (or binary image) needs to have a black area that represents the sky of image one so that the program "knows" where to put the background of figure 2. In other words, where the pixel color is black (0 in an 8-bit image; [0pc0

Image superposition using weighting factor matrix

In the sample code I show here, I combine the image blending method with the mask-based image insertion method. Compared to the "standard" method, I use a non-binary mask (you can save all values between 0 and 255), where each pixel is used as a separate weight. Therefore, instead of defining the overall weighting factor of image one (for example, 0.7) and the overall weighting factor of image two (for example, 0.3), I use each pixel of the mask as the weighting factor (see figure below). For example, if the pixel in the mask at the [X, Y] position has a value of 0 (that is, black), the pixel of image 1 will be placed in that position. If one pixel of the mask remains at a value of 255 (white), the corresponding pixel of image 2 will occupy that position. If it is somewhere in between (for example, 170), image one and image two are mixed. A map made up of weight factors using a mask provides more flexibility when overlaying two images. It can produce transitions or "soft" boundaries (or more).

Use the code

I divided the code examples that you can find in the repository into four parts. Next, I only show the code for two internal parts, because they cover the main topics of this tutorial. For completeness, I will briefly comment on sections 1 and 4 below, and then provide the basic information in sections 2 and 3 in the form of key points.

In Section 1, you can find the OpenCV code used to load the desired image (that is, cv2.imread ("X.jpg", cv2.IMREAD_UNCHANGED). There are three images: image 1 is the surface of the content on which image 2 is pasted, and image 3 is the mask or template that defines the mixing operation conditions. There are some additional features, but I'll leave it to you to understand their purpose (there are informative comments in the sample code I uploaded).

In Section 4, you will find a "while loop" that allows a basic interaction with the program. The loop is waiting for a critical event. Press "Q" to exit the program, press "s" to save the results of the mixed operation and then exit the program.

Sections 2 and 3 contain code for mixing two image information according to the weighting factor provided by the mask.

The function mix_pixel (pix_1, pix_2, perc) handles the input cv2.addWeighted () in a similar manner. It is based on the function of perc in the third parameter (i.e., given weights are required from two images and blend pixel information) (for example, when perc is in the middle of 0 and 255, it gives a 50-50 mixed pixel 1 and pixel 2; see also the mixed image section). When the parameter is a NumPy array (note: all arrays need to have the same dimension), the operation will be applied to all corresponding pixels of the image. The third parameter is an array (mask) that contains a separate weighting factor for each pixel.

The blend_images_using_mask (img_orig, img_for_overlay,img_mask) function is quite simple. It takes three images, checks whether the mask is a 3-channel grayscale image (otherwise, it will not have the same dimension as other images), calls the mix_pixel () function, and returns the result as a 3-channel 8-bit unsigned integer (otherwise the OpenCV command to display the image will fail).

The final command you find in the following example is used to display different types of images so that the results of the process are visible and available for evaluation. There are also lines of code to make the image smaller because the original size of the photo is usually too large for a standard screen.

# code skeletonimport numpy as npimport cv2#. Ombudsman code# functions for blending operations# takes a pixel from image 1 (pix_1) and blends it with a pixel from image 2 (pix_2) # depending on the value given in perc (percentage); # if perc = 0 or 255 (or 0 or 255255255) it will perform no blending at all # and return the value of image 1 or image 2 # by contrast, all values in between (e.g.140) will give a weighted blend of the two images# function can be used with scalars or numpy arrays (perc will be greyscale numpy array then) def mix_pixel (pix_1, pix_2, perc): return (perc/255 * pix_1) + ((255perc) / 255* pix_2) # function for blending images depending on values given in maskdef blend_images_using_mask (img_orig, img_for_overlay Img_mask): # turn mask into 24 bit greyscale image if necessary # because mix_pixel () requires numpy arrays having the same dimension # if image is 24-bit BGR, the image has 3 dimensions, if 8 bit greyscale 2 dimensions if len (img_mask.shape)! = 3: img_mask = cv2.cvtColor (img_mask Cv2.COLOR_GRAY2BGR) # interpolate between two images (img_orig and img_to_insert) # using the values in img_mask (each pixel serves as individual weight) # as weighting factors (ranging from [0LJ 0] to [255255255] or 0 to 100 percent) # because all three images are numpy arrays standard operators # for multiplication etc. Will be applied to all values in arrays img_res = mix_pixel (img_orig, img_for_overlay, img_mask) return img_res.astype (np.uint8) # blend images and display results# call function above to perform blending with mask (containing weights for interpolation) img_blended = blend_images_using_mask (img, img_insert, img_insert_mask) # print (img_blended.shape) # rf-> resizing factor Used to make images smaller, so they can be displayed on screen # blending operations, however, will be performed on original sized imagesrf = 0.4wi = img.shape [1] # width andhi = img.shape [0] # height of images # call OpenCV resize img_sm = cv2.resize (img, (int (wi * rf), int (hi * rf)), interpolation=cv2.INTER_CUBIC) img_insert_sm = cv2.resize (img_insert, (int (wi * rf), int (hi * rf)) Interpolation=cv2.INTER_CUBIC) img_blended_sm = cv2.resize (img_blended, (int (wi * rf), int (hi * rf)), interpolation=cv2.INTER_CUBIC) # display imagescv2.imshow ("Original Image", img_sm) cv2.imshow ("Insert This Image", img_insert_sm) cv2.imshow ("Blended Images", img_blended_sm) # .ommom code so far I believe you have a deeper understanding of "how to mix images with Python and OpenCV", so you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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: 0

*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