当前位置:网站首页>这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】
这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】
2022-07-28 00:49:00 【51CTO】
前言
有一句叫:数据和特征决定了机器学习的上限,而模型和算法只是逼近这个上限而已
实现思路和代码展示
批量裁剪固定区域的实现思路
1、找到存储图片的文件夹路径,并读取名字注意点:裁剪后的图片的长和高必须为整数
代码
import
os
import
cv2
#
准备工作:导入库和读取图片路径以及存储图片路径
imgs_path
=
'/media/pzw/0E50196C0E50196C/weixin/pystudy/imgs'
dst_path
=
'/media/pzw/0E50196C0E50196C/weixin/pystudy/crop1'
#
1
、读取图片名字
imgs_name
=
os.
listdir(
imgs_path)
print(
'文件夹下图片的名字:',
imgs_name)
#
2
、读取图片尺寸,公式定义裁剪区域
for
img
in
imgs_name:
image
=
cv2.
imread(
os.
path.
join(
imgs_path,
img))#
读取图片
x,
y
=
image.
shape[
0:
2] #
读取图片的尺寸
print(
'图片的形状',
image.
shape)
#
裁剪掉x方向的前面和后面各20
%
,并取整
crop_image
=
image[
round(
0.2
*
x):
round(
0.8
*
x),
0:
y]
#
3
、将裁剪后的图片保存
cv2.
imwrite(
dst_path
+
'/'
+
img,
crop_image)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
结果展示

批量自定义裁剪区域的实现思路
1、找到存储图片的文件夹路径,并读取名字需改为用鼠标选取区域)注意点:操作指南,在图片中顺时针或者逆时针点出四个点,即可裁剪出区域,按esc保存并对下一张图片进行裁剪,此时会保留上一次裁剪区域所留下的点,必须按右键取消重新选点
代码
import
cv2
import
numpy
as
np
import
os
#
--
--
--
--
--
--
--
--
--
--
--
-
定义鼠标相关操作
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
lsPointsChoose
= []
tpPointsChoose
= []
pointsCount
=
0
count
=
0
pointsMax
=
5 #
绘制四边形时
pointmax
=
5
,五边形时,为6,以此类推,
但需要调整部分代码
def
on_mouse(
event,
x,
y,
flags,
param):
global
img,
point1,
point2,
count,
pointsMax
global
lsPointsChoose,
tpPointsChoose #
存入选择的点
global
pointsCount #
对鼠标按下的点计数
global
img2,
ROI_bymouse_flag
img2
=
img.
copy() #
此行代码保证每次都重新再原图画
避免画多了
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
#
count
=
count
+
1
#
print(
"callback_count",
count)
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
if
event
==
cv2.
EVENT_LBUTTONDOWN: #
左键点击
pointsCount
=
pointsCount
+
1
print(
'pointsCount:',
pointsCount)
point1
= (
x,
y)
print(
x,
y)
#
画出点击的点
cv2.
circle(
img2,
point1,
10, (
0,
255,
0),
2)
#
将选取的点保存到list列表里
lsPointsChoose.
append([
x,
y]) #
用于转化为darry
提取多边形ROI
tpPointsChoose.
append((
x,
y)) #
用于画点
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
#
将鼠标选的点用直线连起来
print(
len(
tpPointsChoose))
for
i
in
range(
len(
tpPointsChoose)
-
1):
print(
'i',
i)
cv2.
line(
img2,
tpPointsChoose[
i],
tpPointsChoose[
i
+
1], (
0,
0,
255),
2)
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
#
--
--
--
--
--
点击到pointMax时可以提取去绘图
--
--
--
--
--
--
--
--
if (
pointsCount
==
pointsMax):
#
--
--
--
--
--
-
绘制感兴趣区域
--
--
--
--
--
-
ROI_byMouse()
ROI_bymouse_flag
=
1
lsPointsChoose
= []
cv2.
imshow(
'src',
img2)
#
--
--
--
--
--
--
--
--
--
--
--
--
-
右键按下清除轨迹
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
if
event
==
cv2.
EVENT_RBUTTONDOWN: #
右键点击
print(
"right-mouse")
pointsCount
=
0
tpPointsChoose
= []
lsPointsChoose
= []
print(
len(
tpPointsChoose))
for
i
in
range(
len(
tpPointsChoose)
-
1):
print(
'i',
i)
cv2.
line(
img2,
tpPointsChoose[
i],
tpPointsChoose[
i
+
1], (
0,
0,
255),
2)
cv2.
imshow(
'src',
img2)
def
ROI_byMouse():
global
src,
ROI,
ROI_flag,
mask2,
img1
mask
=
np.
zeros(
img.
shape,
np.
uint8)
pts
=
np.
array([
lsPointsChoose],
np.
int32) #
pts是多边形的顶点列表(顶点集)
pts
=
pts.
reshape((
-
1,
1,
2))
#
这里
reshape
的第一个参数为
-
1,
表明这一维的长度是根据后面的维度的计算出来的。
#
OpenCV中需要先将多边形的顶点坐标变成顶点数×1×2维的矩阵,再来绘制
#
--
--
--
--
--
--
--
画多边形
--
--
--
--
--
--
--
--
--
--
-
mask
=
cv2.
polylines(
mask, [
pts],
True, (
255,
255,
255))
##
--
--
--
--
--
--
-
填充多边形
--
--
--
--
--
--
--
--
--
--
-
mask2
=
cv2.
fillPoly(
mask, [
pts], (
255,
255,
255))
cv2.
imshow(
'mask',
mask2)
#
cv2.
imwrite(
'mask.jpg',
mask2)
ROI
=
cv2.
bitwise_and(
mask2,
img)
##
--
--
--
--
--
--
-
裁剪四边形
--
--
--
--
--
--
--
--
--
--
-
print(
pts)
print(
'y0的值为',
pts[
0][
0][
0])
ROI_crop
=
img[
pts[
0][
0][
1]:
pts[
2][
0][
1],
pts[
0][
0][
0]:
pts[
2][
0][
0]] #
裁剪坐标为[
y0:
y1,
x0:
x1]
cv2.
imwrite(
dst_path
+
img1,
ROI_crop)
cv2.
imshow(
img1,
ROI)
''
'批量绘制ROI区域'
''
imgs_path
=
'/media/pzw/0E50196C0E50196C/weixin/pystudy/imgs/'
dst_path
=
'/media/pzw/0E50196C0E50196C/weixin/pystudy/crop2/'
if
not
os.
path.
exists(
dst_path):
os.
makedirs(
dst_path)
imgs
=
os.
listdir(
imgs_path)
print(
imgs)
for
img1
in
imgs:
img
=
cv2.
imread(
os.
path.
join(
imgs_path,
img1))
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
#
--
图像预处理,设置其大小
#
height,
width
=
img.
shape[:
2]
#
size
= (
int(
width
*
0.3),
int(
height
*
0.3))
#
img
=
cv2.
resize(
img,
size,
interpolation
=
cv2.
INTER_AREA)
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
ROI
=
img.
copy()
cv2.
namedWindow(
'src')
cv2.
setMouseCallback(
'src',
on_mouse)
#为了方便裁剪,这里对图片大小进行重新设置
img
=
cv2.
resize(
img,(
640,
960),
interpolation
=
cv2.
INTER_CUBIC)
cv2.
imshow(
'src',
img)
cv2.
waitKey(
0)
cv2.
destroyAllWindows()
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
结果展示



