java绘制图形代码_ImagePy_Learn | 图形学绘制代码学习:core\draw\polygonfill.py
最近在學(xué)圖形學(xué)繪制,想到了ImagePy框架的ROI涂抹交互很方便,于是啃起了繪制代碼。
這里主要對ImagePy中一個填充工具進(jìn)行難點講解。
讓我們好好學(xué)習(xí)Python中的圖形學(xué)繪制吧。
例子代碼來源:
https://github.com/Image-Py/imagepy/blob/master/imagepy/core/draw/polygonfill.py?github.com疑問:
for i in range(len(polys)): for j in range(len(polys[i])): ys.append((i,j,polys[i][j][1])) ys = np.array(ys)
轉(zhuǎn)換成一維數(shù)組還是轉(zhuǎn)換成適合pandas處理的數(shù)據(jù)結(jié)構(gòu)?
polys[i][j][1])代表什么?
cur = scan(polys, idx, ys[:,2], st, y, cur, buf)
一列掃描嗎?
ImagePy_Learn學(xué)習(xí)系列
土鹽:ImagePy_Learn | 圖形學(xué)繪制代碼學(xué)習(xí):coredrawfill.py
土鹽:ImagePy_Learn | 圖形學(xué)繪制代碼學(xué)習(xí):paint.py
詳解如下:
round((p1[0]+k*p2[0])/(1+k),4)
參考原文鏈接:Python round() 函數(shù)
np.sort(rs)
參考原文鏈接:np.sort()函數(shù)的作用 - Vaxue的博客 - CSDN博客
plg[:-1]
X[:,0]是numpy中數(shù)組的一種寫法,表示對一個二維數(shù)組,取該二維數(shù)組第一維中的所有數(shù)據(jù),第二維中取第0個數(shù)據(jù),直觀來說,X[:,0]就是取所有行的第0個數(shù)據(jù), X[:,-1] 就是取所有行的最后一個數(shù)據(jù)。
import numpy as np a=np.random.rand(5) print(a) [ 0.64061262 0.8451399 0.965673 0.89256687 0.48518743]print(a[-1]) ###取最后一個元素 [0.48518743]print(a[:-1]) ### 除了最后一個取全部 [ 0.64061262 0.8451399 0.965673 0.89256687]print(a[::-1]) ### 取從后向前(相反)的元素 [ 0.48518743 0.89256687 0.965673 0.8451399 0.64061262]print(a[2::-1]) ### 取從下標(biāo)為2的元素翻轉(zhuǎn)讀取 [ 0.965673 0.8451399 0.64061262]參考原文鏈接:python中[-1]、[:-1]、[::-1]、[n::-1]使用方法 - qq_21840201的博客 - CSDN博客
python p[:0]與p[:1]的區(qū)別
img.shape[:2]
img.shape[:2] 取彩色圖片的高、寬,如果img.shape[:3] 取彩色圖片的高、寬、通道
1:一般的數(shù)組如:【22,33】 shape是(2,):他表示他是一個一維數(shù)組,數(shù)組中有兩個元素;注意他和shape(2,1)的區(qū)別,他兩個不一樣。
2:[[22],[33]] 他的shape是(2,1),表示二維數(shù)組,每行有一個元素
3:[[22,33]] shape是(1,2) 他表示一個二維數(shù)組,每行有兩個元素
image.shape[0], 圖片垂直尺寸
image.shape[1], 圖片水平尺寸
image.shape[2], 圖片通道數(shù)
參考原文鏈接:shape 函數(shù),以及shape(2,)和shape(2,1)區(qū)別
區(qū)別 image.shape[0],image.shape[1],image.shape[2]
st = np.argsort(ys[:,2])
numpy.argsort(a, axis=-1, kind=’quicksort’, order=None)
功能: 將矩陣a按照axis排序,并返回排序后的下標(biāo)
參數(shù): a:輸入矩陣, axis:需要排序的維度
返回值: 輸出排序后的下標(biāo)
參考原文鏈接:numpy中實用但不常見的方法(3)np.argsort - cetrol_chen的博客 - CSDN博客
bot,top = np.clip([int(ys[:,2].min()-1),int(ys[:,2].max()+2)], 0, shape[0])
將[int(ys[:,2].min()-1),int(ys[:,2].max()+2)]范圍外的數(shù)強(qiáng)制轉(zhuǎn)化為[0, shape[0]]范圍內(nèi)的數(shù)
- def clip(a, a_min, a_max, out=None): 將數(shù)組a中的所有數(shù)限定到范圍a_min和a_max中,即az中所有比a_min小的數(shù)都會強(qiáng)制變?yōu)閍_min,a中所有比a_max大的數(shù)都會強(qiáng)制變?yōu)閍_max.
- 其中a_min和a_max可以為一個和a一樣大小的數(shù)組(列表也可以,只要是類似數(shù)組的結(jié)構(gòu)就是可行的),則數(shù)組中相應(yīng)位置的元素進(jìn)行比較。
- out 是可選項,表示把強(qiáng)制截取后的結(jié)果放到這個數(shù)組中,但是out中的數(shù)組必須和a形狀一樣
參考原文鏈接:np.clip截取函數(shù) - cloud&ken - 博客園
python中numpy模塊下的np.clip()的用法 - IT屆的小學(xué)生 - CSDN博客
idx = ys[:,:2].astype(np.int16)
使用方法:
- df.astype('數(shù)據(jù)類型') #改變整個df的數(shù)據(jù)類型
- df['列名'].astype('數(shù)據(jù)類型') #僅改變某一列的數(shù)據(jù)類型
num=num.astype('str')#將整個dataframe都轉(zhuǎn)換為str類型
參考原文鏈接:python強(qiáng)制類型轉(zhuǎn)換astype - weixin_42036641的博客 - CSDN博客
ys.append((i,j,polys[i][j][1]))
numpy.append(arr, values, axis=None):
簡答來說,就是arr和values會重新組合成一個新的數(shù)組,做為返回值。而axis是一個可選的值
當(dāng)axis無定義時,是橫向加成,返回總是為一維數(shù)組!
Examples-------->>> np.append([1, 2, 3], [[4, 5, 6], [7, 8, 9]])array([1, 2, 3, 4, 5, 6, 7, 8, 9])
numpu.append(arr,values,axis=None)
將values插入到目標(biāo)arr的最后。
注意,這里values跟arr應(yīng)該為相同維度的向量
參考原文鏈接:numpy的numpy.delete()/insert()/append()函數(shù) - 開貳錘 - CSDN博客
對numpy.append()里的axis的用法詳解_python_腳本之家
ys = np.array(ys)
參考原文鏈接:python中數(shù)組(numpy.array)的基本操作 - fu6543210的博客 - CSDN博客
np.array(rst).T
轉(zhuǎn)置函數(shù).T,將原shape為(n,m)的數(shù)組轉(zhuǎn)置為(m,n),一維數(shù)組轉(zhuǎn)置不變
參考原文鏈接:python數(shù)據(jù)分析(3)--numpy數(shù)組形狀轉(zhuǎn)換.T/.reshape()/.resize() - weixin_42695959的博客 - CSDN博客
rst.extend([(x,y) for x in range(max(x1,o[0]), min(x2, shape[2]))])
1. 列表可包含任何數(shù)據(jù)類型的元素,單個列表中的元素?zé)o須全為同一類型。
2. append() 方法向列表的尾部添加一個新的元素。只接受一個參數(shù)。
3. extend()方法只接受一個列表作為參數(shù),并將該參數(shù)的每個元素都添加到原有的列表中。
數(shù)組拼接方法一
思路:首先將數(shù)組轉(zhuǎn)成列表,然后利用列表的拼接函數(shù)append()、extend()等進(jìn)行拼接處理,最后將列表轉(zhuǎn)成數(shù)組。
示例1:
>>> import numpy as np
>>> a=np.array([1,2,5])
>>> b=np.array([10,12,15])
>>> a_list=list(a)
>>> b_list=list(b)
>>> a_list.extend(b_list)
>>> a_list
[1, 2, 5, 10, 12, 15]
>>> a=np.array(a_list)
>>> a
array([ 1, 2, 5, 10, 12, 15])
該方法只適用于簡單的一維數(shù)組拼接,由于轉(zhuǎn)換過程很耗時間,對于大量數(shù)據(jù)的拼接一般不建議使用。
參考原文鏈接:numpy數(shù)組拼接方法介紹 - zyl1042635242的專欄 - CSDN博客
python中的append的用法 - m0_37870649的博客 - CSDN博客
for i in zip(rs[::2],rs[1::2])
參考原文鏈接:Python zip() 函數(shù) | 菜鳥教程
源碼快查
# -*- coding: utf-8 -*- """ Created on Mon Nov 14 17:40:41 2016 @author: yxl """ from __future__ import absolute_import import numpy as npdef f(p1,p2,y):if abs(p1[1]-y) > abs(p2[1]-y):p1,p2 = p2,p1k =1.0* (p1[1]-y)/(y-p2[1])return round((p1[0]+k*p2[0])/(1+k),4)def scan(polys, idx, ys, st, y, cur, buf):while cur<len(idx) and ys[st[cur]]<=y:c = idx[st[cur]]poly = polys[c[0]]for i in (c[0], (c[1]-1)%len(poly)), tuple(c):if i in buf:buf.remove(i)else: buf.append(i)cur += 1return curdef roots(polys, buf, y):rs = []for i in buf:poly = polys[i[0]]i1,i2 = i, (i[0],(i[1]+1)%len(poly))rs.append(f(poly[i1[1]], poly[i2[1]],y))return np.sort(rs)def fill(plgs, img, color = 1, o=(0,0)):polys = [np.array(plg[:-1])-0.5 for plg in plgs]shape = img.shape[:2]ys = []for i in range(len(polys)):for j in range(len(polys[i])):ys.append((i,j,polys[i][j][1]))ys = np.array(ys)st = np.argsort(ys[:,2])buf, rst, cur = [], [], 0bot,top = np.clip([int(ys[:,2].min()-1),int(ys[:,2].max()+2)], 0, shape[0])idx = ys[:,:2].astype(np.int16)for y in range(bot, top):cur = scan(polys, idx, ys[:,2], st, y, cur, buf)rs = roots(polys, buf, y)for i in zip(rs[::2],rs[1::2]):x1, x2 = int(np.ceil(i[0])), int(np.floor(i[1])+2)x1, x2 = max(x1,0), min(x2, shape[1])if x1 >= shape[1] or x2 < 0: continue#rst.extend([(x,y) for x in range(max(x1,o[0]), min(x2, shape[2]))])img[y,x1:x2] = colorreturn np.array(rst).Tif __name__ == '__main__':import matplotlib.pyplot as pltfrom time import time# pg.shape = (1,4,2)pg = np.array([[(-300,-100),(1100,100),(400,1300),(100,100)]])# img.shape = (1000, 500)img = np.zeros((1000, 500))a = time()rc= fill(pg, img)print(time() - a)plt.imshow(img, interpolation='nearest',cmap='gray')plt.show()print("Done!")總結(jié)
以上是生活随笔為你收集整理的java绘制图形代码_ImagePy_Learn | 图形学绘制代码学习:core\draw\polygonfill.py的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux创建vnc服务器,五步建立一个
- 下一篇: mysql api 连接池_Spring