In the past few days, I have been busy sorting out my data sets, and I have taken a lot of high-definition photos with industrial-grade cameras, but I need to use images with a size of 512*512 pixels during model training, and my model design has already been specified. training sample size.
Then divide it, divide the photos taken into small pieces by 512*512, then label them, and feed the model for training.
1. Segment a single image according to specified requirements
Idea: firstly specify the location of the picture picture_path
, and then specify the location to be saved picture_save_path
. picture_name
In order to save the name of the segmented image later, name it according to 原名称_数字
the name, for example, the original name a1.TIF
, the name stored after segmentation is a1_1_1.jpg
, and a1_1_2.jpg
so on.
target_width、target_high、target_channel
Set the number of width and height channels that need to be divided. The data set here is colored and requires a data set of 512*512 pixels, so it is set totarget_width = 512
target_high = 512
target_channel = 3
Then read the picture picture = cv2.imread(picture_path)
and look at the information of the width and height channels of the picture print(picture.shape)
. Here I am (4024, 6048, 3)
. This is the imread() method of OpenCV. The returned result type [H,W,C]
is different from Pytorch. Family members need to pay attention and
then put the H, W, C information to take out
Because it needs to be segmented piece by piece, it’s actually just moving bit by bit
H_number = int(H/target_high)
W_number = int(W/target_width)
. Let’s see how many can be divided. H is the actual height of the picture, and target_high is
the height of the small block that needs to be divided. For example, the H of my original picture is 4024. I need Divide into an image with a height of 512 target_high=512 , then H_number = 4024/512 = 7.859375, and then round up H_number=7 ; the same operation is the same for W
Then there is the loop, each row is divided
mask = picture[i*target_width:(i+1)*target_width, j*target_high:(j+1)*target_high, :]
, here picture[W,H,C]
, use the slice operation, the width is from [i*target_width:(i+1)*target_width]
, in fact, it is just a small piece of movement, the height is also the same, j*target_high:(j+1)*target_high
the :
color channel does not move, how much is still How much; the final mask is a picture of the required size, and then it is saved
save_picture = picture_save_path + picture_name + "_{}_{}.jpg".format(i+1,j+1)
, specify the location path and name to be saved, and what format you want to save in the end, .jpg
just change it
cv2.imwrite(save_picture,mask)
, and finally save it
full code
import cv2
picture_name = "a1"
picture_path = "E:/sample/a1.TIF"
picture_save_path = "E:/result/"
target_width = 512
target_high = 512
target_channel = 3
picture = cv2.imread(picture_path)
print(picture.shape)# (4024, 6048, 3) [H,W,C]
H = picture.shape[0]
W = picture.shape[1]
C = picture.shape[2]
H_number = int(H/target_high)
W_number = int(W/target_width)
C_number = C
print("H_number is:",H_number)
print("W_number is:",W_number)
for i in range(0, H_number):
for j in range(0, W_number):
mask = picture[i*target_width:(i+1)*target_width, j*target_high:(j+1)*target_high, :]
save_picture = picture_save_path + picture_name + "_{}_{}.jpg".format(i+1,j+1)
cv2.imwrite(save_picture,mask)
print("okk")
可分为7*11=77张小图
Show results
Picture location
split position
2. Batch segmentation of images
I took about a hundred photos, and then performed batch processing and segmentation on these samples.
First, I rename these photos. You can refer to the blog post: Renaming batch files
Get these 34 samples
Then start batch processing
and splitting these images. It is similar to the above operation, except that there is an additional os to read and batch obtain file names and paths.
picture_path
The path of the image
picture_save_path
is the path to save after batch processing
for filename in os.listdir(picture_path):
Pass os.listdir()
, get the name filename
picture_absolute_path = picture_path + filename
path + name of each photo, get the absolute path of each picture , and separate picture_absolute_path
picture_name,picture_suffix = os.path.splitext(picture_allname)
the name picture_name
and suffix of the photo . Subsequent operations are the same as processing a single imagepicture_suffix
full code
import cv2
import os
picture_path = r"E:/sample/"
picture_save_path = "E:/result/"
target_width = 512
target_high = 512
target_channel = 3
for filename in os.listdir(picture_path):
picture_absolute_path = picture_path + filename # E:/sample/14-04-2023-16-37-45.TIF
picture_allname = os.path.basename(picture_absolute_path) # 14-04-2023-16-37-45.TIF
picture_name,picture_suffix = os.path.splitext(picture_allname)
# picture_name 14-04-2023-16-37-45
# picture_suffix .TIF
#print(picture_name) # 14-04-2023-16-37-45
picture = cv2.imread(file_path)
#print(picture.shape)
H = picture.shape[0]
W = picture.shape[1]
C = picture.shape[2]
H_number = int(H/target_high)
W_number = int(W/target_width)
C_number = C
for i in range(0, H_number):
for j in range(0, W_number):
mask = picture[i*target_width:(i+1)*target_width, j*target_high:(j+1)*target_high, :]
save_picture = picture_save_path + picture_name + "_{}_{}.jpg".format(i+1,j+1)
cv2.imwrite(save_picture,mask)
print(filename + "is okk")
print("finished!!!")
Show results
A single picture can be divided into a total of 34 pictures, so it can be divided into a total of 7 * 11 = 77
34 pictures77 * 34 = 2618