如何使用opencv python从底部裁剪图像

2024-04-18 17:45:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把图像分成两部分 1) 直到底部文字一个图像 2) 从文本到另一个图像的结尾。在

我不知道从哪里开始,经过几个答案,但仍然困惑。在

Please find the original image

剪完后我想要一种

Top one

下图被分开

Bottom one


Tags: 答案图像文本结尾文字
2条回答

你可以简单地创建2个基于像素值的子图像。在

您可以使用subimage = image[Y_start : Y_end, X_start : X_end]完成此操作。在

下面的代码给出了这个结果:

enter image description here

# load image
img = cv2.imread("map.png")

# create sub images
img_map = img[0:600, 0:600]
img_legend = img[600:705, 0:600]

#show images - to save the images, uncomment the lines below.
cv2.imshow("map", img_map)
cv2.imshow("legend", img_legend)
# cv2.imwrite('map_only.png',img_map)
# cv2.imwrite('legend_only.png',img_legend)
cv2.waitKey(0)
cv2.destroyAllWindows()  

可以基于像素定义裁剪边距并将其指定给新变量:

src_img = cv2.imread(image_file)


crop_img = src_img[h_start : h_end, w_start : w_end].copy()

cv2.imshow("original", src_img )
cv2.imshow("cropped", crop_img)

cv2.waitKey(0)

注:

需要指定新图像末尾的copy(),否则原始图像将被覆盖

相关问题 更多 >