When dividing an image in opencv, to save one image in four parts
#-*-coding:utf-8-*-
import cv2
defmain():
'''
# Cut out the rectangular portion through two points (x1, y1), (x2, y2)
clp=img [x1:x2, y1:y2]
# Save clipped area
cv2.imwrite("img.png", clp)
'''
img = cv2.imread("test.png")
height, width, channels = img.shape
clp=img [0:height/2,0:width/2]
cv2.imwrite("test-tl.png", clp)
clp=img [0:height/2, width/2:width]
cv2.imwrite("test-tr.png", clp)
clp=img [height/2:height, 0:width/2]
cv2.imwrite("test-ul.png", clp)
clp=img [height/2:height, width/2:width]
cv2.imwrite("test-ur.png", clp)
if__name__=='__main__':
main()
It was made with the code
How do I change the code if I divide the image into two verticals and three horizontals?
I would appreciate it if you could let me know.
This is an example of a vertical and horizontal loop.
#!/usr/bin/python3
import cv2
defmain():
img = cv2.imread("test.png")
height, width, channels = img.shape
height_split=2
width_split=3
new_img_height=int(height/height_split)
new_img_width=int(width/width_split)
for h in range (height_split):
height_start=h*new_img_height
height_end = height_start + new_img_height
for win range (width_split):
width_start=w*new_img_width
width_end = width_start + new_img_width
file_name="test_"+str(h)+"_"+str(w)+".png"
clp=img [height_start:height_end, width_start:width_end]
cv2.imwrite(file_name,clp)
if__name__=='__main__':
main()
© 2024 OneMinuteCode. All rights reserved.