python知识:如何从图片的四周扩大一些尺寸
生活随笔
收集整理的這篇文章主要介紹了
python知识:如何从图片的四周扩大一些尺寸
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 問題描述:
?
如何將圖片原來尺寸適度外擴,或增加一個邊框尺寸,使得原圖產生邊框區域?如下列效果?
前提條件是務必操作簡單易實現。
增加邊框后圖片:
2 應用numpy的insert函數
采用numpy的insert函數,下面顯示該函數用法:
example:
1)不指定張量維度
a = np.array([[1, 1], [2, 2], [3, 3]]) >>> a array([[1, 1],[2, 2],[3, 3]]) >>> np.insert(a, 1, 5) array([1, 5, 1, ..., 2, 3, 3]) >>> np.insert(a, 1, 5, axis=1) array([[1, 5, 1],[2, 5, 2],[3, 5, 3]])此時在第1個元素位置插入5;總體元素排列順序為1,1,2,2,3,3.
2)指定坐標軸
a = np.array([[1, 1], [2, 2], [3, 3]]) np.insert(a, [1], [[1],[2],[3]], axis=1) array([[1, 1, 1],[2, 2, 2],[3, 3, 3]]) >>> np.array_equal(np.insert(a, 1, [1, 2, 3], axis=1), ... np.insert(a, [1], [[1],[2],[3]], axis=1)) True?3)平攤后
b = a.flatten() >>> b array([1, 1, 2, 2, 3, 3]) >>> np.insert(b, [2, 2], [5, 6]) array([1, 1, 5, ..., 2, 3, 3])4)切片?
>>> np.insert(b, slice(2, 4), [5, 6]) array([1, 1, 5, ..., 2, 3, 3])5)轉成浮點數?
>>> np.insert(b, [2, 2], [7.13, False]) # type casting array([1, 1, 7, ..., 2, 3, 3])6)指定行或列
>>> x = np.arange(8).reshape(2, 4) >>> idx = (1, 3) >>> np.insert(x, idx, 999, axis=1) array([[ 0, 999, 1, 2, 999, 3],[ 4, 999, 5, 6, 999, 7]])3? 程序代碼
import cv2 import numpy as np # 讀取圖片并轉至灰度模式 imagepath = 'huflv.jpg' img = cv2.imread(imagepath, 1) height,width,c = img.shape # print( img.shape) cv2.imshow("before",img) fiximg = np.insert(img, 50*[0,width] , (0,0,130), axis=1) newimg = np.insert(fiximg, 50*[0,height], (0,0,130), axis=0) cv2.imshow("before long",newimg) cv2.waitKey(0)總結
以上是生活随笔為你收集整理的python知识:如何从图片的四周扩大一些尺寸的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C#教程01:关于C#
- 下一篇: 基础数学:通俗解释,啥叫随机变量?