Skip to content

Instantly share code, notes, and snippets.

@ritvikmath
Created December 6, 2019 21:40
Show Gist options
  • Select an option

  • Save ritvikmath/470d871eac8cbf8b68414e7503b14684 to your computer and use it in GitHub Desktop.

Select an option

Save ritvikmath/470d871eac8cbf8b68414e7503b14684 to your computer and use it in GitHub Desktop.
#create a copy of my grey image and just get first layer (because all layers are same)
copyImg = greyImage.copy()[:,:,0]
#get number of rows, columns in our image
numRows,numCols = copyImg.shape
#define how big we want our blurring box to be, the bigger the blurrier
boxSize = 31
#get half the size of the box
halfBoxSize = boxSize/2
#we can only blur pixels where the box can fit around them (i.e. the edges wont get blurred)
startRow = halfBoxSize
startCol = halfBoxSize
#loop over all the valid pixels in the image
for row in range(startRow, numRows-halfBoxSize):
for col in range(startCol, numCols-halfBoxSize):
#create the local box around a given pixel
localPixels = greyImage[row-halfBoxSize:row+halfBoxSize+1, col-halfBoxSize:col+halfBoxSize+1][:,:,0]
#take the mean of the pixels in the local box
blurredValue = np.mean(localPixels)
#set the new value at that pixel to be that blurred value
copyImg[row,col] = blurredValue
#reshape the flat image to give it a third dimension of 1
copyImg = copyImg.reshape([numRows, numCols, 1])
#stack 3 copies of the transformed image together to create the blurred image
blurredImage = np.concatenate([copyImg]*3, axis=2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment