Image Processing using python

kundan singh
5 min readJun 4, 2021

--

📌Task 1:-

Create image by yourself Using Python Code

Before processing to this task we should know what is image processing and what is image .

what is image

An image is nothing more than a two dimensional signal. It is defined by the mathematical function f(x,y) where x and y are the two co-ordinates horizontally and vertically.

The value of f(x,y) at any point is gives the pixel value at that point of an image.

Image is nothing but a two dimensional array of numbers ranging between 0 and 255.

What is image processing?

Image processing is a method to perform some operations on an image, in order to get an enhanced image or to extract some useful information from it. It is a type of signal processing in which input is an image and output may be image or characteristics/features associated with that image. Nowadays, image processing is among rapidly growing technologies. It forms core research area within engineering and computer science disciplines too.

Image processing basically includes the following three steps:

  • Importing the image via image acquisition tools;
  • Analysing and manipulating the image;
  • Output in which result can be altered image or report that is based on image analysis.

To create a image we need to import the cv2 library

OpenCV is a huge open-source library for computer vision, machine learning, and image processing. OpenCV supports a wide variety of programming languages like Python, C++, Java, etc. It can process images and videos to identify objects, faces, or even the handwriting of a human. When it is integrated with various libraries, such as Numpy which is a highly optimized library for numerical operations, then the number of weapons increases in your Arsenal i.e whatever operations one can do in Numpy can be combined with OpenCV.

import cv2

Now we will creating a white image of 1000*1000 pixels using below command

img = np.zeros((1000,1000,3),np.uint8)

Here uint8 is an unsigned 8-bit integer that can represent values 0.. 255. int on the other hand is usually a 32-bit signed integer. #When you create array using dtype=int, each element in that array takes 4 bytes. OpenCV apparently expect array to be made of 8-#bit tuples representing red, green and blue.

now we will convert an image from one color space to another. by using
cv2.cvtColor() method.

Now we fill colors in the array according to our needs.

hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV)img[0:600,0:1000]=[235,206,135]      #sky
img[600:1000,0:1000]=[0,255,0] #land

img[200:600,350:650]=[0,0,255] #building

img[220:270,370:430]=[255,255,255] #windows row1
img[220:270,470:530]=[255,255,255]
img[220:270,570:630]=[255,255,255]

img[300:350,370:430]=[255,255,255] #windows row2
img[300:350,470:530]=[255,255,255]
img[300:350,570:630]=[255,255,255]

img[380:430,370:430]=[255,255,255] #windows row3
img[380:430,470:530]=[255,255,255]
img[380:430,570:630]=[255,255,255]

img[460:510,370:430]=[255,255,255] #windows row3
img[460:510,470:530]=[255,255,255]
img[460:510,570:630]=[255,255,255]


img[540:590,370:430]=[255,255,255]
img[530:596,470:530]=[255,255,255]
img[540:590,570:630]=[255,255,255]
img = cv2.circle(img,(100,100),60,(0,255,255),-1)

After filling the all colors we will get the image like

📌 Task 2 :-

Take 2 image crop some part of both image and swap it.

we import two images using command

photo1=cv2.imread('a.jpeg')

cv2.imread() method loads an image from the specified file. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix.

These are two photos which we imported

Syntax: cv2.imread(path, flag)

Parameters:
path: A string representing the path of the image to be read.
flag: It specifies the way in which image should be read. It’s default value is cv2.IMREAD_COLOR

Return Value: This method returns an image that is loaded from the specified file.

Now for crop image we use NumPy array slicing method with opencv

cropped_image= image[startY:endY, startX:endX]

a_crop=photo1[20:220,100:300]

startY:endY:-slice provides our rows (since the y-axis is our number of rows) startX:endX:-provides our columns (since the x-axis is the number of columns) in the image. Take a second now to convince yourself that the above statement is true.

After crop the image we store cropped image in one variable and swap this image with another image. here we have simple method for swapping image

#SWAPPING CROPPED IMAGE 1 WITH IMAGE 2
photo2[20:220,100:300]=a_crop

Same we crop image 2 and swap in image 1. after that we get result image like

📌Task-3:-

Take 2 image and combine it to form single image. For example collage

We already discussed about how to import images so import two photos.

Now we proceed to combine that both images

numpy.concatenate() function concatenate a sequence of arrays along an existing axis. we use axis=1 for concatenate because we need to image combine one after the another.

Syntax : numpy.concatenate((arr1, arr2, …), axis=0, out=None)
Parameters :
arr1, arr2, … : [sequence of array_like] The arrays must have the same shape, except in the dimension corresponding to axis.
axis : [int, optional] The axis along which the arrays will be joined. If axis is None, arrays are flattened before use. Default is 0.
out : [ndarray, optional] If provided, the destination to place the result. The shape must be correct, matching that of what concatenate would have returned if no out argument were specified.
Return : [ndarray] The concatenated array.

# Creating Collage

photo_collage=np.concatenate((photo1,photo2),axis=1)

After combining image-1 and image-2 then final image is shown below.

--

--

No responses yet