Install Python and OpenCV in VS Code

  1. Install Python for Windows
  2. Select working dir
  3. Create a test.py file
  4. run command in VS Code terminal and select this working dir as a virtual path for python and dependencies
    py -3 -m venv .venv
    .venv\scripts\activate
  5. pip install opencv-python or pip install opencv-contrib-python
  6. pip install matplotlib
  7. pip install numpy
  8. pip install opencv-python
  9. pip install scipy
  10. run ‘play’ button at top right corner

Sample code:
import cv2
import numpy as np
from matplotlib import pyplot as plt

img = cv2.imread(‘gradient.png’,0)
ret,thresh1 = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO_INV)

titles = [‘Original Image’,’BINARY’,’BINARY_INV’,’TRUNC’,’TOZERO’,’TOZERO_INV’]
images = [img, thresh1, thresh2, thresh3, thresh4, thresh5]

for i in range(6):
plt.subplot(2,3,i+1),plt.imshow(images[i],’gray’)
plt.title(titles[i])
plt.xticks([]),plt.yticks([])

plt.show()

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: