本博不具有普适性。
https://blog.csdn.net/loovelj/article/details/78739790
https://blog.csdn.net/wsp_1138886114/article/details/82945328
pencv阈值
https://blog.csdn.net/a19990412/article/details/81172426
https://blog.csdn.net/jianyuchen23/article/details/80835352
先看这两篇博客找理论,然后我这边直接上我的代码,该代码可对路径下的图片批量操作,矩形切割
import cv2
import os
def get_imgs(imagePath):
imageFilenames = []
labels = []
categoryList = [None]
categoryList = [c for c in sorted(os.listdir(imagePath))
if c[0] != '.' and
os.path.isdir(os.path.join(imagePath, c))]
for category in categoryList:
if category:
walkPath = os.path.join(imagePath, category)
else:
walkPath = imagePath
category = os.path.split(imagePath)[1]
w = _walk(walkPath)
while True:
try:
# dirpath = new_data_manual/train/ce
dirpath, dirnames, filenames = w.__next__()
except StopIteration:
break
# Don't enter directories that begin with '.'
for d in dirnames[:]:
if d.startswith('.'):
dirnames.remove(d)
dirnames.sort()
# Ignore files that begin with 'ori'
filenames = [f for f in filenames if not f.startswith('ori')]
filenames.sort()
# imageFilenames = [os.path.join(dirpath, f) for f in filenames]
for f in filenames:
imageFilenames.append([category, os.path.join(dirpath, f)])
# ['label',img_path]
return imageFilenames
def _walk(top):
"""
Directory tree generator lifted from python 2.6 and then
stripped down. It improves on the 2.5 os.walk() by adding
the 'followlinks' capability.
GLU: copied from image sensor
"""
names = os.listdir(top)
dirs, nondirs = [], []
for name in names:
if os.path.isdir(os.path.join(top, name)):
dirs.append(name)
else:
nondirs.append(name)
yield top, dirs, nondirs
for name in dirs:
path = os.path.join(top, name)
for x in _walk(path):
yield x
all_image_path = 'data'
new_all_image_path = 'new_data'
data = get_imgs(all_image_path)
print(data)
labels,source_files=zip(*data)
#import pdb
#pdb.set_trace()
for i in range(0,len(data)):
initial_img = cv2.imread(source_files[i])
gray = cv2.cvtColor(initial_img, cv2.COLOR_BGR2GRAY)
#this value is important , don't change it !!!
filepath,tempfilename = os.path.split(source_files[i])
_,lab = os.path.split(filepath)
if lab =='':
thresh=0
else:
thresh=50
ret,binary = cv2.threshold(gray, thresh, 255, cv2.THRESH_BINARY)
_,contours, hierarchy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#print ("contours:类型:",type(contours))
#print ("第0 个contours:",type(contours[0]))
#print ("contours 数量:",len(contours))
#print ("contours[0]点的个数:",len(contours[0]))
#print ("contours[1]点的个数:",len(contours[1]))
#for i in range(0,len(contours)):
#x, y, w, h = cv2.boundingRect(contours[i])
#cv2.rectangle(img, (x,y), (x+w,y+h), (153,153,0), 5)
x, y, w, h = cv2.boundingRect(contours[len(contours)-1])
#cv2.rectangle(initial_img, (x,y), (x+w,y+h), (153,153,0), 5)
crop_image = initial_img[y+2:y+h-2,x+2:x+w-2]
#cv2.imwrite("1.jpg",initial_img)
tmp_target_file = new_all_image_path+'/'+labels[i]+'/'+lab+'/'
filepath,tempfilename = os.path.split(source_files[i])
filename,extension = os.path.splitext(tempfilename)
print(tmp_target_file+filename+".jpg")
cv2.imwrite(tmp_target_file+filename+".jpg",crop_image)