— END—
边栏推荐
- Flex开发网页实例web端
- 云原生爱好者周刊:Prometheus 架构演进之路
- Data output - image annotation and annotation
- What are the important applications of MES system in manufacturing enterprises
- Linux Installation mysql8.0.29 detailed tutorial
- Codeforces Round #807 (Div. 2) A-C题解
- HyperMesh circular array - plug in
- Promise从入门到精通 (第1章 Promise的介绍和基本使用)
- 萤石网络,难当「孤勇者」
- sftp文件/文件夹上传服务器
猜你喜欢

【愚公系列】2022年07月 Go教学课程 019-循环结构之for
![[database data recovery] data recovery case of insufficient disk space of SQL Server database](/img/0e/908db40e1e8b7dd62e12558c1c6dc4.png)
[database data recovery] data recovery case of insufficient disk space of SQL Server database

Promise从入门到精通 (第2章 Promise的理解和使用)

C# 使用Abp仓储访问数据库时报错记录集

UE4 unreal ndisplay plug-in easy to use three fold screen details

MPLS tunnel experiment

Linux Installation mysql8.0.29 detailed tutorial

BGP federal experiment

Unreal ue4.27 switchboard porting engine process

二叉树的遍历和性质
随机推荐
微信小程序实现动态横向步骤条的两种方式
Domain Driven Design -- Terminology
MPLS tunnel experiment
SFTP file / folder upload server
Software test interview questions: common post data submission methods
Fiddler mobile packet capturing agent settings (for Huawei glory 60s)
Product interpretation - Design and distributed expansion of metersphere UI test module
Common video resolution
Promise from introduction to mastery (Chapter 1 Introduction and basic use of promise)
Wechat applet pictures are scaled according to the screen scale
Execute add migration migration and report build failed
都在说DevOps,你真正了解它吗?
Completely delete MySQL in Linux system
视频常用分辨率
Codeforces Round #810 (Div. 2)A~C题解
53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b
Flex布局学习完成PC端
Comparison between hardware SPI and software analog SPI rate
Software testing interview question: what do you think is the key to good test case design?
[database data recovery] data recovery case of insufficient disk space of SQL Server database