Creating an image, Swapping objects in two Image, Creating a photo collage in Python using OpenCV and numpy modules

Creating an image, Swapping objects in two Image, Creating a photo collage in Python using OpenCV and numpy modules

Creating an image:


We know that images are a 3-dimensional array. We can create a 3-dimensional array on our own and change data in this way we can create an image of our wish. Here I am going to create the following image.

output.png

Firstly to create the image we need to have a background hence we need to start making the image by creating the background first. I have created the background using the following code

image=np.zeros((500,500,3),dtype='uint8')

but this will just create a black background which is simple. Now we need something more as a background hence I used the following code to make a clean looking background

for i in range(250,0,-1):
    k=(i+3)*2-2
    image[250-i:250+i,250-i:250+i]=[i,i,i]

this will create a background looking as follows

output1.png

Now that we have our background set we can work with the foreground of the image if we see clearly the foreground we have a simple flower which is made of 5 cirlcles now we need add those circles on the background which we created in the earlier steps.

We know that equation of a circle is $$ x^2 + y^2 = r^2 $$ now we can create a circle based on this if we know one of x or y we can find the other. Suppose if we know x then $$ y = \sqrt {r^2 - x^2} $$

With this knowledge of math I have firstly drawn the yellow circles with the following code

r=50
for i in range(-r,r+1):
    d=round((2*r*abs(r-abs(i))-(r-abs(i))**2)**0.5)
    image[250+r+i,250+r-d:250+r+d]=[0,200,200]
    image[250-r+i,250-r-d:250-r+d]=[0,200,200]
    image[250+r+i,250-r-d:250-r+d]=[0,200,200]
    image[250-r+i,250+r-d:250+r+d]=[0,200,200]

This changed the output to the following

output2.png

Now to create the brown circle I have use the following code

for i in range(-r,r+1):
    d=round((2*r*abs(r-abs(i))-(r-abs(i))**2)**0.5)
    image[250+i,250-d:250+d]=[42,42,165]

This further changed the output to the following

output3.png

Now having the flower we need to create the 4 quarter cicles in the four corners of the image to achieve that I have used the following code

r=3*r
for i in range(-r,r+1):
    d=round((2*r*abs(r-abs(i))-(r-abs(i))**2)**0.5)
    image[i,0:d]=[200,100,100]
    image[-i,500-d:500]=[200,100,100]

And the final output is as follows

image.png

Swapping Objects in two images:


Firstly we need to have two images to swap someobject present in them. I have used the following two images.

cricket.jpg

cricket.jpg

football.jpeg

football.jpeg

My final goal is to swap the balls in these two images. The final output looks as follows:

output1 output1_fball.jpg output2

output2_cball.jpg

To do this I have done the following:

  • Firstly I cropped the objects in the two images. I have used the following code to do that

    image1 = cv2.imread('./cricket.jpg')
    image2 = cv2.imread('./football.jpeg')
    cricketball=image1[200:730,440:950]
    cballshape=cricketball.shape[-2::-1]
    

    As we need the shape of the ball too hence I have saved it to another variable The cropped cricket ball is as follows: cball.jpg Similarly I have also cropped the football using the following code

    football=image2[465:790,535:865]
    fballshape=football.shape[-2::-1]
    

    fball.jpg

  • Now that we have the objects we need to change the space according to the space available in the oriminal image hence to do that I used the cv2.resize method and resized the objects according to the requirement

    Following is the code I used to do resizing

    cricketball=cv2.resize(cricketball,fballshape)
    football=cv2.resize(football,cballshape)
    
  • Now that we have the objects in the required shape and size we can swap them

    To swap them I have used the following code:

    image1[200:730,440:950]=football
    image2[465:790,535:865]=cricketball
    

The final output after swapping is as follows:

image.png

Creating a photo collage:


In photo college we can place multiple images side by side or one after another.

To create create the photo collage I have used these two pictures

cricket.jpg

cricket.jpg

football.jpeg

football.jpeg

The final output should look as follows:

ROWWISE:

output_collage1.jpg

COLUMNWISE:

output_collage2.jpg

To do this I have create a function which adds padding to each image such that the images provided to it can be concatenated to form a photo collage. I have use the concatenate method of the numpy module to finally combine them into a single image. As we know that concatenation can be done in two directions hence we will have two types of collages. Following is the function I have defined to create a collage for the given two images

def photocollage(image1,image2,axis=0):
    if axis==0:
        cols=image1.shape[1]+50 if image1.shape[1]>image2.shape[1] else image2.shape[1]+50
        first = np.zeros((image1.shape[0]+50, cols, 3),dtype='uint8')+255
        extra=0
        if image1.shape[1]%2!=0:
            extra=1
        first[25:image1.shape[0]+25, cols//2-image1.shape[1]//2:cols//2+image1.shape[1]//2+extra]=image1
        second = np.zeros((image2.shape[0]+50,cols,3),dtype='uint8')+255
        extra=0
        if image2.shape[1]%2!=0:
            extra=1
        second[25:image2.shape[0]+25,cols//2-image2.shape[1]//2:cols//2+image2.shape[1]//2+extra]=image2
        return np.concatenate((first,second))
    if axis==1:
        rows=image1.shape[0]+50 if image1.shape[0]>image2.shape[0] else image2.shape[0]+50
        first = np.zeros((rows, image1.shape[1]+50, 3),dtype='uint8')+255
        extra=0
        if image1.shape[0]%2!=0:
            extra=1

        first[rows//2-image1.shape[0]//2:rows//2+image1.shape[0]//2+extra,25:image1.shape[1]+25]=image1
        second = np.zeros((rows,image2.shape[1]+50,3),dtype='uint8')+255
        extra=0
        if image2.shape[0]%2!=0:
            extra=1
        second[rows//2-image2.shape[0]//2:rows//2+image2.shape[0]//2+extra,25:image2.shape[1]+25]=image2
        return np.concatenate((first,second),axis=1)

And finally I have called the function and with two images and got the output. Following is the final output:

image.png

You can find the code for the same here