Beginner's notes on Tensorflow opencv

OpenCV expansion training sample If you want ...
OpenCV expansion training sample

OpenCV expansion training sample

If you want to train an object detector from scratch and ensure the correctness, you need a lot of samples. If you don't want to use others' encapsulated samples, you need to make positive and negative samples to take pictures and delineate the area. This process is a bit too boring, so you want to use OpenCV to carry out simple graphic transformation and get multiple training samples in a short time. Here are some simple transformation methods and sample code.

Tailoring

Here is a simple random clipping code

import cv2 import cv2 import random img = cv2.imread("2.jpg") width,height,depth = img.shape img_width_box = int(width * 0.2) #Tailoring size img_height_box = int(height * 0.2) for i in range(9): #9 clipping X = int(random.uniform(0, width * 0.8)) Y = int(random.uniform(0, height * 0.8)) copyImg = img[X:X+img_width_box,Y:Y+img_height_box] cv2.imshow("test"+str(i),copyImg) cv2.waitKey(0)
rotate

If you do not want to change the size of training samples, you can expand the database file by random rotation transformation.

import cv2 img = cv2.imread("2.jpg") rows,cols,depth = img.shape #Using the getRotationMatrix2D function, the first parameter is the rotation center, the second is the counterclockwise rotation angle, and the third is the scaling factor img_change = cv2.getRotationMatrix2D((cols/2,rows/2),45,1) #Image recompression and reality using warpAffine function res = cv2.warpAffine(img,img_change,(rows,cols)) cv2.imshow("test",res) cv2.waitKey(0)

But the rotation of this code will cause incomplete display of the image

Random transformation of image color

Change the hue, saturation and brightness of the image to enlarge the sample

import cv2 import numpy as np img = cv2.imread("2.jpg") img_hsv = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) turn_green_hsv = img_hsv.copy() turn_green_hsv[:,:,0] = (turn_green_hsv[:,:,0] + np.random.random())%180 turn_green_hsv[:,:,1] = (turn_green_hsv[:,:,1] + np.random.random())%180 turn_green_hsv[:,:,2] = (turn_green_hsv[:,:,2] + np.random.random())%180 turn_green_img = cv2.cvtColor(turn_green_hsv,cv2.COLOR_HSV2BGR) cv2.imshow("test",turn_green_img) cv2.waitKey(0)

In order to add some noise to the image, the transformed image can also be Gamma transformed

Mouse monitoring

Finally, OpenCV is used to monitor the mouse so as to mark the target position easily. Of course, labelimg can also be used to process the training image

This is a code that uses the mouse to circle the target object (drawing has no drag effect, if you need to get a good drawing experience, you can use CV2 "event" flag "lbutton (drag with the left key to achieve)

import cv2 rect_start = (0,0) rect_end = (0,0) #Initialize box area diagonal vertex coordinates def on_mouse(event,x,y,flags,param): global rect_start global rect_end if event == cv2.EVENT_LBUTTONDOWN: rect_start = (x,y) elif event == cv2.EVENT_LBUTTONUP: rect_end = (x,y) cv2.rectangle(img,rect_start,rect_end,(0,225,0),2) img = cv2.imread("2.jpg") cv2.namedWindow("test") cv2.setMouseCallback("test",on_mouse) while(True): cv2.imshow("test",img) if cv2.waitKey(1) & 0xFF == ord('q'): break

This is the end of OpenCV learning. If there is any need later, continue to study

weixin_43874764 Published 2 original articles, praised 0 and visited 11 Private letter follow

3 February 2020, 09:54 | Views: 6311

Add new comment

For adding a comment, please log in
or create account

0 comments