0% found this document useful (0 votes)
36 views

Opencv Tutorial: 1 Import

This OpenCV tutorial document demonstrates various image processing techniques using OpenCV in Python including importing and displaying an image, getting image dimensions, selecting a region of interest, resizing images, rotating images, smoothing images, and drawing shapes and text on images. Key functions covered include cv2.imread(), cv2.imshow(), cv2.resize(), cv2.warpAffine(), cv2.GaussianBlur(), cv2.rectangle(), cv2.circle(), cv2.line(), and cv2.putText().

Uploaded by

Haing p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Opencv Tutorial: 1 Import

This OpenCV tutorial document demonstrates various image processing techniques using OpenCV in Python including importing and displaying an image, getting image dimensions, selecting a region of interest, resizing images, rotating images, smoothing images, and drawing shapes and text on images. Key functions covered include cv2.imread(), cv2.imshow(), cv2.resize(), cv2.warpAffine(), cv2.GaussianBlur(), cv2.rectangle(), cv2.circle(), cv2.line(), and cv2.putText().

Uploaded by

Haing p
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

OpenCV Tutorial

March 22, 2021

1 Import
In [1]: import cv2

2 Read Image
In [17]: image = cv2.imread("jp.jpg")

3 Get width and height


In [18]: (h, w, d) = image.shape
print(f"width = {w}, height = {h}, depth = {d}")
width = 2048, height = 1316, depth = 3

4 Showing an image
In [23]: cv2.imshow("Image", image)
cv2.waitKey(10000)
cv2.destroyAllWindows()

5 BGR Colour Space


In [25]: (B, G, R) = image[100, 50]
print(f"R = {R}, G = {G}, B = {B}")
R = 209, G = 217, B = 194

6 ROI
In [29]: roi = image[100:500, 50:450]
cv2.imshow("ROI", roi)
cv2.waitKey(10000)
cv2.destroyAllWindows()

1
7 Resize
In [32]: resized = cv2.resize(image, (500, 500))
cv2.imshow("Fixed Resize", resized)
cv2.waitKey(10000)
cv2.destroyAllWindows()

In [33]: fixed_width = 500


ratio = fixed_width/w
aspect_resize = cv2.resize(image, (fixed_width, int(h*ratio)))
cv2.imshow("Aspect Resize", aspect_resize)
cv2.waitKey(10000)
cv2.destroyAllWindows()

8 Rotate
In [35]: center = (w//2, h//2)
M = cv2.getRotationMatrix2D(center, -45, 1.0)
rotated = cv2.warpAffine(image, M, (w, h))
cv2.imshow("Rotated", rotated)
cv2.waitKey(10000)
cv2.destroyAllWindows()

9 Smoothing
In [42]: blurred = cv2.GaussianBlur(image, (25, 25), 0)
cv2.imshow("Blurred", blurred)
cv2.waitKey(10000)
cv2.destroyAllWindows()

10 Drawing
In [43]: output = image.copy()
cv2.rectangle(output, (200, 300), (500, 800), (0, 0, 255), 2)
cv2.imshow("Rectangle", output)
cv2.waitKey(10000)
cv2.destroyAllWindows()

In [44]: output = image.copy()


cv2.circle(output, (200, 300), 100, (0, 255, 0), -1)
cv2.imshow("Circle", output)
cv2.waitKey(10000)
cv2.destroyAllWindows()

In [45]: output = image.copy()


cv2.line(output, (200, 300), (500, 800), (255, 0, 0), 2)
cv2.imshow("Line", output)

2
cv2.waitKey(10000)
cv2.destroyAllWindows()

In [48]: output = image.copy()


cv2.putText(output, "Hello Jurrasic Park!", (300, 300), cv2.FONT_HERSHEY_SIMPLEX, 3, (
cv2.imshow("Text", output)
cv2.waitKey(10000)
cv2.destroyAllWindows()

You might also like