官术网_书友最值得收藏!

Datasets and libraries required

We will be using a sample image for most of this task. However, you can try the code with any other image or also use a webcam to see live results. The libraries used in this chapter are OpenCV, NumPy, and matplotlib. Even if you are not acquainted with libraries, you can still understand the code and implement them. There are also remarks for special cases when using a Jupyter notebook for the code written here:

import numpy as np 
import matplotlib.pyplot as plt
import cv2
# With jupyter notebook uncomment below line
# %matplotlib inline
# This plots figures inside the notebook

The sample image used in this chapter can be loaded as follows:

# read an image 
img = cv2.imread('flower.png')

This image can be plotted either using OpenCV or matplotlib libraries. We will be using matplotlib for the majority of plots as this will be beneficial in plotting other kinds of data as well in later chapters. A plotting function for a colored image read in OpenCV is defined as follows:

def plot_cv_img(input_image):     
"""
Converts an image from BGR to RGB and plots
"""
# change color channels order for matplotlib
plt.imshow(cv2.cvtColor(input_image, cv2.COLOR_BGR2RGB))

# For easier view, turn off axis around image
plt.axis('off')
plt.show()

The previously read image can be plotted as follows:

The image in Python is a NumPy array, so all of the array operations are still valid in the image. For example, you can crop an image by using array slicing:

plot_cv_img(img[100:400, 100:400]) 

This results in the following:

主站蜘蛛池模板: 满洲里市| 仲巴县| 和政县| 南陵县| 阿拉尔市| 赤水市| 莆田市| 清镇市| 西丰县| 临颍县| 衡水市| 上犹县| 镇坪县| 讷河市| 星子县| 信阳市| 湘阴县| 阳山县| 崇信县| 泽普县| 邯郸市| 大城县| 江西省| 芜湖市| 彭阳县| 巧家县| 随州市| 石城县| 图片| 宜兰市| 团风县| 太和县| 龙井市| 长白| 盐源县| 平阴县| 海南省| 贺兰县| 鄢陵县| 探索| 三台县|