

<?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>image &#8211; Max的程式語言筆記</title>
	<atom:link href="https://stackoverflow.max-everyday.com/tag/image/feed/" rel="self" type="application/rss+xml" />
	<link>https://stackoverflow.max-everyday.com</link>
	<description>我要當一個豬頭，快樂過每一天</description>
	<lastBuildDate>Mon, 12 Jan 2026 03:38:33 +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>image &#8211; Max的程式語言筆記</title>
	<link>https://stackoverflow.max-everyday.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>zi2zi 訓練資料去除空白區域對訓練的影響</title>
		<link>https://stackoverflow.max-everyday.com/2025/02/zi2zi-train-image-remove-padding/</link>
					<comments>https://stackoverflow.max-everyday.com/2025/02/zi2zi-train-image-remove-padding/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 19 Feb 2025 07:19:06 +0000</pubDate>
				<category><![CDATA[機器學習筆記]]></category>
		<category><![CDATA[image]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=6314</guid>

					<description><![CDATA[在 zi2zi-pytorch 的版本, 有分別...]]></description>
										<content:encoded><![CDATA[
<p>在 zi2zi-pytorch 的版本, 有分別把 source font 與 target font 把空白區域移掉後, 再進行訓練, 理論上是更有效率沒錯, 排除掉空白區域, 在相同的解析度下, 是可以取得更多細節, 實際上以為會遇到縮放的問題, 實際測雖然訓練的資料是放大的, 在推論時輸入的資料是縮小的, 實際上推論結果是正確的大小.</p>


<div class="wp-block-image">
<figure class="aligncenter size-full"><img fetchpriority="high" decoding="async" width="532" height="547" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2025/02/2025-02-19_14-58_6a.jpg?v=1739949394" alt="" class="wp-image-6316"/></figure>
</div>


<p>以這個例子來說, 藍色是被放大的訓練資料, 紅色是實際要學習的 target font. 黑色是推論結果. </p>



<p>排除掉空白區域</p>



<ul class="wp-block-list">
<li>優點: 可以處理部份文字會在框線外的問題, </li>



<li>缺點: 是會失去空間資訊, 也可能遇到 source font 與 target font 比例互相沖突的字, 但機率非常非常的低, 例如: 作者心血來潮, 把某一個特徵(規則)之外讓字填滿可視區域, 但在思源黑體這個字, 只占畫面中宮的一小部份.</li>
</ul>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2025/02/zi2zi-train-image-remove-padding/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>ImageFilter.GaussianBlur(radius=sigma) 對訓練資料的影響</title>
		<link>https://stackoverflow.max-everyday.com/2025/02/image-filter-gaussian-blur-radius-sigma/</link>
					<comments>https://stackoverflow.max-everyday.com/2025/02/image-filter-gaussian-blur-radius-sigma/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Sun, 16 Feb 2025 15:18:06 +0000</pubDate>
				<category><![CDATA[機器學習筆記]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=6268</guid>

					<description><![CDATA[在 zi2zi pytorch 裡, 有 20%...]]></description>
										<content:encoded><![CDATA[
<p>在 zi2zi pytorch 裡, 有 20% 的機率會對訓練資料套用 blur 效果, 接著有 3種radius 分別是 [1, 1.5, 2], 有 1/15  機率讓學習資料套用到 blur + radius=2, 這個情況對資料有什麼影響? 用這張圖來說明:</p>



<figure class="wp-block-image size-large"><img decoding="async" width="1024" height="285" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2025/02/0_00032_0-tile-1024x285.png?v=1739717709" alt="" class="wp-image-6269" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2025/02/0_00032_0-tile-1024x285.png?v=1739717709 1024w, https://stackoverflow.max-everyday.com/wp-content/uploads/2025/02/0_00032_0-tile-600x167.png?v=1739717709 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2025/02/0_00032_0-tile-768x214.png?v=1739717709 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2025/02/0_00032_0-tile.png?v=1739717709 1099w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>說明: </p>



<ul class="wp-block-list">
<li>左圖裡的2個小圖, 分別是ZenMaruGothic-Regular style 與思源黑體 demi-light style.</li>



<li>left 是 80% 機率, 不做 blur, </li>



<li>right, 約 7% 機率 blur 產生的暈染效果, 讓原本沒有交叉的線條相連, L 形的直角變成圓角.</li>



<li>blur 優點就是反鋸齒, 缺點就是暈染.</li>
</ul>



<p>程式碼: dataset.py</p>



<pre class="wp-block-code"><code>if self.blur and random.random() &gt; 0.8:
    sigma_list = &#91;1, 1.5, 2]
    sigma = random.choice(sigma_list)
    img_A = img_A.filter(ImageFilter.GaussianBlur(radius=sigma))
    img_B = img_B.filter(ImageFilter.GaussianBlur(radius=sigma))
</code></pre>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>除此之外, blur 還會讓圓形筆觸有可能讓筆頭的弧度變的扁平, 例如, 原本可能是 round=10 的曲度, 有可能變成 round=8</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2025/02/image-filter-gaussian-blur-radius-sigma/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>檔案轉換為svg比較</title>
		<link>https://stackoverflow.max-everyday.com/2022/05/convert-to-svg-potrace/</link>
					<comments>https://stackoverflow.max-everyday.com/2022/05/convert-to-svg-potrace/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 25 May 2022 18:39:39 +0000</pubDate>
				<category><![CDATA[電腦相關應用]]></category>
		<category><![CDATA[image]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=4033</guid>

					<description><![CDATA[原始的圖片： 轉成點陣圖，感覺是滿正常的： 再轉...]]></description>
										<content:encoded><![CDATA[
<p>原始的圖片：</p>



<figure class="wp-block-image size-full"><img decoding="async" width="786" height="785" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.49-AM.png" alt="" class="wp-image-4036" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.49-AM.png?v=1653501307 786w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.49-AM-600x600.png?v=1653501307 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.49-AM-300x300.png?v=1653501307 300w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.49-AM-768x767.png?v=1653501307 768w" sizes="(max-width: 786px) 100vw, 786px" /></figure>



<p>轉成點陣圖，感覺是滿正常的：</p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="966" height="1024" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.55.58-AM-966x1024.png?v=1653501392" alt="" class="wp-image-4037" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.55.58-AM-966x1024.png?v=1653501392 966w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.55.58-AM-566x600.png?v=1653501392 566w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.55.58-AM-768x814.png?v=1653501392 768w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.55.58-AM.png?v=1653501392 1040w" sizes="(max-width: 966px) 100vw, 966px" /></figure>



<p></p>



<p>再轉回向量，如果是使用 potrace:<br><a href="http://potrace.sourceforge.net/#downloading">http://potrace.sourceforge.net/#downloading</a></p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="786" height="785" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.32-AM.png" alt="" class="wp-image-4035" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.32-AM.png?v=1653501265 786w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.32-AM-600x600.png?v=1653501265 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.32-AM-300x300.png?v=1653501265 300w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.45.32-AM-768x767.png?v=1653501265 768w" sizes="(max-width: 786px) 100vw, 786px" /></figure>



<p>測試了調整 potrace 的參數：</p>



<pre class="wp-block-code"><code>Algorithm options:
 -z, --turnpolicy &lt;policy&gt;  - how to resolve ambiguities in path decomposition
 -t, --turdsize &lt;n&gt;         - suppress speckles of up to this size (default 2)
 -a, --alphamax &lt;n&gt;         - corner threshold parameter (default 1)
 -n, --longcurve            - turn off curve optimization
 -O, --opttolerance &lt;n&gt;     - curve optimization tolerance (default 0.2)
 -u, --unit &lt;n&gt;             - quantize output to 1/unit pixels (default 10)
 -d, --debug &lt;n&gt;            - produce debugging output of type n (n=1,2,3)</code></pre>



<p>但都無法消除掉上面圖中造成的一個鈍角。</p>



<p>改用 VTracer:<br><a href="https://github.com/visioncortex/vtracer">https://github.com/visioncortex/vtracer</a></p>



<p>是解決掉較小的圓角時，會造成鈍角的問題，但也造成較大的曲線會造成新的鈍角：</p>



<figure class="wp-block-image size-full"><img loading="lazy" decoding="async" width="786" height="785" src="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.47.38-AM.png" alt="" class="wp-image-4038" srcset="https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.47.38-AM.png?v=1653501664 786w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.47.38-AM-600x600.png?v=1653501664 600w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.47.38-AM-300x300.png?v=1653501664 300w, https://stackoverflow.max-everyday.com/wp-content/uploads/2022/05/Screen-Shot-2022-05-26-at-1.47.38-AM-768x767.png?v=1653501664 768w" sizes="(max-width: 786px) 100vw, 786px" /></figure>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>暫時的解法，是使用肉眼先檢查一次，在FontForge 裡合併多個節點，就會變圓滑。</p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<p>除了 potrace, 還可以使用 autotrace, 下載用網址：<br><a href="https://github.com/autotrace/autotrace/releases">https://github.com/autotrace/autotrace/releases</a></p>



<p>在 Mac 先使用 terminal 建立 symbol link:</p>



<pre class="wp-block-preformatted">ln -s /Applications/autotrace.app/Contents/MacOS/autotrace /usr/local/bin</pre>



<p>接著就可以把 bmp 檔案轉成 svg</p>



<pre class="wp-block-code"><code>autotrace  --output-format svg --output-file output_filename.svg  input_filename.bmp</code></pre>



<p>測試結果，會吐血，potrace 產生的檔案 11KB, autotrace 產生出 270KB檔案，使用繪圖軟體開啟 .svg 檔案，似乎沒問題，但用 FontForge 開啟 .svg 滿滿的節點。@_@；  </p>



<hr class="wp-block-separator has-alpha-channel-opacity"/>



<h3 class="wp-block-heading">VTracer</h3>



<p>這是目前非常推薦的開源選擇，採用 Rust 語言編寫。它最大的特色是支援彩色影像追蹤，效果比 Potrace 豐富。它有網頁版可以直接上傳檔案測試，也有命令列工具可以整合到程式流程。它的線條處理相對平滑，適合處理標誌或插畫。</p>



<h3 class="wp-block-heading">AutoTrace</h3>



<p>這是一個歷史悠久的專案，功能比 Potrace 多。它支援彩色追蹤，並且可以設定更多參數來調整曲線的精密度。不過這個專案更新頻率較低，在某些現代系統上的安裝過程可能會比較繁瑣。如果你覺得 Potrace 轉出來的線條太生硬，可以試試這款的演算法。</p>



<h3 class="wp-block-heading">Inkscape 內建工具</h3>



<p>Inkscape 是一款免費的向量繪圖軟體，它其實內置了點陣圖轉向量的功能。在選單中找到路徑下的描繪點陣圖即可。它背後整合了 Potrace，但也加入了多層掃描功能來處理彩色影像。對於不習慣寫指令的人來說，這是最直觀的替代方案。</p>



<h3 class="wp-block-heading">Vector Magic</h3>



<p>這是一個付費的商業軟體，也有提供線上轉換服務。雖然要收費，但它的自動識別與細節保留能力通常優於開源工具。它能自動清理雜訊並處理反鋸齒邊緣，產出的 SVG 路徑非常乾淨。如果你對精準度有極高要求，且預算允許，這通常是業界公認最強的方案。</p>



<h3 class="wp-block-heading">程式開發庫選項</h3>



<p>如果你是在寫程式，Python 有一個叫做 osra 的庫可以參考。另外還有一些基於深度學習的方案在 GitHub 上出現，例如利用 AI 進行向量化，但這類工具通常運算資源需求較高。</p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2022/05/convert-to-svg-potrace/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Python PIL: How to save cropped image?</title>
		<link>https://stackoverflow.max-everyday.com/2021/06/python-pil-how-to-save-cropped-image/</link>
					<comments>https://stackoverflow.max-everyday.com/2021/06/python-pil-how-to-save-cropped-image/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Wed, 09 Jun 2021 18:13:52 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=3792</guid>

					<description><![CDATA[需要取得圖片中的某一個區塊，想要剪裁圖片的範例如...]]></description>
										<content:encoded><![CDATA[
<p>需要取得圖片中的某一個區塊，想要剪裁圖片的範例如下：</p>



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

imageObject = Image.open(saved_image_path)
box = (x_offset, Y_offset, width, height)
crop = imageObject.crop(box)
crop.save(saved_image_path, format)</pre>



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



<p>資料來源：<br><a href="https://stackoverflow.com/questions/6456479/python-pil-how-to-save-cropped-image">https://stackoverflow.com/questions/6456479/python-pil-how-to-save-cropped-image</a></p>



<p>Python Pillow &#8211; Cropping an Image<br><a href="https://www.tutorialspoint.com/python_pillow/python_pillow_cropping_an_image.htm">https://www.tutorialspoint.com/python_pillow/python_pillow_cropping_an_image.htm</a></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2021/06/python-pil-how-to-save-cropped-image/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>[PyTorch] output with shape [1, 28, 28] doesn&#8217;t match the broadcast shape [3, 28, 28]</title>
		<link>https://stackoverflow.max-everyday.com/2021/06/pytorch-output-with-shape-1-28-28-doesnt-match-the-broadcast-shape-3-28-28/</link>
					<comments>https://stackoverflow.max-everyday.com/2021/06/pytorch-output-with-shape-1-28-28-doesnt-match-the-broadcast-shape-3-28-28/#respond</comments>
		
		<dc:creator><![CDATA[max-stackoverflow]]></dc:creator>
		<pubDate>Thu, 03 Jun 2021 20:42:56 +0000</pubDate>
				<category><![CDATA[Python筆記]]></category>
		<category><![CDATA[機器學習筆記]]></category>
		<category><![CDATA[colab]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[Python]]></category>
		<guid isPermaLink="false">https://stackoverflow.max-everyday.com/?p=3766</guid>

					<description><![CDATA[會產生這一個錯誤，原因是圖片的格式造成，是彩色還...]]></description>
										<content:encoded><![CDATA[
<p>會產生這一個錯誤，原因是圖片的格式造成，是彩色還是灰階（黑白）圖片。</p>



<p>Let me clarify, if the img has three channels, you should have three number for mean, for example, img is RGB, mean is [0.5, 0.5, 0.5], the normalize result is R * 0.5, G * 0.5, B * 0.5. If img is grey type that only one channel, so mean should be [0.5], the normalize result is R * 0.5</p>



<p>資料來源：<br><a href="https://github.com/yunjey/pytorch-tutorial/issues/161">https://github.com/yunjey/pytorch-tutorial/issues/161</a></p>



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



<p>解法很多，需要配合程式變成彩色，或修改程式的流程。如果是在python 裡使用 PIL 讀取圖片，轉換方式如下：</p>



<pre class="wp-block-code"><code>img = Image.open(GIF_FILENAME)
rgbimg = Image.new("RGBA", img.size)
rgbimg.paste(img)
rgbimg.save('foo.jpg')</code></pre>



<p></p>
]]></content:encoded>
					
					<wfw:commentRss>https://stackoverflow.max-everyday.com/2021/06/pytorch-output-with-shape-1-28-28-doesnt-match-the-broadcast-shape-3-28-28/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<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 loading="lazy" 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 loading="lazy" 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 loading="lazy" 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>
	</channel>
</rss>
