Skip to content

Instantly share code, notes, and snippets.

@nhomble
Last active September 5, 2017 05:33
Show Gist options
  • Save nhomble/929b81c57183979ba2bc702c5a04e589 to your computer and use it in GitHub Desktop.
Save nhomble/929b81c57183979ba2bc702c5a04e589 to your computer and use it in GitHub Desktop.
computational photography discussion 2---wrap two opencv tutorials to grab an image from the webcam and denoise it
import cv2
from matplotlib import pyplot as plt
def before_after(bef, af):
"""
refactor out the matplot lib thing
"""
plt.subplot(121), plt.imshow(bef, 'gray')
plt.subplot(122), plt.imshow(af, 'gray')
plt.show()
def manually_get_img():
"""
http://docs.opencv.org/3.0-beta/doc/py_tutorials/py_gui/py_video_display/py_video_display.html
"""
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
return frame
def denoise_single(img):
"""
http://docs.opencv.org/3.1.0/d5/d69/tutorial_py_non_local_means.html
"""
dst = cv2.fastNlMeansDenoising(img, None, 4, 7, 35)
before_after(img, dst)
def main():
"""
The obvious place to search for opencv tutorials is to look right at the docs, https://docs.opencv.org.
Since we are in a class about computational photography, I decided to jump right into the computational photography
section (who does tutorials in order anyway).
The concept seems straightforward, we have an image and we want to smooth it out by removing noise. The concept of
noise in this case assumes white noise with mean 0. The idea is that if we had a video instead of just a single
image, the average of the same pixel across many frames would tend to the true value.
p0 = p_t + u0
p1 = p_t + u1
.
.
.
np = np_t + sum(u, 0, n)
p = p_t + sum(u, 0, n) / n
p = p_t + average(u, 0, n)
We said mean 0 so
p = p_t + 0
For this particular algorithm, we have just a single image. The underlying algorithm described here
http://www.ipol.im/pub/art/2011/bcm_nlm/
looks for nearby pixels or patches that are semi-local. I guess the local-ness depends on your choice of cost
function C(p) since depending on the noise level of the image you may need to do more work and expand your search
to greater lengths of the image. If you are doing the patch approach, you may also increase your patch size. At
the end, the smoothing is done on the similar pixel(s) by doing an average.
Basically, I combined the tutorial to pull an image from your webcam and to denoise it.
"""
img = manually_get_img()
denoise_single(img)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment