|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# @Time : 2017/11/8 0008 下午 19:18 |
| 5 | +# @Author : 李雪洋 |
| 6 | +# @File : 01.py |
| 7 | +# @Software: PyCharm |
| 8 | +import cv2 |
| 9 | +import numpy as np |
| 10 | +import os |
| 11 | + |
| 12 | +img_save_path = 'E:\\pythonDemo\\opencv\\cbss\\spit\\' |
| 13 | + |
| 14 | + |
| 15 | +# 按照指定图像大小调整尺寸 |
| 16 | +def resize_image(image, height, width): |
| 17 | + top, bottom, left, right = (0, 0, 0, 0) |
| 18 | + |
| 19 | + # 获取图像尺寸 |
| 20 | + h, w = image.shape |
| 21 | + |
| 22 | + # 对于长宽不相等的图片,找到最长的一边 |
| 23 | + longest_edge = max(h, w) |
| 24 | + |
| 25 | + # 计算短边需要增加多上像素宽度使其与长边等长 |
| 26 | + if h < longest_edge: |
| 27 | + dh = longest_edge - h |
| 28 | + top = dh // 2 |
| 29 | + bottom = dh - top |
| 30 | + elif w < longest_edge: |
| 31 | + dw = longest_edge - w |
| 32 | + left = dw // 2 |
| 33 | + right = dw - left |
| 34 | + else: |
| 35 | + pass |
| 36 | + |
| 37 | + # RGB颜色 |
| 38 | + BLACK = [0, 0, 0] |
| 39 | + |
| 40 | + # 给图像增加边界,是图片长、宽等长,cv2.BORDER_CONSTANT指定边界颜色由value指定 |
| 41 | + constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK) |
| 42 | + |
| 43 | + # 调整图像大小并返回 |
| 44 | + return cv2.resize(constant, (height, width)) |
| 45 | + |
| 46 | + |
| 47 | +def saveSpitImg(boxList, dst): |
| 48 | + for box in boxList: |
| 49 | + img_org2 = dst.copy() |
| 50 | + img_plate = img_org2[box[2]:box[3], box[0]:box[1]] |
| 51 | + # img_plate = cv2.resize(img_plate, (30, 30), interpolation=cv2.INTER_CUBIC) |
| 52 | + img_plate = resize_image(img_plate, 30, 30) |
| 53 | + l = os.listdir(img_save_path) |
| 54 | + size = len(l) |
| 55 | + size += 1 |
| 56 | + cv2.imwrite(img_save_path + str(size) + '.bmp', img_plate) |
| 57 | + |
| 58 | + |
| 59 | +for i in range(1, 5001): |
| 60 | + print(i) |
| 61 | + srcImg = cv2.imread(f"E:\\pythonDemo\\opencv\\cbss\\img\\{i}.bmp") |
| 62 | + h, w, _ = srcImg.shape |
| 63 | + dstImg = cv2.resize(srcImg, (w * 10, h * 10), interpolation=cv2.INTER_CUBIC) |
| 64 | + # cv2.imshow('dstImg', dstImg) |
| 65 | + # cv2.waitKey(0) |
| 66 | + |
| 67 | + gray = cv2.cvtColor(dstImg, cv2.COLOR_BGR2GRAY) |
| 68 | + |
| 69 | + ret1, th1 = cv2.threshold(gray, 82, 255, cv2.THRESH_BINARY_INV) |
| 70 | + # cv2.imshow('th1', th1) |
| 71 | + # cv2.waitKey(0) |
| 72 | + |
| 73 | + kernel = np.ones((11, 11), np.uint8) |
| 74 | + dilation = cv2.dilate(th1, kernel, iterations=1) |
| 75 | + |
| 76 | + # cv2.imshow('dilation', dilation) |
| 77 | + # cv2.waitKey(0) |
| 78 | + |
| 79 | + kernel = np.ones((11, 11), np.uint8) |
| 80 | + dilation2 = cv2.dilate(dilation, kernel, iterations=1) |
| 81 | + |
| 82 | + # cv2.imshow('dilation2', dilation2) |
| 83 | + # cv2.waitKey(0) |
| 84 | + |
| 85 | + (_, contours, _) = cv2.findContours(dilation2, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) |
| 86 | + |
| 87 | + boxList = [] |
| 88 | + for j in contours: |
| 89 | + # 计算该轮廓的面积 |
| 90 | + area = cv2.contourArea(j) |
| 91 | + # |
| 92 | + # 面积小的都筛选掉 |
| 93 | + if area < 1300: |
| 94 | + continue |
| 95 | + |
| 96 | + x, y, width, height = cv2.boundingRect(j) |
| 97 | + box = [x, y, width, height] |
| 98 | + box = [box[0], box[0] + box[2], box[1], box[1] + box[3]] |
| 99 | + boxList.append(box) |
| 100 | + # cv2.rectangle(dstImg, (box[0], box[2]), (box[1], box[3]), (0, 255, 0), 1) |
| 101 | + saveSpitImg(boxList, th1) |
0 commit comments