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:
- Press and hold the WIN+R key on the keyboard to open the operation box
- Enter cmd and click OK to open a small black box
- Enter PIP install opencv Python - i https://pypi.tuna.tsinghua.edu.cn/simple/ (Tsinghua source used in the following - i + link, with faster download speed)
- If you are asked yes/no, just enter y. The installation will be completed in a short time
- Aside: in fact, not only opencv python, many Python packages can be installed in this way, such as pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple/ 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: https://pan.baidu.com/s/1TVIOEPh4wkEIbVHANLX5_A
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 cv.namedWindow("img",cv.WINDOW_NORMAL) #Allows the window to zoom in and out freely cv.imshow("img",img) #Show pictures cv.waitKey(0) #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. cv.destroyAllWindows() #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") plt.imshow(img) plt.show()
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.imshow(pic) plt.xticks([]) #Remove the x-axis plt.yticks([]) #Remove the y-axis plt.show()
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) cv.line(img,(50,50),(300,350),(255,255,0),3) # cv.circle(img,(250,250),90,(0,0,255),3) #Hollow circle cv.circle(img,(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 cv.imshow('pic',img) cv.waitKey(0) cv.destroyAllWindows()
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 cv.circle(img,(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 while(1): cv.imshow("image",img) if(cv.waitKey(1)&0xFF == ord('q')): #Enter the q key to end the interaction break # cv.waitKey(0) # cv.imshow("image",img) cv.destroyAllWindows()
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 drawing=False mode=True ix,iy=-1,-1 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 drawing=True 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 else: cv2.circle(img,(x,y),3,(0,0,255),-1) #Writing consists of a series of dots elif event==cv2.EVENT_LBUTTONUP: drawing=False img=np.ones((600,600,3),np.uint8)*50 cv2.namedWindow('image') cv2.setMouseCallback('image',call_back_draw) while True: cv2.imshow('image',img) if cv2.waitKey(1)==ord('m'): #Replacement mode mode=not mode elif cv2.waitKey(1)==ord('q'): #Exit painting break cv2.destroyAllWindows()
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 #cv.createTrackbar() #cv.getTrackbarPos() 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") sys.exit() cv.namedWindow("Brighter",cv.WINDOW_NORMAL) 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: cv.imshow("Brighter",img1) if cv.waitKey(2) == 27: #Press Esc to exit break cv.destroyAllWindows()
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: cv2.imshow('tracebar',img) if cv2.waitKey(1)==ord('q'): break cv2.destroyAllWindows()