文章目录提示:个人笔记
- 前言
- 一、取目标轮廓坐标
- 二、代码处理
前言
提示:笔记背景
在写专利的时候,需要附录一些图,画轮廓图又太难看了,所以就把实物的目标抠出来后再放到白板上
提示:以下是本篇文章正文内容,下面案例可供参考
一、取目标轮廓坐标用的是labelme把对象都标注出来,保存成json
代码如下(示例):
import os
import cv2
import json
import numpy as np
import matplotlib.pyplot as plt
def read_json(josn_path):
obj = json.load(open(josn_path))
shapes = obj['shapes']
return shapes
def draw(image_path,shapes):
#灰度化
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# 创建白板背景图
background = np.zeros_like(image)
background[:] = 255
temp = []
# 将轮廓都取出来
for shape in shapes:
points = shape['points']
np_points = np.array(points, np.int32)
temp.append(np_points)
#在白板上填充目标
cv2.fillPoly(background,temp,0)
# 将灰度化的目标取出来,粘贴到白板上
background[background==0] = image[background==0]
#name = os.path.basename(image_path)
#path = ''
#保存
cv2.imwrite(os.path.join(path,name),background)
def fillploy():
''' fillploy测试例
'''
a = np.array([[[10,10],[100,10],[100,100],[10,100]]],dtype=np.int32)
b = np.array([[[100, 100], [200, 230], [150, 200], [100, 220]]], dtype=np.int32)
print(a.shape)
im = np.zeros([240,320],dtype=np.uint8)
cv2.polylines(im,a,1,255)
cv2.fillPoly(im,b,255)
plt.imshow(im)
plt.show()
if __name__=='__main__':
root_path = ''
for file in os.listdir(root_path):
file_path = os.path.join(root_path,file)
if file.endswith('json'):
image_path = file_path.replace('json','jpg')
shapes = read_json(file_path)
draw(image_path,shapes)



