OpenCV in Python. Part 1

Hello, Habr! I am starting a series of articles on the OpenCV library in Python. Who cares, welcome under the cut!



my_logo



Introduction



OpenCV is an open source computer vision library for image analysis, classification and processing. It is widely used in languages ​​such as C, C ++, Python and Java.



Installation



We will assume that you have already installed Python and the OpenCV library, if not, then here are the instructions for installing python on windows and ubuntu , installing OpenCV on windows and ubuntu .





, . . — . , , ( 0, 0 ) . , , 400x300 . , 400 300 . 400*300 = 120000 .



: RGB. 0 255, 0 , 255 . 0 255 , 0 , 255 :



4850884 91136851 P7DI0Ak0 greyscalesteps0255



RGB(red, green, blue — , , ), , . 0 255 , «» . , [0,255], , , 8- . (, , ). , , 255: (255, 255, 255). , , 0: (0, 0, 0). , RGB :

Screenshot from 2020-08-31 01-29-26



OpenCV



. , — . , — :



import cv2


:



from cv2 import cv2


,



def loading_displaying_saving():
    img = cv2.imread('girl.jpg', cv2.IMREAD_GRAYSCALE)
    cv2.imshow('girl', img)
    cv2.waitKey(0)
    cv2.imwrite('graygirl.jpg', img)


cv2.imread(), , , , , . RGB — cv2.IMREAD_COLOR, — cv2.IMREAD_GRAYSCALE. cv2.IMREAD_COLOR. 2D ( ) 3D ( ) NumPy. : x x 3, 3 — , . : x .



cv2.imshow() . , , , , cv2.waitKey(), . , . , 0. , RGB:



concatenate_two_girl



, , cv2.imwrite() jpg( :png, tiff,jpeg,bmp . ., ), , , .





, , shape:



print(":"+str(img.shape[0]))
print(":" + str(img.shape[1]))
print(" :" + str(img.shape[2]))


, img.shape[2] , 2D .



, x y , . , OpenCV RGB , , , OpenCV , :



(b, g, r) = img[0, 0]
print(": {}, : {}, : {}".format(r, g, b))


C , (0,0). , , . , b, g r. . , , :



img[0, 0] = (255, 0, 0)
(b, g, r) = img[0, 0]
 print(": {}, : {}, : {}".format(r, g, b))


In the first line, we set the pixel value (0, 0) to (255, 0, 0), then we again take the value of this pixel and display it on the screen, as a result, the following was displayed on my console:



: 251, : 43, : 65
: 0, : 0, : 255


This is the end of the first part. If suddenly someone needs the source code and a picture, then here is a link to github . Thank you all for your attention!




All Articles