Note from author :

_This tutorial is the foundation of computer vision delivered as “Lesson 2” of the series, there are more Lessons upcoming which would talk to the extend of building your own deep learning based computer vision projects. You can find the _complete syllabus and table of content here

Target Audience_ : Final year College Students, New to Data Science Career, IT employees who wants to switch to data science Career ._

Takeaway_ : Main takeaway from this article :_

  1. Loading an Image from Disk
  2. Obtaining the ‘Height’, ‘Width’ and ‘Depth’ of Image
  3. Finding R,G,B components of the Image
  4. Drawing using OpenCV

Loading an Image from Disk:

Before we perform any operations or manipulations of an image, it is important for us to load an image of our choice to the disk. We will perform this activity using OpenCV. There are two ways we can perform this loading operations. One way is to load the image by simply passing the image path and image file to the OpenCV’s “imread” function. The other way is to pass the image through a command line argument using python module argparse.

Image for post

Fig 2.1 Loading an Image from Disk by hard coding the image path and name in code

#Loading Image from disk

import cv2

image = cv2.imread(“C:/Sample_program/example.jpg”)

cv2.imshow(‘Image’, image)

cv2.waitKey(0)

Let’s create a file name Loading_image_from_disk.py in a notepad++. First we import our OpenCV library and contains our image processing functions. we import the library using the first line of code as cv2. The second line of code is where we read our image using cv2.imread function in OpenCV and we pass on the path of image as parameter, the path should also contain the file name with its image format extension .jpg , .jpeg , .png or .tiff .

**syntax **— // image=cv2.imread(“path/to/your/image.jpg”) //

Absolute care has to be taken while specifying the file extension name. we are likely to receive the below error if we provide the wrong extension name

ERROR :

c:\Sample_program>python Loading_image_from_disk.py

Traceback (most recent call last):

File “Loading_image_from_disk.py”, line 4, in

cv2.imshow(‘Image’, image)

cv2.error: OpenCV(4.3.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function ‘cv::imshow’

The third line of code is where we actually display our image loaded. First parameter is a string, or the “name” of our window. The second parameter is the object to which image was loaded.

Finally, a call to cv2.waitKey pauses the execution of the script until we press a key on our keyboard. Using a parameter of “0” indicates that any keypress will un-pause the execution. Please feel free to run you program without having the last line of code in your program to see the difference.

Image for post

#python #machine-learning #artificial-intelligence #computer-vision #opencv

Image Basics Using OpenCV — Lesson 2 of Computer Vision Tutorial
15.75 GEEK