机器视觉中,目标检测得到物体的坐标。
可根据坐标取出子图,进入下一步的处理。
cairo在ubuntu安装如下
sudo apt update
sudo apt install libcairo2-dev
pip install pycairo
取出子图的代码:
# -*- encoding: utf-8 -*-
"""
@date: 2021/5/13 12:56 下午
@author: xuehuiping
"""
import cairo
# 分析一行数据
def analysis(line):
category_id, line = line.strip().split(' ')
temp = []
values = line.strip().split(',')
for v in values:
if len(v) > 0:
temp.append(int(v))
return category_id, temp
def get_coordinates(regin_file_name):
coordinates = []
category_ids = []
lines = open(regin_file_name).readlines()
for line in lines:
category_id, temp = analysis(line)
coordinates.append(temp)
category_ids.append(category_id)
return category_ids, coordinates
def cut(img_file_name, category_ids, coordinates):
white_background = True
src_surface = cairo.ImageSurface.create_from_png(img_file_name)
intermediate_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
src_surface.get_width(),
src_surface.get_height())
result_index = 0;
for category_id, shape in zip(category_ids, coordinates):
result_index = result_index + 1
intermediate_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
src_surface.get_width(),
src_surface.get_height())
intermediate_ctx = cairo.Context(intermediate_surface)
intermediate_ctx.set_source_surface(src_surface, 0, 0)
intermediate_ctx.new_path()
intermediate_ctx.reset_clip()
it = iter(shape)
try:
for x, y in zip(it, it):
# print('line [%d, %d]' % (x, y))
intermediate_ctx.line_to(x, y)
print('Close')
intermediate_ctx.close_path()
# bb = 'bounding box'
(bb_x1, bb_y1, bb_x2, bb_y2) = intermediate_ctx.path_extents()
print('Resulting... %d' % result_index)
bounding_x = int(bb_x1)
bounding_y = int(bb_y1)
bounding_w = int(bb_x2 - bb_x1)
bounding_h = int(bb_y2 - bb_y1)
print('x = %d y = %d w = %d h = %d' % (bounding_x, bounding_y,
bounding_w, bounding_h))
output_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
bounding_w, bounding_h)
output_context = cairo.Context(output_surface)
if white_background:
output_context.set_source_rgb(1, 1, 1)
output_context.paint()
output_context.reset_clip()
output_context.set_source_surface(intermediate_surface, -bounding_x, -bounding_y)
intermediate_ctx.clip()
intermediate_ctx.paint()
output_context.paint()
# 第X个图片,属于X类别
output_surface.write_to_png(img_file_name + '_Region_{}_{}.png'.format(result_index, category_id))
except Exception as e:
# print('[Cairo] Exception path construction %s' % str(e))
print(e)
示例文件:
1 322,93,321,94,316,94,315,95,309,95,308,96,303,96,302,97,298,97,297,98,290,98,289,99,286,99,285,100,279,100,278,101,272,101,271,102,268,102,267,103,262,103,261,104,253,104,252,105,249,105,248,106,243,106,242,107,238,107,237,108,233,108,232,109,223,109,222,110,218,110,217,111,213,111,212,112,207,112,206,113,202,113,201,114,197,114,196,115,190,115,189,116,187,116,186,117,181,117,180,118,173,118,172,119,170,119,169,120,164,120,163,121,157,121,156,122,152,122,151,123,144,123,143,124,140,124,139,125,135,125,134,126,126,126,125,127,121,127,120,128,117,128,116,129,111,129,110,130,108,130,107,131,104,131,103,132,100,132,99,133,97,133,96,134,95,134,94,135,92,135,88,139,88,140,83,145,83,146,82,147,82,150,81,151,81,165,82,166,84,166,85,167,120,167,121,166,129,166,130,165,137,165,138,164,146,164,147,163,152,163,153,162,160,162,161,161,168,161,169,160,173,160,174,159,178,159,179,158,181,158,182,157,185,157,186,156,191,156,192,155,196,155,197,154,200,154,201,153,205,153,206,152,209,152,210,151,214,151,215,150,223,150,224,149,227,149,228,148,231,148,232,147,236,147,237,146,240,146,241,145,246,145,247,144,252,144,253,143,256,143,257,142,262,142,263,141,267,141,268,140,272,140,273,139,281,139,282,138,286,138,287,137,292,137,293,136,298,136,299,135,302,135,303,134,313,134,314,133,321,133,322,132,326,132,327,131,334,131,335,130,337,130,338,129,339,129,340,128,342,128,343,127,345,127,346,126,347,126,349,124,349,123,350,122,350,120,351,119,351,111,350,110,350,106,349,105,349,99,348,98,348,97,345,94,344,94,343,93,
第一列1
,是类别id
图片
取出的子图