![](https://crypto4nerd.com/wp-content/uploads/2023/11/1FauIVRy-Aygftv32DZXtdQ-1024x544.png)
Today’s story is about ahndling and examining images in Python with the open source library for computer vision OpenCV. Personally, I like working in Pycharm and for the goals of this article I will save an image which I have downloaded in the data folder of my current project:
Of course, you should also add the image you want to process to your IDE by a similar way. The first thing I will perform is just showing the image from my data using the following code:
import cv2 as cv
import numpy as np
#the following function will be used afterwards
def rescale(image, scale):
width = int(image.shape[1] * scale)
height = int(image.shape[0] * scale)
dimensions = (width, height)
print(dimensions)
return cv.resize(image, dimensions, interpolation=cv.INTER_AREA)
image = cv.imread('C:/Users/nikos/PycharmProjects/pythonProject/data/nature.jpg')
cv.imshow('Nature', image)
cv.waitKey(0)
So in ordeer to read an image we just pass the path to cv.imread and after setting a title to the uprising window we can see our image:
At this point we see the title we set before to characterize our image and the image I chose. Often, the dimensions of the images may not fit perfectly our window and it can result into partial representation of the image we want. At the first block of code, I have created a function which can be applied to rescale the image using the shape method which retuns many useful properties of the image, including height and width. Also, we use a scale factor which does the shrinking or, more rarely, the expanding.
rescaled = rescale(image, 0.5)
cv.imshow('Nature_Rescaled', rescaled)Now if we full-screen the above code it will be blank int the other half of the image. Since rescaling and resizing is a pretty helpful operation we can also apply the .resize method which works exactly the same, if we also pass the desirable size of the new window:
resized = cv.resize(image, (400,400))
cv.imshow('Nature_Resized', resized)
Even from our knowledge from neural networks, we have already assumed that grayscale images may come in handy since many functions work with no color channels to better identify edges or patterns. This process provides us with info about the distribution of the pixels density than their color.
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
cv.imshow('Nature_Grayscaled', gray)
Furthermore, we can understand that the above images are of high resolution and quality and this can sometimes may not be desired. At first it may seem out of logic, but in order to find patterns or contours we would like to work with less information to be more effective. I chose the Gaussian blurring but you can also see the documentation for more ways:
blur = cv.GaussianBlur(image, (3,3), cv.BORDER_DEFAULT)
cv.imshow('Nature_Blurred', blur)
#blurring with bigger ksize
blur1 = cv.GaussianBlur(image, (7,7), cv.BORDER_DEFAULT)
cv.imshow('Nature_Blurred_2', blur1)
Gradually we can grasp that the analysis of the pixels is smaller and hence the quality worsens. The biggest the tuple we assing to the function the biggest the blurring. Now, we will enter to the beautiful world of image processing with baby steps, firstly recognizing edges with the canny method. I applied it to both the initial image and the two blurred and as you should expect the edges with images descending are reduced:
canny = cv.Canny(image,125,125)
cv.imshow('Nature_Edges', canny)
canny1 = cv.Canny(blur,125,125)
cv.imshow('Nature_Edges_1', canny1)
canny2 = cv.Canny(blur1,125,125)
cv.imshow('Nature_Edges_2', canny2)
From the result we can now fully understand the importance of blurring images. Using the edges we will now detect the contour of the images and for displaying reasons I will depict the contours to the initial and the second version of the blurred images:
contours, hierarchiies = cv.findContours(canny,cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
contours1, hierarchiies = cv.findContours(canny2,cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
blank = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)
blank1 = np.zeros((image.shape[0], image.shape[1], 3), np.uint8)
cv.drawContours(blank, contours, -1, (0,255,0), 1)
cv.imshow('Contours On the Initial Image ', blank)
cv.drawContours(blank1, contours1, -1, (0,0,255), 1)
cv.imshow('Contours On The Blurred Image', blank1)
The contours are better depicted in the blurring image but the analysis of the image is higher with more edges. I hope I gave you a good appetizer for the world of the computer vision and feel free to ask whatever you want!