Android Path类中有直接能判断坐标点是不是在Path内的吗
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android Path类中有直接能判断坐标点是不是在Path内的吗相关的知识,希望对你有一定的参考价值。
参考技术A 下面的代码描述了一个套索类,该类可以判断一个点是否在用户手指所画的一个套索区域中:/**
* a polygon represents a lasso.
*
* @author snow
*
*/
public class Lasso
// polygon coordinates
private float[] mPolyX, mPolyY;
// number of size in polygon
private int mPolySize;
/**
* default constructor
*
* @param px
* polygon coordinates X
* @param py
* polygon coordinates Y
* @param ps
* polygon sides count
*/
public Lasso(float[] px, float[] py, int ps)
this.mPolyX = px;
this.mPolyY = py;
this.mPolySize = ps;
/**
* constructor
*
* @param pointFs
* points list of the lasso
*/
public Lasso(List<PointF> pointFs)
this.mPolySize = pointFs.size();
this.mPolyX = new float[this.mPolySize];
this.mPolyY = new float[this.mPolySize];
for (int i = 0; i < this.mPolySize; i++)
this.mPolyX[i] = pointFs.get(i).x;
this.mPolyY[i] = pointFs.get(i).y;
Log.d("lasso", "lasso size:" + mPolySize);
/**
* check if this polygon contains the point.
*
* @param x
* point coordinate X
* @param y
* point coordinate Y
* @return point is in polygon flag
*/
public boolean contains(float x, float y)
boolean result = false;
for (int i = 0, j = mPolySize - 1; i < mPolySize; j = i++)
if ((mPolyY[i] < y && mPolyY[j] >= y)
|| (mPolyY[j] < y && mPolyY[i] >= y))
if (mPolyX[i] + (y - mPolyY[i]) / (mPolyY[j] - mPolyY[i])
* (mPolyX[j] - mPolyX[i]) < x)
result = !result;
return result;
当用户手指在屏幕上划动时,可以保存手指划过的点用来实例化Lasso类,也可以在用户手指抬起后通过PathMeasure类来对封闭的Path对象取点,然后实例化Lasso类。
Lasso类中的contains方法即是判断点是否在多边形内本回答被提问者和网友采纳 参考技术B 不能。
欢迎采纳 参考技术C RectF rectF = new RectF();
path.computeBounds(rectF, true);
Region region = new Region();
region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));
if (region.contains((int) rawX, (int) rawY))
result = true;
ctd数据格式转普通坐标点
ctd_label2pt.py
import numpy as np
import cv2
import random
import os
def get_absolute_path(p):
if p.startswith('~'):
p = os.path.expanduser(p)
return os.path.abspath(p)
def read_lines(p):
"""return the text in a file in lines as a list """
p = get_absolute_path(p)
f = open(p,'r')
return f.readlines()
def replace_all(s, old, new, reg = False):
if reg:
import re
targets = re.findall(old, s)
for t in targets:
s = s.replace(t, new)
else:
s = s.replace(old, new)
return s
def remove_all(s, sub):
return replace_all(s, sub, '')
def split(s, splitter, reg = False):
if not reg:
return s.split(splitter)
import re
return re.split(splitter, s)
def get_bboxes(img, gt_path, save_path):
h, w = img.shape[0:2]
lines = read_lines(gt_path)
bboxes = []
tags = []
for line in lines:
line = line.strip()
line = remove_all(line, '\xef\xbb\xbf')
gt = split(line, ',')
x1 = np.int(gt[0])
y1 = np.int(gt[1])
bbox = [np.int(gt[i]) for i in range(4, 32)]
bbox = np.asarray(bbox) + ([x1 * 1.0, y1 * 1.0] * 14)
with open(save_path,'a') as fw:
for cnt,val in enumerate(bbox):
val = int(val)
if cnt != len(bbox)-1:
fw.write(str(val))
fw.write(",")
else:
fw.write(str(val))
fw.write('\n')
# def get_bboxes(img, gt_path, save_path):
# h, w = img.shape[0:2]
# lines = util.io.read_lines(gt_path)
# bboxes = []
# tags = []
# for line in lines:
# line = line.strip()
# line = util.str.remove_all(line, '\xef\xbb\xbf')
# gt = util.str.split(line, ',')
#
# x1 = np.int(gt[0])
# y1 = np.int(gt[1])
#
# bbox = [np.int(gt[i]) for i in range(4, 32)]
# bbox = np.asarray(bbox) + ([x1 * 1.0, y1 * 1.0] * 14)
# with open(save_path,'a') as fw:
# for cnt,val in enumerate(bbox):
# val = int(val)
# if cnt != len(bbox)-1:
# fw.write(str(val))
# fw.write(",")
# else:
# fw.write(str(val))
# fw.write('\n')
path_text_image = "/media/data_1/2019_project_test/PSENet/data/CTW1500/train/text_image/"
path_text_label_curve = "/media/data_1/2019_project_test/PSENet/data/CTW1500/train/text_label_curve/"
path_save_txt = os.path.dirname(os.path.dirname(path_text_image)) + '/ctd2general/'
if not os.path.exists(path_save_txt):
os.makedirs(path_save_txt)
list_img = os.listdir(path_text_image)
cnt = 0
for img_name in list_img:
cnt += 1
print("cnt=%d,img=%s"%(cnt,img_name))
img_path = path_text_image + img_name
img = cv2.imread(img_path)
txt_path = path_text_label_curve + img_name.replace('.jpg','.txt')
txt_save = path_save_txt + img_name.replace('.jpg','.txt')
get_bboxes(img, txt_path, txt_save)
以上是关于Android Path类中有直接能判断坐标点是不是在Path内的吗的主要内容,如果未能解决你的问题,请参考以下文章