OpenCV & Python

Getting started with Computer Vision and Video in Python

Install OpenCV: <= Search for your particular environment, but this usually works:pip install opencv-python

What is it ?

The above script consists of creating a video capture and then feeding that into a loop where frames are read and displayed one by one with imshow, the conditional checks for the exit command (type q to exit), cap.release and cv2.destroyAllWindows outside of the loop then deal with final cleanup. The result should be a video feed with the default OpenCV UI: 
Artifacts and washed colors from gif compression.


The colorspace transformation happens in the line :gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY) and then gets shown in the imshow('frame',  gray) 

Transformations and Operations.

Note how we operate on each frame and then pass it to the next operation gray->denoised->thresholded->mirrored->imshow. The docs are a good place to start if you want to know what other operations you can do, but most likely you'll want to experiment on your own by stacking them or making your own, in any case here's what the above gives you:

Getting to those pixels.

print(frame.shape) >> Gives the matrix shape Color : (480, 640, 3)GRAY : (480, 640)B/W : (480, 640)Note that the color capture has 3 Dimensions or channels while the gray and Black & White have one, here's a zoomed in comparison with the channels overlaid and pixel values:

UI Integrations.

Here we simply add an empty callback function (you can move the loop logic here if you want to), create track-bars and then tie those values to an OpenCv function.For a full explanation see here: Changing the contrast and brightness of an image.
This might look complicated, but it is basically the same structure from all the previous examples, the only difference ( beyond having 4 mirrored images) is that the OpenCV loop and gui bits have been replaced by pygame ones...

Use in AI

This is still the same structure we've been using all along, there are 2 new things though, the first is the use of a circle detector cv2.HoughCircles and the second is the logic block that draws on top of the found circles with For a more detailed explanation see here: Hough Circle Transform

Companion Repo:

AI, Software Developer, Designer :

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store