Restbits in Array-Spalten zufällig aufteilen
Verfasst: Samstag 6. Juli 2019, 17:42
Hallo!
Als Autodidakt zweifle ich, ob der Code effizient ist.
Bin für Kommentare zum unterstehenden Code dankbar
Erhy
Als Autodidakt zweifle ich, ob der Code effizient ist.
Bin für Kommentare zum unterstehenden Code dankbar
Erhy
Code: Alles auswählen
import numpy as np
import matplotlib.pyplot as plt
import imageio
filReadPathImage = r'C:\Users\gle\AppData\Local\Temp\DistDepMod\DispDepMod_2019-7-3_21_14_53.bmp'
picdata = np.asarray( imageio.imread(filReadPathImage)[:,:,:3] ) #not with transparency channel
picShape = picdata.shape
factpreci = 1 / 255.0
picw = picdata * factpreci #picw in range 0...1.0
#
# some calculations
# e.g.
refcolor = picw[ 0, 0 ]
picCalc = np.zeros_like(picw[:,:,0])
for colix in range(0, 3):
picCalc[:,:] += np.fabs( ( refcolor[colix] - picw[:,:,colix] ) )
# calculation done
# finishing of the modified image
arrInt = (np.clip( picCalc, 0.0 , 3.0) / factpreci ).astype(np.int)
picFinished = np.ma.empty_like(picdata) #so it is uint8
picModQuot, picModRema = np.divmod(arrInt, 3 )
picFinished[:,:,0] = picFinished[:,:,1] = picFinished[:,:,2] = picModQuot
#
# distribute the remainder randomly to avoid moiré
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# perhaps this can be coded in a smarter way:
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
picRandsIxsFi = np.random.randint(3, size=(picShape[0] * picShape[1]))
picRandsIxsFirst = picRandsIxsFi.reshape((picShape[0], picShape[1]))
# picRandsIxsFirst: values from 0 to 2
#pseudo random:
picRandsRotateAmount = picRandsIxsFi[-1::-1].reshape((picShape[0], picShape[1]))
twoPartsToAdd = np.array([[0,0],[1,0],[1,1]], dtype=np.uint8 ) # remainder with 0..2 is able to access the parts
rotatedIxs = np.array([[1,2],[2,0],[0,1]]) # rotation of indices 0,1,2 by 1, and by 2
for i in np.ndindex(picShape[0],picShape[1]):
r = picModRema[i[0],i[1]] #remainder
if r != 0 :
tLow = twoPartsToAdd[r,0] # the low significant remainder
if tLow != 0 :
colorIx = picRandsIxsFirst[i[0],i[1]]
picFinished[ i[0], i[1], colorIx ] += tLow
tHigh = twoPartsToAdd[r,1] # the high significant remainder
if tHigh != 0 :
rotAmount = picRandsRotateAmount[i[0],i[1]] // 2 # is not really random
rotIx = rotatedIxs[colorIx, rotAmount ]
picFinished[ i[0], i[1], rotIx ] += tHigh
#
# now result image picFinished was built
#
plt.imshow(picFinished)
plt.show()