当前位置:网站首页>这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】
这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】
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—
边栏推荐
- Software testing interview question: what do you think is the key to good test case design?
- Implementation of mongodb/mongotemplate.upsert batch inserting update data
- 11-Django-基础篇-数据库操作
- sftp文件/文件夹上传服务器
- Product interpretation - Design and distributed expansion of metersphere UI test module
- What are the important applications of MES system in manufacturing enterprises
- Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
- synchronized详解
- Behind every piece of information you collect, you can't live without TA
- 软件测试面试题:你认为做好测试用例设计工作的关键是什么?
猜你喜欢

MySQL高可用和主从同步

Flex development web page instance web side

一种比读写锁更快的锁,还不赶紧认识一下

Vxe Table/Grid 单元格分组合并

Flex开发网页实例web端

Behind every piece of information you collect, you can't live without TA

Five basic data structures of redis

CeresDAO:Ventures DAO的“新代言”

Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses

Starfish OS X metabell strategic cooperation, metauniverse business ecosystem further
随机推荐
uniapp 总结篇 (小程序)
软件测试面试题:为什要在一个团队中开展测试工作?
54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
华为APP UI自动化测试岗面试真题,真实面试经历。
Hcip 13th day notes
Aike AI frontier promotion (7.14)
Huawei app UI automation test post interview real questions, real interview experience.
SkyWalking分布式系统应用程序性能监控工具-中
C# 使用Abp仓储访问数据库时报错记录集
结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
BGP federal experiment
Embedded classic communication protocol
Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses
Promise从入门到精通 (第2章 Promise的理解和使用)
Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步
MySQL高可用和主从同步
WMS you don't know
FreeRTOS kernel summary
Traversal and properties of binary trees
day7