二值化
from PIL import Image
## use the "open" function of PIL class
img=Image.open("C:\Users\LENOVO\Desktop\exp9\01-02-00015.jpg")
## im.convert(mode)⇒ image
## turn current image into other mode and return a new image
## "convert" function have 9 mode =>1,L,P,RGB,RGBA,CMYK,YCbCr,I,F
## L mode is pray mode
Img=img.convert("L")
Img.save("C:\Users\LENOVO\Desktop\exp9\text1.jpg")
threshold=200
## user-define pray degree
table=[]
for i in range(256):
if i
基于labelimg坐标的小图分割
import os
import cv2
label_path="C:\Users\LENOVO\Desktop\exp9\z_exp\label"
image_path="C:\Users\LENOVO\Desktop\exp9\z_exp\image"
image=cv2.imread(image_path+"\19-03-00341.jpg")
print(image.shape)## (2048, 2048, 3)
lines=[];len=0;w=h=2048
## file read
file_object = open(label_path+"\19-03-00341.txt",'r+')
try:
for line in file_object:
lines.append([line])
len=len+1
finally:
file_object.close()
for i in range(len):
lines[i]=lines[i][0].split(" ")
print(lines)
## line [0 ,x_center ,y_center ,width ,height ]
for i in range(len):
tl=float(lines[i][1])*w-float(lines[i][3])*w/2
tr=float(lines[i][1])*w+float(lines[i][3])*w/2
bl=float(lines[i][2])*h-float(lines[i][4].strip())*h/2
br=float(lines[i][2])*h+float(lines[i][4].strip())*h/2
crop=image[int(bl):int(br),int(tl):int(tr)]## slice indices must be integers or None or have an __index__ method
cv2.imwrite("C:/Users/LENOVO/Desktop/exp9/z_exp/imgsave/" + str(i) + ".jpg", crop)