利用python和opencv批量去掉图片黑边
import os
import cv2
import numpy as np
from scipy.stats import mode
import time
import concurrent.futures '''
multi-process to crop pictures.
''' def crop(file_path_list):
origin_path, save_path = file_path_list
img = cv2.imread(origin_path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) closed_1 = cv2.erode(gray, None, iterations=4)
closed_1 = cv2.dilate(closed_1, None, iterations=4)
blurred = cv2.blur(closed_1, (9, 9))
# get the most frequent pixel
num = mode(blurred.flat)[0][0] + 1
# the threshold depends on the mode of your images' pixels
num = num if num <= 30 else 1 _, thresh = cv2.threshold(blurred, num, 255, cv2.THRESH_BINARY) # you can control the size of kernel according your need.
kernel = np.ones((13, 13), np.uint8)
closed_2 = cv2.erode(thresh, kernel, iterations=4)
closed_2 = cv2.dilate(closed_2, kernel, iterations=4) _, cnts, _ = cv2.findContours(closed_2.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
c = sorted(cnts, key=cv2.contourArea, reverse=True)[0] # compute the rotated bounding box of the largest contour
rect = cv2.minAreaRect(c)
box = np.int0(cv2.boxPoints(rect)) # draw a bounding box arounded the detected barcode and display the image
# cv2.drawContours(img, [box], -1, (0, 255, 0), 3)
# cv2.imshow("Image", img)
# cv2.imwrite("pic.jpg", img)
# cv2.waitKey(0) xs = [i[0] for i in box]
ys = [i[1] for i in box]
x1 = min(xs)
x2 = max(xs)
y1 = min(ys)
y2 = max(ys)
height = y2 - y1
width = x2 - x1
crop_img = img[y1:y1 + height, x1:x1 + width]
cv2.imwrite(save_path, crop_img)
# cv2.imshow("Image", crop_img)
# cv2.waitKey(0)
print(f'the {origin_path} finish crop, most frequent pixel is {num}') def multi_process_crop(input_dir):
with concurrent.futures.ProcessPoolExecutor() as executor:
executor.map(crop, input_dir) if __name__ == "__main__":
data_dir = ''
save_dir = ''
path_list = [(os.path.join(data_dir, o), os.path.join(save_dir, o)) for o in os.listdir(data_dir)]
start = time.time()
multi_process_crop(path_list)
print(f'Total cost {time.time()-start} seconds')
利用python和opencv批量去掉图片黑边的更多相关文章
- Python cv2 OpenCV 中传统图片格式与 base64 转换
Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,是一种基于64个可打印字符来表示二进制数据的方法.通过http传输图片常常将图片数据转换成base64之后再进行传输. Base64简 ...
- 10 行 Python 代码,批量压缩图片 500 张,简直太强大了
本文原创并首发于公众号[Python猫],未经授权,请勿转载. 原文地址:https://mp.weixin.qq.com/s/5hpFDgjCpfb0O1Jg-ycACw 熟悉 "Pyth ...
- 【python】PIL 批量绘制图片矩形框工具
工具采用PIL:Python Imaging Library,图像处理标准库.PIL功能非常强大,但API却非常简单易用. 安装PIL 在Debian/Ubuntu Linux下直接通过apt安装 $ ...
- 利用Python实现简单的相似图片搜索的教程
大概五年前吧,我那时还在为一家约会网站做开发工作.他们是早期创业公司,但他们也开始拥有了一些稳定用户量.不像其他约会网站,这家公司向来以洁身自好为主要市场形象.它不是一个供你鬼混的网站——是让你能找到 ...
- 利用python爬虫关键词批量下载高清大图
前言 在上一篇写文章没高质量配图?python爬虫绕过限制一键搜索下载图虫创意图片!中,我们在未登录的情况下实现了图虫创意无水印高清小图的批量下载.虽然小图能够在一些移动端可能展示的还行,但是放到pc ...
- 手把手教你利用Python自动下载CL社区图片
需求描述: 最近发现CL社区上好多精华的帖子分享的图片非常棒,好想好想保存下来,但是一张一张地保存太费时间了,因此,造物者思想主义的我就想动手写个工具,实现只要输入帖子的链接,就能把所有的精华 ...
- opencv批量读取图片
#include<opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){ int num=4;// ...
- 利用Python对文件批量重命名
由于要用到对无序的文件进行重命名,以下写了一个脚本,进行批量进行重命名. 基本格式是 i.后缀名 ( i 循环条件下的数 ) 1 #coding:utf8 2 import os; 3 4 def ...
- opencv批量修改图片尺寸
#include"opencv2/opencv.hpp" using namespace std; using namespace cv; #include<opencv2/ ...
随机推荐
- python数据分析工具安装集合
用python做数据分析离不开几个好的轮子(或称为科学棧/第三方包等),比如matplotlib,numpy, scipy, pandas, scikit-learn, gensim等,这些包的功能强 ...
- SQL Server2008宝典 全书代码
-- ============================================= -- Create database template -- ==================== ...
- Recsys2018 music recomendation
http://www.recsyschallenge.com/2018/ January 2018 Release of the "One Million Playlists" d ...
- pb getchild获取DropDownDW子窗体后进行取值
datawindowchild ldwc if dw_1.getchild("dwmc", ldwc) = 1 then long ll_f ll_f = ldwc.find( ...
- poj 2356 抽屉原理
基本原理: n+1个鸽子放到n个笼子里,至少有一个笼子里有两只及其以上的鸽子.若有n个笼子,kn+1个鸽子,至少有一个笼子里面有k+1个鸽子: 题意:给定N个数,挑出一些数,他们和和是n的整数倍: 分 ...
- ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- POJ 3764 The xor-longest Path 【01字典树&&求路径最大异或和&&YY】
题目传送门:http://poj.org/problem?id=3764 The xor-longest Path Time Limit: 2000MS Memory Limit: 65536K ...
- 使用Sleep方法延迟时间
实现效果: 关键知识:(线程的定义) 实现代码: private void Form1_Load(object sender, EventArgs e) { Thread show = new Thr ...
- c#隐藏tabcontrol选项卡
隐藏:Tab1.TabPage2.Parent = null;显示:Tab1.TabPage2.Parent =Tab1;tab1为tab控件名,TabPage2为选项卡名.
- 数据流管理:redux
redux和react是两个独立的库,所以redux并不是非用不可,是在Flux框架的基础上改进的一个框架,所以一鸣惊人 redux的三大基本原则 唯一的数据源(single source of tr ...