+ All Categories
Home > Documents > Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2...

Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2...

Date post: 25-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
55
Transcript
Page 1: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything
Page 2: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

2

Page 3: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

Open ipython on the terminal and type:

• $ import cv2

• $ print cv2.__version__

If version displayed is equal or bigger than 3.1.0 everything is perfect.

Now, get the last version of the repository files with the following commands:

• $ cd $HOME

• $ cd opencv-feeg6003

• $ hg pull

• $ hg update3

Page 4: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

4

Page 5: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

5

Page 6: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

6

Page 7: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

7

Page 8: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

8

Page 9: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task1.py

Task: Read/Display and Write Images

• Image = cv2.imread(‘filename’,color_flag)

• cv2.imshow(‘windowname’, source)

• cv2.imwrite(‘filename’,source)

Remind: import cv2 – to access to the library (use an alias if preferred)

9

Page 10: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

10

Page 11: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

11

Page 12: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

12

Page 13: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

13

Video

Capture

If correct

Read each

frame of the

Video

True

False

Display

Frame

Get out

Loop

Release

Next

frame

Page 14: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

14

Page 15: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

15

Page 16: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

16

Video

Capture

If correct

Read each

frame of the

Video

True

False

Mod/Write

/Display-

Frame

Get out

Loop

Release

Next

frame

Page 17: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

17

Page 18: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task2.py

Task: Capture/Display and Write a video.

• video = cv2.VideoCapture(‘filename’)

• cv2.imshow(‘windowname’, source)

• videowriter.write(source) – VideoWriter object provided in the template

Remind:

18

Page 19: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

19

Page 20: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

20

Page 21: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

21

Page 22: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

22

Page 23: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

23

Page 24: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task3.py

Task: Access to matrix information and modify it

• image.shape – 3 outputs if image is in color.

• Access to an specific value of a matrix in Python - Image[row,column]

• Select a region of a matrix – Image[20:30, 50:60]

Remind: BGR type of matrix (Blue, Green and Red color)

24

Page 25: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

25

Page 26: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• Pixel position(x,y)

• Color channel(Colorspace)

Images information in OpenCV are stored as matrix:

• BGR: mixing blue, green and red

• HSV: Hue(Color), Saturation(Grayscale), Value(Brightness)

• GRAY: Black-and-white mode

Color mode: BGR, HSV, GRAY

26

Page 27: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

27

Page 28: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task4.py

Task: Convert colorspaces from BGR to GRAY

• input_image: source image

• flag: cv.COLOR_BGR2HSV or COLOR_BGR2GRAY

Remind: : cv.cvtColor(input_image,flag)

28

Page 29: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

29

Page 30: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

30

Page 31: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

31

Page 32: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task5.py

Task: Extract blue color object from the OpenCV logo

• Threshold: cv.inRange(image_input, lower_boundary, upper_boundary) - Check if every element lies in the range: set it to be 255 if it is and to be 0 if not.

• Extract: cv.bitwise_and(img, img, mask = mask)

• Get product of each element of two images bit-wisely. Here we use it to mask images, so keep the first two parameters the same.

• HSV value of color blue: [120, 255, 255]

Remind:

32

Page 33: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

33

Source

Result

Mask

Page 34: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

34

Page 35: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

35

Page 36: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task6.py

Task: scaling an image with fx = 1.5, fy =1.5

• Interpolation Methods:

• cv.INTER_AREA: for shrinking

• cv.INTER_CUBIC: for zooming (slow)

• cv.INTER_LINEAR: for zooming (default method)

Remind: cv.resize(img, None, fx, fy, interpolation method)

36

Page 37: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

37

Page 38: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

38

Page 39: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task7.py

Task: translate an image with tx = 100, ty =50

• Construct transformation matrix M

• Pass M to cv.warpAffine(img, M, area)

• area: display area with the form of (width, height)

Steps:

39

Page 40: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

40

Page 41: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

41

Page 42: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task8.py

Task: rotate an image with original center, angle 90°, scale 1

• Get transformation matrix M using cv.getRotationMatrix2D(rotation center, angle, scale)

• Pass M to cv.warpAffine(img, M, area)

Steps:

42

Page 43: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

43

Page 44: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

44

Page 45: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

45

Page 46: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

46

Page 47: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

47

Page 48: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

48

Page 49: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task9.py

Task: find and draw contours

• Get a binary image: cv.inRange(img_gray, lower_boundary, upper_boundary)

• im2, contours, hierarchy = cv.findContours(img_binary,cv.RETR_TREE,method)

• methods: cv.CHAIN_APPROX_NONE, cv.CHAIN_APPROX_SIMPLE

• cv.drawContours(img,contours,index,(color),thickness)

• Index = -1 to draw all contours, color given in BGR mode with brackets

Remind:

49

Page 50: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

50

Page 51: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

51

https://opencv.org/

Page 52: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

52

Page 53: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

• $ cd ~/tasks

• $ nano task10.py

Task: find and draw histogram of channel ‘r’ with histSize = [256] (full scale)

• channel: index of channel(given in square brackets)

• mask: mask image(‘None’ for full area)

• histSize: the size of bin( [256] for full scale)

• ranges: the value range to calculate(normally [0,256])

Remind: cv.calcHist([image],[channel],mask,[histSize],[ranges])

53

Page 54: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

54

Page 55: Open on the terminal and type - Bitbucket · Open ipython on the terminal and type: •$ import cv2 •$ print cv2.__version__ If version displayed is equal or bigger than 3.1.0 everything

55


Recommended