Profile Pic

Iqbal Ahmed

I develop Android Apps along with Backend
and My only LOVE is Kotlin

OpenCV Series: 2. Get Started with OpenCV and Python

In this post, we will play with OpenCV with python.

3 minute read

This is a sample code that does a basic operation reading and writing image and We will learn more about NumPy and cv2.

import numpy as np
import cv2

#Read opencv.png logo file from local
img = cv2.imread("opencv.png") 

#Create Window
cv2.namedWindow("Logo",cv2.WINDOW_NORMAL)

#Show local opencv.png in window
cv2.imshow("Logo",img)

#Wait user press key eg: Esc
cv2.waitKey(0)

#Deep copy the opencv.png to output.png in local
cv2.imwrite("output.jpg",img)
exit()

Here we are using NumPy, which is a very popular library in AI. It’s written in C and wrapped in Python for ease of use.

What is NumPy

NumPy is the n-dimensional array, unlike the python list. At the time of creation, we fixed the size and when we change the ndarray size. It creates a new array and deletes the original.

NumPy used for advanced mathematical and scientific computing and Its a very fast. For more reading you can check this NumPy link

What is cv2

cv2 is a openCV binding for python which provide all the openCV methods or features. Its orignally written in C/C++. In above code we have used few methods like namedWindow waitKey imshow imread imwrite these methods basically calling C/C++ method.

To learn more about above function you can check OpenCV documentation

Lets play with numpy and cv2

Further, we will visualize the data structures and how to access them,

To read the image we have imported cv2 and Numpy, Lets load the image with default color value(1)

read more about image reading mode

>> import cv2
>> import numpy as np
>> img = cv2.imread("./../res/opencv-logo.png", 1)  

After reading the image we can see all three color channel (BGR) data

>> print(img)
[[[255 255 255]
  [255 255 255]
  [255 255 255]
  ...
  [255 255 255]
  [255 255 255]
  [255 255 255]]
 [[255 255 255]
  [255 255 255]
  [255 255 255]
  ...

To see the data type of the image

>> print(type(img))  
<class 'numpy.ndarray'>

as you can see all the data stored in ndarray. ndarray is an n-dimensional array

To find the number of rows in the image array

>> print(len(img))  
739

To find the number of cols in image array at 0th index

>> print(len(img[0]))  
600

To find the number of color channels

>> print(len(img[0][0]))
3

To find all the info about image color, rows, and columns

>> print(img.shape)
(739, 600, 3)

To find the data type

>> print(img.dtype)
uint8

uint8 means 2pow8(0-255) possible values in the array of Image at any index

Get pixel value at 10th row and 5th column

>> print(img[10, 5])
[255 255 255]

Get one channel of Image

print(img[:, :, 0])  # one channel image
[[255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 ...
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]
 [255 255 255 ... 255 255 255]]

Total number of pixels in Image

print(img.size)
1330200

Source code you can find here here

Say something

Comments

Nothing yet.

Recent posts

Categories

About

I develop Android Apps along with Backend
and My only LOVE is Kotlin