OpenCV: Difference between revisions

From David's Wiki
Line 8: Line 8:
=== Free algorithms only ===
=== Free algorithms only ===
<pre>
<pre>
pip install opencv-python opencv-contrib-python
pip install opencv-python
</pre>
</pre>


=== Non-free Algorithms ===
=== Non-free Algorithms ===
Compile [https://github.com/opencv/opencv OpenCV] from source alongside [https://github.com/opencv/opencv_contrib OpenCV-contrib]
<pre>
pip install opencv-contrib-python
</pre>


You can also compile [https://github.com/opencv/opencv OpenCV] from source alongside [https://github.com/opencv/opencv_contrib OpenCV-contrib] if you want to do things the hard way
{{hidden | Compilation Instructions |
See [https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/ https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu] for instructions.
See [https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu/ https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu] for instructions.
<pre>
<pre>
Line 33: Line 37:
sudo ldconfig
sudo ldconfig
</pre>
</pre>
}}


==Usage==
==Usage==

Revision as of 12:21, 11 May 2020

OpenCV is a very popular computer vision and image processing library.
There are bindings for C++, Java, JavaScript, and Python

Installation

  • Download Python 3

Free algorithms only

pip install opencv-python

Non-free Algorithms

pip install opencv-contrib-python

You can also compile OpenCV from source alongside OpenCV-contrib if you want to do things the hard way

Compilation Instructions

See https://www.pyimagesearch.com/2018/08/15/how-to-install-opencv-4-on-ubuntu for instructions.

cd ~
git clone [email protected]:opencv/opencv.git
git clone [email protected]:opencv/opencv_contrib.git
cmake -D CMAKE_BUILD_TYPE=RELEASE \
	-D CMAKE_INSTALL_PREFIX=/usr/local \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D INSTALL_C_EXAMPLES=OFF \
	-D OPENCV_ENABLE_NONFREE=ON \
	-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \
	-D PYTHON3_EXECUTABLE=~/anaconda3/envs/tf2/bin/python \
	-D PYTHON3_INCLUDE_DIRS=~/anaconda3/envs/tf2/python3.7m \
	-D PYTHON3_LIBRARIES=~/anaconda3/envs/tf2/lib/libpython3.7m.so \
	-D BUILD_EXAMPLES=ON ..
make -j16
sudo make install
sudo ldconfig

Usage

Getting Started

import cv2

# cv2.IMREAD_ANYCOLOR = 4
# cv2.IMREAD_ANYDEPTH = 2
# cv2.IMREAD_COLOR = 1
# cv2.IMREAD_GRAYSCALE = 0

# Use cv2.IMREAD_GRAYSCALE to read in grayscale
my_image = cv2.imread("my_image.png", cv2.IMREAD_GRAYSCALE)

# Perform some modification
# Do your machine learning here

# Write your output image
cv2.imwrite("my_modified_image.png", my_image)

Resizing an Image

Reference

import numpy as np
import cv2

img = cv2.imread('messi5.jpg')
new_img = cv2.resize(img, (500,200), interpolation = cv.INTER_CUBIC)

Video

Reading Video

video_capture = cv2.VideoCapture(path.join(videos_folder, video_filename))
if not video_capture.isOpened():
    print("Error opening video stream or file")
    sys.exit(0)

frame_num = 0
while video_capture.isOpened() and frame_num < 30:
    ret, frame = video_capture.read()
    if ret:
        cv2.imshow('Frame', frame)

        frame_num = frame_num + 1
        if cv2.waitKey(25) & 0xFF == ord('q'):
            break
    else:
        break
Parameters
# Resolution
width = int(video_capture.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video_capture.get(cv2.CAP_PROP_FRAME_HEIGHT))

total_frames = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT))
frame_pos = int(video_capture.get(cv2.CAP_PROP_POS_FRAMES))

Writing Video

C++ Video Write
Note that OpenCV does not handle audio.

output_video = cv2.VideoWriter()
codec = cv2.VideoWriter_fourcc(*"avc1")
fps = 15
image_size = (100, 100)
output_video.open("video_output.mp4", codec, fps, image_size, True)
if not output_video.isOpened():
    print("Error opening output video")

# Write all of your frames
# while have_frames:
#   output_video.write(my_frame)

# Release the video
output_video.release()

Resources