Machine vision - Fundamentals of case analysis (basic case drawing and display)

Machine vision - Fundamentals of case analysis (I) (basic case drawing and display)

1, Theoretical analysis

We mainly use opencv Python to process various images. It is a development library of computer vision and machine learning, which also integrates a large number of API s. It is also widely used in image processing. We focus on processing various images according to this library.

2, Code analysis

First, we need to install an opencv package, which is also very simple. The steps are as follows:

  1. Press and hold the WIN+R key on the keyboard to open the operation box
  2. Enter cmd and click OK to open a small black box
  3. Enter PIP install opencv Python - i (Tsinghua source used in the following - i + link, with faster download speed)
  4. If you are asked yes/no, just enter y. The installation will be completed in a short time
  5. Aside: in fact, not only opencv python, many Python packages can be installed in this way, such as pip install numpy -i wait.

After installation, we open pycharm. The environment configuration is relatively simple and wordy. I won't repeat it here. If you have a problem, you can baidu yourself. Please pay attention to every step carefully. Don't learn AL. Just getting started, you give up from the installation. Ha ha.

Enter the code impor cv2 and run it. No error is reported. Congratulations, the installation is successful. Ha ha. Next, the little programmer will attach a group of pictures for case display and a link for everyone to eat. Link:
Extraction code: cjfe.

2.1 presentation picture basis

With the installation package and pictures, the following is of course a pleasant display picture. Chong Chong Chong! First of all, it is recommended to create a new project in pycharm, where we can store the code we need, and put our picture files in the same directory of the code, so that the writing, reading and writing path is very convenient.

from cv2 import cv2 as cv #In this way, there will be code prompts when writing code
import numpy as np 
img = cv.imread(r"CV-Pictures/015.jpg") 
#To read the picture path, the reader can also write the absolute path. As the name suggests, it is to find the path of the picture and paste it in. In case of an external one, you can add r before the path.
#img = cv.imread(r"CV-Pictures/015.jpg",0)
#Read a single channel and read a gray image 
#Allows the window to zoom in and out freely
#Show pictures
#Wait for a signal to enter the keyboard. Keep showing the picture. If you enter a number n, you can keep it for n milliseconds.
#You can call destroyWindow() or destroyAllWindows() to close the window and deallocate any related memory usage. For a simple program, you don't actually need to call these functions, because the operating system will automatically close all resources and windows of the application when you exit

The running results are shown in the figure, which is also the simplest image display.

We can read imread into the picture and return the returned object img. You will see that it is a three-dimensional matrix. The type of input image is type < class' numpy.ndarray '>, and the shape is (1035, 690, 3)
Then, when you see the imshow function above, you will not think of the plt show function, but there are differences here.

import matplotlib.pyplot as plt
img = plt.imread("CV-Pictures/015.jpg")

Code run results

We try to use the same method of the two libraries to read images. The code is as follows

from cv2 import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
img0 = plt.imread(r'CV-Pictures\003.jpg')
img1 = cv.imread(r'CV-Pictures\003.jpg')
pic = np.hstack([img0,img1])
plt.xticks([]) #Remove the x-axis
plt.yticks([]) #Remove the y-axis

Code run results

Here we can see that the shapes of the pictures are compatible, but the colors are different. The reason is that plt reads the file in RGB format, that is, green, red and blue three channels. opencv reads the file in BGR format. The displayed pictures are different.

2.2 drawing picture Foundation

from cv2 import cv2 as cv
import numpy as np
img = np.zeros((500,500,3),dtype=np.uint8)
#,(250,250),90,(0,0,255),3) #Hollow circle,(250,250),90,(0,0,255),-1) #Fill circle
cv.rectangle(img,(10,400),(100,480),(255,0,0),3) #rectangle
# cv.ellipse(img,(300,400),(150,80),0,0,360,(0,255,255),3)#ellipse
# cv.ellipse(img,(300,400),(150,80),50,0,360,(0,255,255),3) #Rotate clockwise by 50
# cv.ellipse(img,(300,400),(150,80),-30,0,360,(0,255,255),3)#Counterclockwise rotation 30
# cv.ellipse(img,(300,400),(150,80),-30,60,300,(0,255,255),-1)#Show partial ellipse
cv.putText(img,'Deep Learning',[10,380],cv.FONT_ITALIC,1.5,(0,0,255)) #FONT_ITALIC stands for font

