问答 广场 立即登录 免费注册 提取边界框并将其另存为图像 Extract bounding box and save it as an image image image-processing opencv python 9 0 问题阐述清晰明了 0 问题阐述不知所云 假设您具有以下图像: 现在,我想将每个独立字母提取到单个图像中。目前,我已经恢复了轮廓,然后绘制了一个边界框,在这种情况下,为字符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() 提前致谢。 参考资料: Stack Overflow 收藏 2020-06-01 20:27:41 更新 花飞英• 616 评论 共 1 个回答 高赞 时间 活跃 问题阐述清晰明了 0 问题阐述不知所云 以下将给您一封信 letter = im[y:y+h,x:x+w] 收藏 2020-06-01 20:28:27 更新 百如云• 653 评论 写回答 提问 新手导航 社区规范 提出问题 进行投票 个人资料 优化问题 回答问题 关于我们 常见问题 内容许可 联系我们 @2020 AskGo 京ICP备20001863号-1
提取边界框并将其另存为图像 Extract bounding box and save it as an image image image-processing opencv python 9 0 问题阐述清晰明了 0 问题阐述不知所云 假设您具有以下图像: 现在,我想将每个独立字母提取到单个图像中。目前,我已经恢复了轮廓,然后绘制了一个边界框,在这种情况下,为字符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() 提前致谢。 参考资料: Stack Overflow 收藏 2020-06-01 20:27:41 更新 花飞英• 616 评论 共 1 个回答 高赞 时间 活跃 问题阐述清晰明了 0 问题阐述不知所云 以下将给您一封信 letter = im[y:y+h,x:x+w] 收藏 2020-06-01 20:28:27 更新 百如云• 653 评论 写回答 提问 新手导航 社区规范 提出问题 进行投票 个人资料 优化问题 回答问题 关于我们 常见问题 内容许可 联系我们 @2020 AskGo 京ICP备20001863号-1
提取边界框并将其另存为图像 Extract bounding box and save it as an image image image-processing opencv python 9 0 问题阐述清晰明了 0 问题阐述不知所云 假设您具有以下图像: 现在,我想将每个独立字母提取到单个图像中。目前,我已经恢复了轮廓,然后绘制了一个边界框,在这种情况下,为字符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() 提前致谢。 参考资料: Stack Overflow 收藏 2020-06-01 20:27:41 更新 花飞英• 616 评论 共 1 个回答 高赞 时间 活跃 问题阐述清晰明了 0 问题阐述不知所云 以下将给您一封信 letter = im[y:y+h,x:x+w] 收藏 2020-06-01 20:28:27 更新 百如云• 653 评论 写回答 提问 新手导航 社区规范 提出问题 进行投票 个人资料 优化问题 回答问题 关于我们 常见问题 内容许可 联系我们 @2020 AskGo 京ICP备20001863号-1
0
假设您具有以下图像:
现在,我想将每个独立字母提取到单个图像中。目前,我已经恢复了轮廓,然后绘制了一个边界框,在这种情况下,为字符
a
:之后,我要提取每个框(在本例中为字母
a
)并将其保存到图像文件中。预期结果:
到目前为止,这是我的代码:
提前致谢。