当前位置:网站首页>Study on the basis of opencv

Study on the basis of opencv

2022-07-26 10:11:00 SingleDog_ seven

Follow these days b The teacher on the station did opencv The most basic operation of learning , Next, write down what you have learned .

( This is b Link to learn on the website , If you are interested, you can have a look at )https://www.bilibili.com/video/BV1PV411774y?p=9&share_source=copy_web

  This is my photo


 

def cv_show(name,img):
    cv2.imshow(name, img)
    cv2.waitKey(0)           #  0 Means press any key to exit     Other numbers represent how many milliseconds to wait 
    cv2.destroyAllWindows()   #    Destroy all windows 

First, define the function, and then display the image ( Don't knock again and again ) 

It's used here imshow To show. , If it is to use print Words , The values of each channel of the picture will be output with a matrix . Here's an example .

img = cv2.imread('cat.jpg', cv2.IMREAD_GRAYSCALE)    # Grey diagram   cv2.IMREAD_GRAYSCALE The second parameter indicates the reading type 
print(img)
cv_show('img2', img)

Take gray-scale image as an example , The result is :( The value range is 0--255 Between )

[[10 10 10 ... 13 13 13]
 [10 10 10 ... 13 13 13]
 [10 10 10 ... 13 13 13]
 ...
 [14  7  6 ... 16  9 22]
 [40 18  3 ... 15 30 52]
 [63 28  0 ... 15 19 47]]


Next is shape The use of , It shows the picture high , wide , And there are several color channels

img = cv2.imread('cat.jpg')
print(img.shape)    #  h,w,c( Several color channels )

This is the running result of the color chart : (296, 474, 3), This is the result of converting into a grayscale image (296, 474)


Save the picture :

# cv2.inwrite(' name ',img)     preservation 

Next is the partial interception of the image :

# Capture part of the image data 
img=cv2.imread('cat.jpg')
cat=img[0:200,0:200]       # Coordinate range 
cv_show('cat',cat)

This is the image obtained , The pixel we selected is high :0-200, wide :0-200


Operation of color channel :

The first is the extraction of color channels :

# Color channel extraction 
b,g,r=cv2.split(img)    # Separate 
print(b)


[[12 12 12 ... 11 11 11]
 [12 12 12 ... 11 11 11]
 [12 12 12 ... 11 11 11]
 ...
 [14  7  6 ...  7  2 15]
 [40 18  3 ...  6 23 45]
 [63 28  0 ...  6 12 40]]

Below the code snippet is the result B Parameters of the channel .

Then there is the combination of color channels :

img=cv2.merge((b,g,r))   # Combine 

Only images of monochrome channels are preserved :   B---0,G---1,R---2

# Only keep R
cur_img =img.copy()
cur_img[:,:,0]=0        #B---0
cur_img[:,:,1]=0        #G---1
cv_show('R',cur_img)

# Only keep B
cur_img =img.copy()
cur_img[:,:,1]=0        #G---1
cur_img[:,:,2]=0        #R---2
cv_show('B',cur_img)

# Only keep R
cur_img =img.copy()
cur_img[:,:,0]=0        #B---0
cur_img[:,:,1]=0        #G---1
cv_show('R',cur_img)

Let's show you : This is B Image of the channel


Fill the image boundary :

# Border filling 
top_size,bottom_size,left_size,right_size =(50,50,50,50)       # Specify the fill size up, down, left and right 

replicate = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REPLICATE)    # Replication 

We use the replication method here ( Copy the most edge pixel ) give an example , There are also the following methods :
#BORDER_REFLECT     Reflection , Copy the pixels in the image of interest on both sides      dcba|abcd|dcba
#BORDER_REFLECT_101   Reflection , Take the most edge pixel as the axis , symmetry           dcb|abcd|cba
#BORDER_WRAP   The outer packing method        abcd|abcd|abcd
#BORDER_CONSTANT   Constant method

All the above methods use cv2.copyMakeBorder This is for operation


We can also operate on the values in the matrix :

img_cat = cv2.imread('cat.jpg')
img_cat2 = img_cat + 10

shape If the value is the same, the two pictures can also be added , exceed 255 Words , The value obtained will be correct 256 Remainder .

cv2.add(img_cat,img_cat2)
# If you don't cross the line, take yourself , If you cross the line, take 255

This line of code is also additive , The difference is that if the number after adding is greater than 255, Its value will show 255.


  If shape The value is different , You can put their shape Values are converted to the same , And then we'll do the operation :

# Change height and width 
img_dog = cv2.resize(img_dog,(474,296))
#shape The output is height and width ,resize What is needed is width and height 

We need to pay attention here .shape The values are the output height and width ,resize What is needed is width and height .

Their width and height can also be listed in a certain ratio :

res1 = cv2.resize(img,(0,0),fx=3,fy=1)
# Height and width are in proportion 

Here are two pictures to fuse :

img_cat = cv2.imread('cat.jpg')
img_dog = cv2.imread('dog.jpg')
res = cv2.addWeighted(img_cat,0.4,img_dog,0.6,0)
# The selected photos and the weight of each photo , The last number represents how much brightness is increased 

This is the result of fusion


  Finally, I want to talk about the operation of video :

vc =cv2.VideoCapture('cat.mp4')        # Capture the video 
if vc.isOpened():         # Check whether it can be opened correctly 
    open,frame = vc.read()
else:
    open =False

while open:
    ret,frame =vc.read()
    if frame is None:
        break
    if ret == True:
        gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)   # Convert to black and white ?
        cv2.imshow('result',gray)
        if cv2.waitKey(10) & 0xFF ==27:               #0xFF==27 Means press the exit key to exit ,waitKey Indicates how long to wait to exit 
            break
vc.release()
cv2.destroyAllWindows()     # Close all windows 

if Statement to determine whether it can open , If it can't be opened , that while Directly terminate in the statement , If it can be opened , We are here to convert the video into black and white , The video will be played as we have done ,waitkey() The number in represents the number of seconds to wait before closing the video ,0xFF==27 Yes means press esc Key to exit ( According to the key ASCII Modify the code value ).


The above is the whole content of this blog , If there is anything wrong, you are welcome to correct .

 

原网站

版权声明
本文为[SingleDog_ seven]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/201/202207181750554412.html