Code run result:

2.3 fundamentals of interactive drawing

2.3.1 double click to draw a circle in the window

from cv2 import cv2 as cv
import numpy as np
def draw_circle(event,x,y,flags,param): #Circle drawing function
    if event == cv.EVENT_LBUTTONDBLCLK: #Entering an event is equal to double clicking the left mouse button,(x,y),60,(0,0,255),-1) #Draw a circle centered on the click point

img = np.zeros((500,500,3),dtype=np.uint8) #Constructing the blackboard of painting
cv.namedWindow('image') #Name the window
cv.setMouseCallback('image',draw_circle) #Import mouse case function
    if(cv.waitKey(1)&0xFF == ord('q')): #Enter the q key to end the interaction
# cv.waitKey(0)
# cv.imshow("image",img)

The operation results are shown in the figure

2.3.2 drawing and writing in the window

from cv2 import cv2
import numpy as np
def call_back_draw(event,x,y,flags,param):
    global drawing,mode,ix,iy
    if event==cv2.EVENT_LBUTTONDOWN:#Press the left mouse button to start drawing
        ix,iy=x,y #Drawing for reading key coordinates
    elif event==cv2.EVENT_MOUSEMOVE and flags==cv2.EVENT_FLAG_LBUTTON:
        if drawing==True:
            if mode==True: 
                cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1) #Draw a square
      ,(x,y),3,(0,0,255),-1) #Writing consists of a series of dots
    elif event==cv2.EVENT_LBUTTONUP:


while True:
    if cv2.waitKey(1)==ord('m'): #Replacement mode
        mode=not mode
    elif cv2.waitKey(1)==ord('q'): #Exit painting

The code result is shown in the figure:

2.3.3 adjust the slider to change the brightness of the picture

from cv2 import cv2 as cv
import numpy as np
import sys

def call_back_brightness(param):
    global value,img,img1 #Define global variables
    value = cv.getTrackbarPos('brightness','Brighter') #Get value from slider
    img1 = np.uint8(np.clip((value/100*img),10,255)) #Limit the value to 10 to 255

img = cv.imread(r"C:\Users\my magicbook\Desktop\CV-Pictures\006.jpg")
img1 = img.copy()
if img is None: #If the picture is not found
    print("Failed to read the lena.jpg")
value = 80 #Initial value
cv.createTrackbar("brightness","Brighter",value,255,call_back_brightness) #Create a moving slider with a maximum value of 255

while True:
    if cv.waitKey(2) == 27: #Press Esc to exit

The operation results are shown in the figure:

2.3.4 adjust the slider to generate BGR color

import cv2
import numpy as np
img=np.zeros((150,400,3),np.uint8)  #Sketchpad size
def doChange(param):
    b = cv2.getTrackbarPos('B','tracebar') #Gets the color shown by the b slider
    g = cv2.getTrackbarPos('G', 'tracebar') #Gets the color shown by the g slider
    r = cv2.getTrackbarPos('R', 'tracebar') #Gets the color shown by the r slider
    img[:]=[b,g,r] #Integrated into img Sketchpad
cv2.namedWindow('tracebar') #Renaming a Window 
cv2.createTrackbar('B','tracebar',0,255,doChange) #Create a B slider, initialize it to 0, and the maximum value is 255. Change the slider position by changing doChange
cv2.createTrackbar('G','tracebar',0,255,doChange) #Create a G slider, initialize it to 0, and the maximum value is 255. Change the slider position by changing doChange
cv2.createTrackbar('R','tracebar',0,255,doChange) #Create an R slider, initialize it to 0, and the maximum value is 255. Change the slider position by changing doChange

while True:
    if cv2.waitKey(1)==ord('q'):

Tags: Python OpenCV AI Computer Vision machine vision

Posted on Sun, 28 Nov 2021 13:42:57 -0500 by alivec