当前位置:网站首页>This operation may not be worth money, but it is worth learning | [batch cutting of pictures]
This operation may not be worth money, but it is worth learning | [batch cutting of pictures]
2022-07-28 02:17:00 【51CTO】
Preface
There is a saying called : Data and features determine the upper limit of machine learning , And the model and algorithm are just approaching the upper limit
Implementation ideas and code display
The realization idea of batch cutting fixed areas
1、 Find the folder path where the pictures are stored , And read the name Be careful : The length and height of the cropped image must be an integer
Code
import
os
import
cv2
#
preparation : Import library, read image path and store image path
imgs_path
=
'/media/pzw/0E50196C0E50196C/weixin/pystudy/imgs'
dst_path
=
'/media/pzw/0E50196C0E50196C/weixin/pystudy/crop1'
#
1
、 Read the picture name
imgs_name
=
os.
listdir(
imgs_path)
print(
' The name of the picture under the folder :',
imgs_name)
#
2
、 Read the picture size , The formula defines the crop region
for
img
in
imgs_name:
image
=
cv2.
imread(
os.
path.
join(
imgs_path,
img))#
Read the picture
x,
y
=
image.
shape[
0:
2] #
Read the size of the picture
print(
' The shape of the picture ',
image.
shape)
#
Cut out x The front and back of the direction 20
%
, And round it up
crop_image
=
image[
round(
0.2
*
x):
round(
0.8
*
x),
0:
y]
#
3
、 Save the cropped picture
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.
Result display

The realization idea of batch customized clipping region
1、 Find the folder path where the pictures are stored , And read the name You need to use the mouse to select the area ) Be careful : Operation guide , Point out four points clockwise or counterclockwise in the picture , You can crop out the area , Press esc Save and crop the next picture , At this time, the points left by the last clipping region will be preserved , You must press the right button to cancel the reselection
Code
import
cv2
import
numpy
as
np
import
os
#
--
--
--
--
--
--
--
--
--
--
--
-
Define mouse related operations
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
lsPointsChoose
= []
tpPointsChoose
= []
pointsCount
=
0
count
=
0
pointsMax
=
5 #
When drawing quadrangles
pointmax
=
5
, Pentagon time , by 6, And so on ,
But you need to adjust some of the code
def
on_mouse(
event,
x,
y,
flags,
param):
global
img,
point1,
point2,
count,
pointsMax
global
lsPointsChoose,
tpPointsChoose #
Save the selected point
global
pointsCount #
Count the points pressed by the mouse
global
img2,
ROI_bymouse_flag
img2
=
img.
copy() #
This line of code ensures that the original picture is repeated every time
Avoid drawing too much
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
#
count
=
count
+
1
#
print(
"callback_count",
count)
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
if
event
==
cv2.
EVENT_LBUTTONDOWN: #
Left click
pointsCount
=
pointsCount
+
1
print(
'pointsCount:',
pointsCount)
point1
= (
x,
y)
print(
x,
y)
#
Draw the point you click on
cv2.
circle(
img2,
point1,
10, (
0,
255,
0),
2)
#
Save the selected points to list In the list
lsPointsChoose.
append([
x,
y]) #
Used to convert to darry
Extract polygons ROI
tpPointsChoose.
append((
x,
y)) #
Used to draw dots
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
#
Connect the points selected by the mouse with a straight line
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)
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
#
--
--
--
--
--
Click to pointMax Can be extracted to draw
--
--
--
--
--
--
--
--
if (
pointsCount
==
pointsMax):
#
--
--
--
--
--
-
Draw the region of interest
--
--
--
--
--
-
ROI_byMouse()
ROI_bymouse_flag
=
1
lsPointsChoose
= []
cv2.
imshow(
'src',
img2)
#
--
--
--
--
--
--
--
--
--
--
--
--
-
Right click to clear the track
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
if
event
==
cv2.
EVENT_RBUTTONDOWN: #
Right click on the
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 Is a list of vertices of a polygon ( Vertex set )
pts
=
pts.
reshape((
-
1,
1,
2))
#
here
reshape
The first parameter of is
-
1,
It shows that the length of this one dimension is calculated according to the following dimensions .
#
OpenCV In, you need to change the vertex coordinates of the polygon into vertex points ×1×2 A matrix of dimensions , Then draw
#
--
--
--
--
--
--
--
Draw a polygon
--
--
--
--
--
--
--
--
--
--
-
mask
=
cv2.
polylines(
mask, [
pts],
True, (
255,
255,
255))
##
--
--
--
--
--
--
-
Fill polygons
--
--
--
--
--
--
--
--
--
--
-
mask2
=
cv2.
fillPoly(
mask, [
pts], (
255,
255,
255))
cv2.
imshow(
'mask',
mask2)
#
cv2.
imwrite(
'mask.jpg',
mask2)
ROI
=
cv2.
bitwise_and(
mask2,
img)
##
--
--
--
--
--
--
-
Crop quadrilateral
--
--
--
--
--
--
--
--
--
--
-
print(
pts)
print(
'y0 The value of is ',
pts[
0][
0][
0])
ROI_crop
=
img[
pts[
0][
0][
1]:
pts[
2][
0][
1],
pts[
0][
0][
0]:
pts[
2][
0][
0]] #
The cutting coordinate is [
y0:
y1,
x0:
x1]
cv2.
imwrite(
dst_path
+
img1,
ROI_crop)
cv2.
imshow(
img1,
ROI)
''
' Batch drawing ROI Area '
''
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))
#
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
#
--
Image preprocessing , Set its size
#
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)
# For the convenience of cutting , Here you can reset the size of the picture
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.
Result display



— END—
边栏推荐
猜你喜欢

Codeworks round 810 (Div. 2) a~c problem solution

Vxe Table/Grid 单元格分组合并

Ceresdao: new endorsement of ventures Dao

Redis design specification

Cloud native enthusiast weekly: the evolution of Prometheus architecture

这个操作可能不值钱,但却值得学习 | 【图片批量裁剪】

Unity 保存图片到相册以及权限管理

软考 --- 数据库(2)关系模型

Ceresdao: the world's first decentralized digital asset management protocol based on Dao enabled Web3.0

Flume (5 demos easy to get started)
随机推荐
Interviewer: are you sure redis is a single threaded process?
软件测试面试题:常见的 POST 提交数据方式
学会这招再也不怕手误让代码崩掉
如何评估研发人员效能?软件工程师报告帮你看见每个人的贡献
Codeworks round 807 (Div. 2) a-c problem solution
Promise from introduction to mastery (Chapter 1 Introduction and basic use of promise)
They are all talking about Devops. Do you really understand it?
Software test interview questions: common post data submission methods
WMS you don't know
Cloud native enthusiast weekly: the evolution of Prometheus architecture
Go learning 01
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(3)后台功能
11 Django basics database operation
[Star Project] small hat aircraft War (VI)
Starfish Os打造的元宇宙生态,跟MetaBell的合作只是开始
Common video resolution
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
Flex development web page instance web side
MySQL create stored procedure ------ [hy000][1418] this function has none of deterministic, no SQL
QGIS制图:矢量数据制图流程及导出