The basic operation of the image opencv 3

1, access and modify pixel

Reading an image, the acquired pixel value of pixels which row and column coordinates, for an RGB image, the RGB values ​​of the return, the return value for the grayscale gradation

. 1  Import CV2
 2  Import numpy
 3 IMG = cv2.imread ( ' 1.jpg ' )
 . 4 PX IMG = [100, 100 ]
 . 5  Print (PX) # acquired images (100, 100) of the 3-dimensional matrix 
. 6 Blue IMG = [100 , 100 , 0]
 . 7 gray IMG = [100, 100,. 1 ]
 . 8 Red IMG = [100, 100, 2 ]
 . 9  Print (Blue, gray, Red) # gradation value of the tri-color print FIG default the BGR 
10  for I in IMG [100, 100]: # traverse the matrix 
. 11      Print (I)
 12 is 
13 is  Print (img.item (100, 100, 2)) # quick access matrix values 
14 img.itemset ((100, 100, 2), 99) # quickly modified matrix values

Results of the:

[254 148 125]
254 148 125
254
148
125
125

 

 2, the image acquired properties

 Image attribute comprises: rows, columns, channel, type of image data, number of pixels

2.1 img.shape shape image can be obtained, the return value is the number of rows comprising a number of columns, the number of channels tuple

If the image is a grayscale image, return only a few rows and columns value, the return value may be determined by checking a grayscale or color image

1 port cv2
2 import numpy
3 img = cv2.imread('1.jpg')
4 print(img.shape)
5 img1 = cv2.imread('1.jpg', 0)
6 print(img1.shape)

 

 Results of the:

(375, 500, 3)
(375, 500)

 

2.2 img.size may return the number of pixels of the image 

. 1  Print (img.size) # number of pixels of the image can return img.size 
2 execution results: 562,500

 

 2.3 img.dtype return data type of image, it is important when Debug, because the data type of inconsistency OpenCV-Python code often

. 1  Print (img.dtype)
 2  Results:
 . 3 of unit8

 

 The image area of ​​the image of the ROI for a particular operation. ROI is obtained using numpy index.

   Simply means that the region of interest of the image, machine vision, image processing, the image is processed in block, circle, ellipse, irregular polygon, etc. outline area in need of treatment, known as region of interest ROI. Take, for example: There is a picture, there are a variety of animals on the picture, but you only like the picture of the dog, then the regional area where the dog is of interest (ROI).

 (1) the ROI rectangle, directly using a microtome, simple and crude

. 1  Import CV2
 2  
. 3 IMG = cv2.imread ( " 1.jpg " )
 . 4 ROI IMG = [50: 120, 50: 100] # front height taken, taken transversely behind 
. 5 cv2.imshow ( ' ROI ' , ROI)
 . 6  cv2.waitKey (0)
 . 7 cv2.destroyAllWindows ()

 

 (2) Some common functions of the selected region of interest

numpy.zeros () Usage See: https://www.runoob.com/numpy/numpy-array-creation.html

 

 

 

 

 

 

 

 

1

Guess you like

Origin www.cnblogs.com/weststar/p/11506300.html