Day 15 - Get Started With OpenCV


Hello guys,
This is Day 15 of my #100DaysOfMLCode challenge and sorry for the late post, I am busy in the office work. So today, I learned the OpenCv modules basics.

Python Notebook-






Getting Started

In [1]:
import numpy as np
import cv2
%matplotlib inline
import matplotlib.pyplot as plt

print("OpenCV Version %s" %cv2.__version__)











OpenCV Version 3.4.0


Read and Display an image

In [5]:
img = cv2.imread('images/messi.jpg', cv2.IMREAD_COLOR)

# If the image path is wrong, the resulting img will be none
if img is None:
    print ('Open Error')
else:
    print ('Image Loaded')











Image Loaded


In [7]:
print(type(img))
print(img.shape)
print(img.dtype)
print(img[:2,:2, :])











<class 'numpy.ndarray'>
(342, 548, 3)
uint8
[[[39 43 44]
  [42 46 47]]

 [[37 41 42]
  [42 45 49]]]


In [8]:
# display an image using matplotlib
# plt.imshow(img) # => The color of this line is wrong
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))








Out[8]:





<matplotlib.image.AxesImage at 0x1e5e35fe550>













Basic operations on Images

1. Draw an object

  • cv2.line(image, startPoint, endPoint, rgb, thinkness)
  • cv2.rectangle(image, topLeft, bottomRight, rgb, thinkness)
  • cv2.circle(image, center, radius, rgb, thinkness)
  • cv2.ellipse(image, center, axes, angle, startAngle, endAngle, rgb, thinkness)

In [9]:
# Create a black image
img2 = np.zeros((512,512,3), np.uint8)
plt.imshow(img2)








Out[9]:





<matplotlib.image.AxesImage at 0x1e5e6334e80>













In [10]:
# Draw a line using cv2.line(image, startPoint, endPoint, rgb, thinkness)
cv2.line(img2, (0,0), (511,511), (255,0,0), 5)
# => Diagonal red line with thickness of 5 px

# Draw a rectangle using cv2.rectangle(image, topLeft, bottomRight, rgb, thinkness)
cv2.rectangle(img2, (384,0), (510,128), (0,255,0), 3)
# => Green rectangle with thickness of 3 px

# Draw a circle using cv2.circle(image, center, radius, rgb, thinkness)
cv2.circle(img2, (447,63), 63, (0,0,255), -1)
# => Blue filled circle(note that the thinkness is -1)

# Draw a ellipse using cv2.ellipse(image, center, axes, angle, startAngle, endAngle, rgb, thinkness)
cv2.ellipse(img2, (256,256), (100,50), -45, 0, 180, (255,0,0), -1)
# => Red wide down-half ellipse

plt.imshow(img2)








Out[10]:





<matplotlib.image.AxesImage at 0x1e5e63d65f8>













In [11]:
# Draw a line using cv2.polylines(image, points, isClosed, rgb, thinkness, lineType, shift)
pts = np.array([[10,10],[150,200],[300,150],[200,50]], np.int32)
pts = pts.reshape((-1,1,2))
cv2.polylines(img2,[pts],True,(0,255,255),3)
# => Cyan closed quadrangle 

print (pts)
plt.imshow(img2)











[[[ 10  10]]

 [[150 200]]

 [[300 150]]

 [[200  50]]]




Out[11]:





<matplotlib.image.AxesImage at 0x1e5e64594e0>













In [12]:
# Put some text using cv2.putText(image, text, bottomLeft, fontType, fontScale, rgb, thinkness, lineType)
font = cv2.FONT_HERSHEY_COMPLEX
cv2.putText(img2, 'OpenCV', (10,500), font, 4, (255,255,255), 10, cv2.LINE_AA)
# => White 'OpenCV' text at the bottom

plt.imshow(img2)








Out[12]:





<matplotlib.image.AxesImage at 0x1e5e74b39e8>














2. Modify pixels & ROI

  • You can access/modify a single pixel or ROI using Numpy indexing.
  • Just like matrix indexing, img[a, b] refer to a-th row and b-th column.

In [13]:
# Access a pixel value(BGR order)
img[50, 235]








Out[13]:





array([27, 25, 24], dtype=uint8)



In [14]:
# Change pixel values
for i in range(5):
    for j in range(5):
        img[50+i, 235+j] = (0, 255, 0)
# => Green dot above messi's head

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))








Out[14]:





<matplotlib.image.AxesImage at 0x1e5e75462e8>













In [15]:
# ROI is obtained using Numpy indexing 
ball = img[280:340, 330:390]
img[273:333, 100:160] = ball

# img[50:55, 235:240] = (0, 255, 0)  # The for-loop in the code block above is equavalent to this line. 

plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))








Out[15]:





<matplotlib.image.AxesImage at 0x1e5e75d5400>












Thanks.
Next Post Previous Post
No Comment
Add Comment
comment url