我就廢話不多說了,直接上代碼吧!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
from numpy import * import numpy as np import cv2, os, math, os.path from PIL import Image base = "F:\\Spy_CNN\\pythonCode\\cvSPY\\cvTest\\LBP\\LBPImag3\\" base2 = "F:\\ProgrameCode\\FaceDataLib\\orl_Arry\\" imageOld = cv2.imread(base2 + "s1_1.bmp" ) image = cv2.cvtColor(imageOld,cv2.COLOR_BGR2GRAY) '''圖像的合成''' H,W = image.shape #(112, 92) kuai = 5 a = 1 #為了好調程序 maskx,masky = H / kuai,W / kuai #29 14 toImage = np.zeros((H + (kuai - 1 ) * a,W + (kuai - 1 ) * a)) toImage.shape #您畫畫圖,總結規律,就可以想出來了 for i in range (kuai): for j in range (kuai): '''float64 array''' faceZi = image[ int (i * maskx): int ((i + 1 ) * maskx), int (j * masky) : int ((j + 1 ) * masky)] cv2.imwrite(base + str (i) + str (j) + ".bmp" ,faceZi) # toImage[int(i*maskx)+a: int((i+1)*maskx)+a,int(j*masky)+a :int((j+1)*masky)+a]=faceZi toImage[ int (i * maskx) + i: int ((i + 1 ) * maskx) + i, int (j * masky) + j : int ((j + 1 ) * masky) + j] = faceZi cv2.imwrite(base + "toImage.bmp" ,toImage) #最簡單的,直接在灰度圖像上畫出一條黑線,只是會丟失一些像素的數據 for i in range ( 1 ,kuai): print (i) toImage[ int (i * maskx),:] = 0 toImage[:, int (i * masky)] = 0 cv2.imwrite(base + "toImage.bmp" ,toImage) |
以上這篇Python-numpy實現灰度圖像的分塊和合并方式就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/weixin_38671006/article/details/92787612