python openCv实现批量图像自动识别裁剪区域进行裁剪保存

# -*- coding: utf-8 -*-
import cv2
import numpy as np

import copy
import cv2 as cv
import os

def read_path(file_pathname):
    # 遍历该目录下的所有图片文件
    for filename in os.listdir(file_pathname):
        src = cv.imread(file_pathname + '/' + filename)
        gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
        width = gray.shape[0] #图片行
        height = gray.shape[1]#图片列
        green_max = np.zeros(2) #一行两列0
        green_min = np.zeros(2)
        # print(green_max)

        a_list = []
        for i in range(width):
            for j in range(height):
                gray_value = gray[i, j]
                if gray_value > 20:
                    a = np.array([i, j])
                    a_list.append(a) #所有黑色区域坐标点
        #print(a_list[1])
        #print(a_list)
        # 最大坐标
        green_max = a_list[1]
        for g, k in enumerate(a_list):

            if k[0] > green_max[0]:
                green_max[0] = int(k[0])
            else:
                green_max[0] = int(green_max[0])

                if k[1] > green_max[1]:
                    green_max[1] = int(k[1])
                else:
                    green_max[1] = int(green_max[1])
                # print(green_max)

            # 最小坐标
            green_min = a_list[0]
            # print(green_min)
            for h, z in enumerate(a_list):
                # print(green_min)
                if z[0] < green_min[0]:
                    green_min[0] = z[0]
                else:
                    green_min[0] = green_min[0]

                    if z[1] < green_min[1]:
                        green_min[1] = z[1]
                    else:
                        green_min[1] = green_min[1]

            dst = src[int(green_min[0] - 80):int(green_max[0] + 60), int(green_min[1] - 60):int(green_max[1] + 60)]
        # cv.imshow('gray', dst)
            cv.imwrite('D:/Anaconda3/test7' + "/" + filename, dst)

read_path("09")

  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值