opencv-图像基本操作

参考:

1、http://docs.opencv.org/3.3.0/  官方文档api

2、http://docs.opencv.org/3.3.0/d6/d00/tutorial_py_root.html 官方英文教程

3、https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html

4、https://github.com/makelove/OpenCV-Python-Tutorial# 进阶教程

5、https://docs.opencv.org/3.3.0/index.html  官方英文教程

6、https://github.com/abidrahmank/OpenCV2-Python-Tutorials

7、https://www.learnopencv.com/

8、http://answers.opencv.org/questions/ OpenCV论坛


注:安装的版本 opencv_python-3.3.0-cp36-cp36m-win_amd64.whl



参考:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html




图像基本操作

目标

Learn to:

  • Access pixel values and modify them
  • Access image properties
  • Setting Region of Image (ROI)
  • Splitting and Merging images

访问和修改像素值

import cv2
import numpy as np

img = cv2.imread('messi5.jpg')

px = img[100,100]
print(px)
# [157 166 200]

# accessing only blue pixel
blue = img[100,100,0]
print(blue)
# 157

img[100,100] = [255,255,255]
print(img[100,100])
# [255 255 255]


# So if you want to access all B,G,R values, 
# you need to call array.item() separately for all
# accessing RED value
img.item(10,10,2) # 只能获取单个像素点的像素值
# 59

# modifying RED value
img.itemset((10,10,2),100) # 修改像素值
img.item(10,10,2)
# 100



访问图像属性

Image properties include number of rows, columns and channels, type of image data, number of pixels etc.

print img.shape
# (342, 548, 3)

print img.size
# 562248

print img.dtype
# uint8

图像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]
img[273:333, 100:160] = ball


分割和合并图像通道

import cv2
import numpy as np

img = cv2.imread('messi5.jpg')

b,g,r = cv2.split(img)
img = cv2.merge((b,g,r))

# or
b=img[:,:,0]
g=img[:,:,1]
r=img[:,:,2]
img2 = cv2.merge((b,g,r))

cv2.imshow('img',img)
cv2.imshow('img2',img2)

cv2.waitKey(0)
cv2.destroyAllWindows()
注: cv2.split()  is acostly operation (in terms of time), so only use it if necessary. Numpy indexing is much more efficient and should be used if possible.



制作边框图像(填充)

cv2.copyMakeBorder()
  • src - input image
  • topbottomleftright - border width in number of pixels in corresponding directions
  • 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

import cv2
import numpy as np
from matplotlib import pyplot as plt

BLUE = [255,0,0]

img1 = cv2.imread('opencv_logo.png')

replicate = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REPLICATE)
reflect = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_WRAP)
constant= cv2.copyMakeBorder(img1,10,10,10,10,cv2.BORDER_CONSTANT,value=BLUE)

plt.subplot(231),plt.imshow(img1,'gray'),plt.title('ORIGINAL')
plt.subplot(232),plt.imshow(replicate,'gray'),plt.title('REPLICATE')
plt.subplot(233),plt.imshow(reflect,'gray'),plt.title('REFLECT')
plt.subplot(234),plt.imshow(reflect101,'gray'),plt.title('REFLECT_101')
plt.subplot(235),plt.imshow(wrap,'gray'),plt.title('WRAP')
plt.subplot(236),plt.imshow(constant,'gray'),plt.title('CONSTANT')

plt.show()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值