

<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>math &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/math/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Wed, 01 Apr 2020 18:44:20 +0000</lastBuildDate>
	<language>zh-TW</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://stackoverflow.max-everyday.com/wp-content/uploads/2017/02/max-stackoverflow-256.png</url>
	<title>math &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Numpy 基礎操作 reshape</title>
		<link>https://stackoverflow.max-everyday.com/2020/04/numpy-reshape/</link>
					<comments>https://stackoverflow.max-everyday.com/2020/04/numpy-reshape/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 01 Apr 2020 09:52:32 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=3366</guid>

					<description><![CDATA[在處理圖片時，針對某一個區塊的內容想用來判斷，傳...]]></description>
										<content:encoded><![CDATA[
<p>在處理圖片時，針對某一個區塊的內容想用來判斷，傳統的做法（例如：寫COBOL語言的世界）是自己寫迴圈，把要輸出的內容先Queue住，自己處理分段。現在有了numpy 居然一個 reshape 就做完了。@_@；好方便。</p>



<p>執行結果比較：</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="553" height="270" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/04/Screen-Shot-2020-04-01-at-17.45.02.png" alt="" class="wp-image-3368"/></figure>



<p>上面是自己做中斷，每6筆為一行。</p>



<p>下面是用 numpy 一行指令，把原本的資料切開。取得的結果是一樣的。</p>



<p>舊的寫法：</p>



<pre class="wp-block-preformatted">row = ""
idx=0
values_formated = []
for item in values:
    idx += 1
    flag = 1
    if item[0]==0:
        flag = 0
    row += " %d" % (flag)
    values_formated.append(flag)
    if idx % (diff_x+1) == 0:
        print((diff_x+1), row)
        row = ""</pre>



<hr class="wp-block-separator"/>



<p>新的寫法：</p>



<pre class="wp-block-preformatted">np_re = np.array(values_formated)
np2 = np_re.reshape([5,6])
print("np2:", np2)</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2020/04/numpy-reshape/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>處理圖片中的多角形</title>
		<link>https://stackoverflow.max-everyday.com/2020/04/get-pixel-values-inside-of-a-rectangle-within-an-image/</link>
					<comments>https://stackoverflow.max-everyday.com/2020/04/get-pixel-values-inside-of-a-rectangle-within-an-image/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 01 Apr 2020 02:52:29 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=3361</guid>

					<description><![CDATA[原本的需求是想知道圖片中一個4角形的內容值，剛好...]]></description>
										<content:encoded><![CDATA[
<p>原本的需求是想知道圖片中一個4角形的內容值，剛好看到一個實用的範例。似乎 OpenCV 和 numpy 變成必學的項目了，好多專案都在使用。</p>



<hr class="wp-block-separator"/>



<p>How to get pixel values inside of a rectangle within an image<br><a href="https://stackoverflow.com/questions/58790535/how-to-get-pixel-values-inside-of-a-rectangle-within-an-image">https://stackoverflow.com/questions/58790535/how-to-get-pixel-values-inside-of-a-rectangle-within-an-image</a></p>



<p>You can do that with Python/OpenCV by first drawing a white filled polygon on black background as a mask from your four points. The use np.where to locate and then print all the points in the image corresponding to the white pixels in the mask.</p>



<p>Input:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="256" height="256" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/04/Giazs.png" alt="" class="wp-image-3362"/></figure>



<pre class="wp-block-code"><code>import cv2
import numpy as np

# read image
image = cv2.imread('lena.png')

# create mask with zeros
mask = np.zeros((image.shape), dtype=np.uint8)

# define points (as small diamond shape)
pts = np.array( &#91;&#91;&#91;25,20],&#91;30,25],&#91;25,30],&#91;20,25]]], dtype=np.int32 )
cv2.fillPoly(mask, pts, (255,255,255) )

# get color values
values = image&#91;np.where((mask == (255,255,255)).all(axis=2))]
print(values)

# save mask
cv2.imwrite('diamond_mask.png', mask)

cv2.imshow('image', image)
cv2.imshow('mask', mask)
cv2.waitKey()</code></pre>



<p><br>Mask:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="256" height="256" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/04/MMsO0.png" alt="" class="wp-image-3363"/></figure>



<p>Results:</p>



<pre class="wp-block-code"><code> &#91;&#91;108 137 232]
 &#91;104 134 232]
 &#91;108 136 231]
 &#91;106 134 231]
 &#91;109 133 228]
 &#91;108 136 229]
 &#91;109 137 230]
 &#91;110 135 232]
 &#91;103 126 230]
 &#91;112 134 228]
 &#91;114 136 228]
 &#91;111 138 230]
 &#91;110 137 233]
 &#91;103 135 234]
 &#91;103 126 230]
 &#91;101 120 226]
 &#91;108 137 230]
 &#91;112 133 228]
 &#91;114 136 227]
 &#91;115 139 232]
 &#91;112 137 232]
 &#91;105 134 233]
 &#91;102 128 232]
 &#91; 98 119 226]
 &#91; 93 105 220]
 &#91;108 139 230]
 &#91;110 137 230]
 &#91;112 135 230]
 &#91;113 135 230]
 &#91;111 138 231]
 &#91;112 139 232]
 &#91;109 134 233]
 &#91;101 128 232]
 &#91;100 120 224]
 &#91; 90 104 221]
 &#91; 87  95 211]
 &#91;111 138 229]
 &#91;109 135 231]
 &#91;109 136 230]
 &#91;113 141 233]
 &#91;110 139 233]
 &#91;105 136 234]
 &#91;101 127 232]
 &#91; 95 117 225]
 &#91; 90 107 220]
 &#91;110 137 231]
 &#91;110 138 231]
 &#91;107 140 236]
 &#91;110 139 233]
 &#91;104 135 234]
 &#91;105 130 231]
 &#91; 92 116 227]
 &#91;114 141 234]
 &#91;112 142 235]
 &#91;111 140 235]
 &#91;111 138 234]
 &#91;110 132 232]
 &#91;114 140 234]
 &#91;108 140 233]
 &#91;107 134 233]
 &#91;107 140 235]]</code></pre>



<hr class="wp-block-separator"/>



<p>換成 PIL 寫法：</p>



<p>How to read an image file as ndarray</p>



<pre class="wp-block-preformatted">from PIL import Image
import numpy as np

im <strong>=</strong> np<strong>.</strong>array(Image<strong>.</strong>open('data/src/lena_square.png'))

print(im<strong>.</strong>dtype)
# uint8

print(im<strong>.</strong>ndim)
# 3

print(im<strong>.</strong>shape)
# (512, 512, 3)</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2020/04/get-pixel-values-inside-of-a-rectangle-within-an-image/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>FontForge 坐標系統轉換為圖片格式</title>
		<link>https://stackoverflow.max-everyday.com/2020/03/fontforge-coordinate-system-convert/</link>
					<comments>https://stackoverflow.max-everyday.com/2020/03/fontforge-coordinate-system-convert/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Tue, 31 Mar 2020 15:20:56 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[FontForge]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=3348</guid>

					<description><![CDATA[在 FontForge 裡的一個坐標，換成BMP...]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="975" height="889" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-31-at-23.11.51.png" alt="" class="wp-image-3350" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-31-at-23.11.51.png?v=1585667624 975w, https://stackoverflow.max-everyday.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-31-at-23.11.51-600x547.png?v=1585667624 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2020/03/Screen-Shot-2020-03-31-at-23.11.51-768x700.png?v=1585667624 768w" sizes="(max-width: 975px) 100vw, 975px" /></figure>



<p>在 FontForge 裡的一個坐標，換成BMP格式的檔案，新的坐標值如何轉換？</p>



<p>滿有趣的一個數學問題，第一次遇到。上圖裡 Y 軸是 790，在 bmp 系統是對到 90. fontforge Y 軸向下，數值會減少，但在 bmp 系統，Y軸向下，數值會增加。</p>



<p>解法是，把 bmp 系統推換回原點，再乘一個負數，換回去 bmp=0 對到 fontforge 裡對到的頂點。再把 bmp 系統裡的異動值，以 fontforge 的基準點做減少，就可以對應了。</p>



<p>例如：</p>



<pre class="wp-block-preformatted">def ff_y_to_bmp_y(y):
    return 880 + (y * -1)
</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2020/03/fontforge-coordinate-system-convert/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Python 判斷一群的點，是否往同一個方向</title>
		<link>https://stackoverflow.max-everyday.com/2020/03/python-check-same-direction/</link>
					<comments>https://stackoverflow.max-everyday.com/2020/03/python-check-same-direction/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Sun, 22 Mar 2020 03:42:19 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=3329</guid>

					<description><![CDATA[想知道一群的資料，是否都往同一個方向移動，解法：...]]></description>
										<content:encoded><![CDATA[
<p>想知道一群的資料，是否都往同一個方向移動，解法：</p>



<p></p>



<hr class="wp-block-separator"/>



<pre class="wp-block-preformatted">def average(lst): 
    return sum(lst) / len(lst) 

def is_same_direction_list(args,deviation=0):
    ret = True
    args_average=average(args)
    #print("args_average:", args_average)

    direction = -1
    if args[0] &lt;= args_average:
        direction = 1
    
    idx=0
    args_count = len(args)
    for item in args:
        idx+=1
        if idx == args_count:
            break

        if direction==1:
            if (args[idx]+deviation)&lt;item and (args[idx]-deviation)&lt;item:
                ret = False
                break
        else:
            if (args[idx]+deviation)&gt;item and (args[idx]-deviation)&gt;item:
                ret = False
                break

    return ret

def is_same_direction(*args,deviation=0):
    return is_same_direction_list(args,deviation=deviation)


print(&amp;quot;case 1:&amp;quot;, is_same_direction(10,20,30,40))
print(&amp;quot;case 2:&amp;quot;, is_same_direction(10,20,40,30))
print(&amp;quot;case 3:&amp;quot;, is_same_direction(40,30,20,10))

# allow small mistake.
print(&amp;quot;case 4:&amp;quot;, is_same_direction(10,20,33,30,deviation=3))
</pre>



<p>執行結果：</p>



<pre class="wp-block-preformatted"> case 1: True
 case 2: False
 case 3: True
 case 4: True</pre>



<p>成功讓 case 4 號變成 True, 允許小範圍的誤差. </p>



<hr class="wp-block-separator"/>



<h2 class="wp-block-heading">技術原理：</h2>



<p>Python不定長度參數<em>args,</em>*kwargs<br><a href="https://stackoverflow.max-everyday.com/2020/03/python-function-args-kwargs/">https://stackoverflow.max-everyday.com/2020/03/python-function-args-kwargs/</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2020/03/python-check-same-direction/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Python 找 2點間長度</title>
		<link>https://stackoverflow.max-everyday.com/2020/03/python-distance-between-two-points/</link>
					<comments>https://stackoverflow.max-everyday.com/2020/03/python-distance-between-two-points/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Sat, 21 Mar 2020 17:53:13 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">http://stackoverflow.max-everyday.com/?p=3319</guid>

					<description><![CDATA[距離公式：設A(X ,Y) ,B(R ,S)為平...]]></description>
										<content:encoded><![CDATA[
<p>距離公式：設A(X ,Y) ,B(R ,S)為平面上兩點，則距離長度</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="338" height="55" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2020/03/two-points-distance.gif" alt="" class="wp-image-3322"/></figure>



<p>換成python 寫法：</p>



<pre class="wp-block-preformatted">dist = sqrt( (x2 - x1)2 + (y2 - y1)2 )</pre>



<p>較佳寫法：</p>



<pre class="wp-block-preformatted">from math import hypot
dist = hypot(x2-x1, y2-y1)</pre>



<p>如果使用 (3,4) 會得到 5.0 </p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2020/03/python-distance-between-two-points/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
