Skip to content

Instantly share code, notes, and snippets.

@nhomble
Last active September 7, 2017 06:16
Show Gist options
  • Save nhomble/1ecea5155b6b99ebf541d72df61a513e to your computer and use it in GitHub Desktop.
Save nhomble/1ecea5155b6b99ebf541d72df61a513e to your computer and use it in GitHub Desktop.
denoise in real time
import cv2
import numpy as np
def get_img(cap):
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
return frame
def add_noise(img):
# http://docs.opencv.org/3.1.0/d5/d69/tutorial_py_non_local_means.html
img = [np.float64(i) for i in img]
noise = np.random.randn(*img[1].shape) * 10
noisy = [i + noise for i in img]
return [np.uint8(np.clip(i, 0, 255)) for i in noisy]
def stitch_imgs(one, two):
"""
Maybe there is a better way.. my thinking is
Matrices are defined like
[
[ .. ]
[ .. ]
]
so an append would add to the bottom of the image, but I want side-by-side. The transpose will make my rows columns
by definition so that I can do the append. The outer transpose is obviously to take my back to the beginning. The
axis variable came from:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.append.html
'cause I don't want to flatten
:param one:
:param two:
:return:
"""
one = add_noise(one)
return np.transpose(np.append(np.transpose(one), np.transpose(two), axis=0))
def main():
cap = cv2.VideoCapture(0)
while cv2.waitKey(1) & 0xFF != ord('q'):
img = get_img(cap)
denoised = cv2.fastNlMeansDenoising(img, None, 10, 3, 17)
cv2.imshow('frame', stitch_imgs(img, denoised))
cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment