python打出由边框包围的_python – 提取边框并将其保存为图像
假設你有以下圖像:
現在我想提取每個獨立字母的個人圖像,為了這個任務,我已經恢復了輪廓,然后繪制了一個邊框,在這種情況下為字符’a’:
之后,我想提取每個框(在這種情況下為字母’a’),并將其保存到圖像文件.
預期結果:
這是我的代碼到目前為止
import numpy as np
import cv2
im = cv2.imread('abcd.png')
im[im == 255] = 1
im[im == 0] = 255
im[im == 1] = 0
im2 = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(im2,127,255,0)
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for i in range(0, len(contours)):
if (i % 2 == 0):
cnt = contours[i]
#mask = np.zeros(im2.shape,np.uint8)
#cv2.drawContours(mask,[cnt],0,255,-1)
x,y,w,h = cv2.boundingRect(cnt)
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
cv2.imshow('Features', im)
cv2.imwrite(str(i)+'.png', im)
cv2.destroyAllWindows()
提前致謝.
總結
以上是生活随笔為你收集整理的python打出由边框包围的_python – 提取边框并将其保存为图像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mq日志怎么看_RocketMQ的消息是
- 下一篇: usestate中的回调函数_React