CV


一、截取 ROI

ROI : region of interest

import cv2
img=cv2.imread('dj.jpg')

# 封装方法来显示图片
def cv_show(img):
    cv2.imshow('w title',img) 
    waitret = cv2.waitKey(2000) 
    print(waitret)
    cv2.destroyAllWindows()
apple = img[70:110, 120:160] # 前面是高,后面是宽;左上角为(0,0)
cv_show('dj', apple)


二、数值计算

1、+ / -

img_cat=cv2.imread('dj.jpg')
img_dog=cv2.imread('dj3.jpg')
img_cat2 = img_cat + 50 # 在原始图片上每个位置都加一个值;整体的shape 值不变;如果值超过 255,就会自动减去 255;
img_cat[:5,:,0]

array([[53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 53, 54, 54, 54,
            ...
            73, 73, 73, 73, 72, 72, 72, 72]], dtype=uint8)
cv_show('001',img_cat2)
img_cat2[:5,:,0]

array([[63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 63, 64, 64, 64,
            64, 64, 64, 64, 64, 64, 64, 64, 62, 63, 63, 64, 65, 66, 67, 67,
...
            80, 80, 80, 80, 81, 81, 81, 81, 83, 83, 83, 84, 85, 85, 86, 86,
            83, 83, 83, 83, 82, 82, 82, 82]], dtype=uint8)
#相当于% 256
(img_cat + img_cat2)[:5,:,0] 
array([[116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
        ...
        152, 152, 156, 156, 156, 158, 160, 160, 162, 162, 156, 156, 156,
        156, 154, 154, 154, 154]], dtype=uint8)

2、cv2.add 相加

# 使用 add 函数,如果越界了(超过255),则取255;
cv2.add(img_cat,img_cat2)[:5,:,0]
array([[116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116, 116,
        ...
        156, 154, 154, 154, 154]], dtype=uint8)

三、图像融合

图像相加 +

相加的对象,shape 值必须相同;不同的可以进行 resize 操作;

img1 = img_cat + img_dog
plt.imshow(img1)

<matplotlib.image.AxesImage at 0x7f9543ef5b90>

img_cat.shape
(200, 200, 3)

四、修改尺寸

1、目标值

img_dog = cv2.resize(img_dog, (400, 400))
img_dog.shape
# (400, 400, 3)

img_dog1 = cv2.imread('dj3.jpg')
cv_show('002',img_dog1)
plt.imshow(img_dog1)
<matplotlib.image.AxesImage at 0x7f95437acb50>

[图片上传失败...(image-ceac34-1608014860800)]


2、设置倍数

res = cv2.resize(img, (0, 0), fx=4, fy=4)
plt.imshow(res)
<matplotlib.image.AxesImage at 0x7f9542b92c50>
res = cv2.resize(img, (0, 0), fx=1, fy=3)  # 倍数
plt.imshow(res)
<matplotlib.image.AxesImage at 0x7f9543d649d0>

3、addWeighted 使用权重融合

设置权重
R = aX1 + bX2 + b

res = cv2.addWeighted(img_cat, 0.4, img_dog, 0.6, 0)
plt.imshow(res)

五、边界填充

在对图像进行变化的过程中,可能会将 外边界扩大一圈;
比如 卷积 中提取特征前,会增加一个 padding;

copyMakeBorder

  • BORDER_REPLICATE:复制法,也就是复制最边缘像素。
  • BORDER_REFLECT:反射法,对感兴趣的图像中的像素在两边进行复制例如:fedcba|abcdefgh|hgfedcb
  • BORDER_REFLECT_101:反射法,也就是以最边缘像素为轴,对称,gfedcb|abcdefgh|gfedcba (减少了临界的像素)
  • BORDER_WRAP:外包装法cdefgh|abcdefgh|abcdefg
  • BORDER_CONSTANT:常量法,常数值填充。
top_size,bottom_size,left_size,right_size = (50,50,50,50)

replicate = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, borderType=cv2.BORDER_REPLICATE)

reflect = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_REFLECT)

reflect101 = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_REFLECT_101)

wrap = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size, cv2.BORDER_WRAP)

constant = cv2.copyMakeBorder(img, top_size, bottom_size, left_size, right_size,cv2.BORDER_CONSTANT, value=0)


%matplotlib inline
import matplotlib.pyplot as plt

plt.subplot(231), plt.imshow(img, '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()
原文地址:https://www.cnblogs.com/fldev/p/14371196.html