def cutting(self, writer, fname):
fpath = '{:s}/{:s}/{:s}'.format(self.conf.source_path, writer, fname)
img = cv2.imread(fpath)
if img is None: return
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.GaussianBlur(img_gray, (5, 5), 0)
ret, im_th = cv2.threshold(img_gray, 90, 255, cv2.THRESH_BINARY_INV)
ctrs, hier = cv2.findContours(im_th.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
rects = [cv2.boundingRect(ctr) for ctr in ctrs]
if self.debug:
print('=> file path = {:s}'.format(fpath))
for i, rect in enumerate(rects):
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.rectangle(img, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), (0, 255, 0), 3)
cv2.putText(img, '({:d},{:d})'.format(rect[0], rect[1]), (rect[0], rect[1]), font, 0.8, (0, 255, 0), 2)
cv2.putText(img, '({:d},{:d})'.format(rect[0] + rect[2], rect[1] + rect[3]), (rect[0] + rect[2], rect[1] + rect[3]), font, 0.8, (0, 255, 0), 2)
leng = int(rect[3] * 1.6)
pt1 = int(rect[1] + rect[3] // 2 - leng // 2)
pt2 = int(rect[0] + rect[2] // 2 - leng // 2)
if pt1 < 0 or pt2 < 0: continue
roi = im_th[pt1:pt1 + leng, pt2:pt2 + leng]
print("i = {:d} leng = {:.0f} pt1 = {:d} pt2 = {:d} rect[0] = {:d} rect[1] = {:d} rect[2] = {:d} rect[3] = {:d}".format(i, leng, pt1, pt2, rect[0], rect[1], rect[2], rect[3]))
from matplotlib import pyplot
import matplotlib as mpl
fig = pyplot.figure()
ax = fig.add_subplot(1, 1, 1)
imgplot = ax.imshow(roi, cmap=mpl.cm.Greys)
imgplot.set_interpolation('nearest')
ax.xaxis.set_ticks_position('top')
ax.yaxis.set_ticks_position('left')
#pyplot.show()
roi = cv2.resize(roi, (28, 28), interpolation = cv2.INTER_AREA)
roi = cv2.dilate(roi, (3, 3))
#roi_hog_fd = hog(roi, orientations = 9, pixels_per_cell = (14, 14), cells_per_block = (1, 1), visualise = False)
cv2.imwrite('{:s}/{:s}/img.{:d}.{:.2f}.jpg'.format(self.conf.train_path, writer, i, time.time()), roi)
cv2.imwrite('{:s}/img.{:d}.jpg'.format(self.conf.tmp_path, i), img)
评论列表
文章目录