OpenCV之基本的图像操作函数使用及案例

1、Accessing and Modifying pixel values

函数:img[100,100]

Tips: Numpy is a optimized library for fast array calculations. So simply accessing each and every pixel values and modifying it will be very slow and it is discouraged.

 2、Accessing Image Properties

函数:img.shape

It returns a tuple of number of rows, columns and channels (if image is color)

函数:img.size

Total number of pixels

函数:img.dtype

Image datatype

Tips: img.dtype is very important while debugging because a large number of errors in OpenCV-Python code is caused by invalid datatype.

3、Image ROI

ROI is again obtained using Numpy indexing. Here I am selecting the ball and copying it to another region in the image:

ball = img[280:340, 330:390]    # ball原来的位置

img[273:333, 100:160] = ball    # 在新的位置生成一个ball

4、Splitting and Merging Image Channels

函数:cv2.split()

The function is a costly operation (in terms of time). So do it only if you need it. Otherwise go for Numpy indexing.

For Example

b,g,r = cv2.split(img)    # 将一张图片分割成BGR channels

img = cv2.merge((b,g,r))  # join these individual channels to BGR image

b = img[:,:,0]    # you want to make all the red pixels to zero, you need not split like this and put it equal to zero.

img[:,:,2] = 0    # You can simply use Numpy indexing, and that is more faster.

5、Making Borders for Images(Padding)

函数1: cv2.copyMakeBorder()

  • src - input image
  • topbottomleftright - border width in number of pixels in corresponding directions  第2,3,4,5这四个参数
  • borderType - Flag defining what kind of border to be added. It can be following types:
    • cv2.BORDER_CONSTANT - Adds a constant colored border. The value should be given as next argument.
      • cv2.BORDER_REFLECT - Border will be mirror reflection of the border elements, like this : fedcba|abcdefgh|hgfedcb
      • cv2.BORDER_REFLECT_101 or cv2.BORDER_DEFAULT - Same as above, but with a slight change, like this : gfedcb|abcdefgh|gfedcba
      • cv2.BORDER_REPLICATE - Last element is replicated throughout, like this: aaaaaa|abcdefgh|hhhhhhh
      • cv2.BORDER_WRAP - Can't explain, it will look like this : cdefgh|abcdefgh|abcdefg
  • value - Color of border if border type is cv2.BORDER_CONSTANT

6、Example Demo

# -*- coding: utf-8 -*-
import cv2
from matplotlib import pyplot as plt
RED = [255,0,0]        # 定义红颜色
BLUE = [0,255,0]       # 定义蓝颜色
GREEN = [0,0,255]      # 定义绿颜色
img1 = cv2.imread('opencv-logo.png')    # 加载一张图片
replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
replicate_2 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE,value=BLUE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect_2 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT,value=BLUE)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
reflect101_2 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101,value=BLUE)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
wrap_2 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP,value=BLUE)
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT)
constant_2= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)  # 只能用参数类型BORDER_CONSTANT,才能设置后面的值
plt.subplot(3,4,1),plt.imshow(img1,'gray'),plt.title('ORIGINAL')        # 函数subplot(3,4,1) 和subplot(341)一样,如果最后一个值在10之内
plt.subplot(3,4,2),plt.imshow(replicate,'gray'),plt.title('REPLICATE')  # 函数imshow()的第二个参数还不是很懂
plt.subplot(3,4,3),plt.imshow(replicate_2,'gray'),plt.title('REPLICATE_2')
plt.subplot(3,4,5),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(3,4,6),plt.imshow(reflect_2,'gray'),plt.title('REFLECT_2')
plt.subplot(3,4,7),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(3,4,8),plt.imshow(reflect101_2,'gray'),plt.title('REFLECT_101_2')
plt.subplot(3,4,9),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(3,4,10),plt.imshow(wrap_2,'gray'),plt.title('WRAP_2')
plt.subplot(3,4,11),plt.imshow(constant,'gray'),plt.title('CONSTANT')
plt.subplot(3,4,12),plt.imshow(constant_2,'gray'),plt.title('CONSTANT_2')
plt.show()

7、Reference Materials

资料1:http://docs.opencv.org/3.3.0/d3/df2/tutorial_py_basic_ops.html

资料2(copyMakeBorder()):

http://docs.opencv.org/3.3.0/d2/de8/group__core__array.html#ga2ac1049c2c3dd25c2b41bffe17658a36

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: