opencv (3) boundary filling, image fusion, image threshold processing

Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.


1. Boundary filling

  • Border filling is to fill the picture outwards in a certain way and enlarge the picture.
  • Because the color channel in opencv is not RGB but BGR, we need to use the segmentation mentioned before to reintegrate the color channel into RGB and then use the matplotlib library to draw it.
import cv2
import matplotlib.pyplot as plt
import numpy as np
# 导入图片
img = cv2.imread('cat.jpg')
img = img[200:600,200:700]
b,g,r = cv2.split(img)
img = cv2.merge((r,g,b))
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,borderType=cv2.BORDER_REFLECT)
reflect101 = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_REFLECT_101)
wrap = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_WRAP)
constant = cv2.copyMakeBorder(img,top_size,bottom_size,left_size,right_size,borderType=cv2.BORDER_CONSTANT)
# 分别展示看到不同的效果
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()
  • BORDER_REPLICATE: copy method, that is, copy the edge pixels
  • BORDER_REFLECT: Reflection method, copy the pixels in the image of interest on both sides
  • BORDER_REFLECT_101: Reflection method, that is, the edge pixel of the instrument is the axis, symmetrical
  • BORDER_WRAP: outer packaging method
  • BORDER_CONSTANT: constant method, constant value filling

The effects of each method are shown below:

Insert image description here

2. Image fusion

  • cv2.resize(img,(length,height))
  • cv2.addWeighted(img1,alpha1,img2,alpha2,b)
    • alpha is the weight, b is the bias term
    • The formula is img = a1 ∗ * img1 + a2∗ * img2 + b
    • form new images
  • When merging images, be sure to keep the size of the two images the same
# 展示图片函数
import cv2
import numpy as np
def show_img(name,img_path):
    cv2.imshow(name,img_path)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
import cv2
import matplotlib.pyplot as plt
import numpy as np
import show_img		#该包为自己写的展示图片功能
cat = cv2.imread('cat.jpg')
dog = cv2.imread('dog.jpg')
# print(cat+dog)	#查看两张图片的大小(不一样大会报错提示)
cat = cv2.resize(cat,(889,500))	#调整两张图片大小一致(将大图片改小)
# 图像融合
add_img = cv2.addWeighted(cat,0.4,dog,0.6,0)
show_img.show_img('add',add_img)	#展示图片(可自己写展示图片那几行代码)
  • Finally, cats and dogs were fused together in certain proportions.

Insert image description here

3. Image threshold

  • ret,dst = cv2.threshold(src,thresh,maxval,type)
    • src: Input image, only single-channel images can be input, usually grayscale images
    • dst: output graph
    • thresh: threshold
    • maxval: When the pixel exceeds the threshold (or is less than the threshold, depending on the type), the value assigned
    • type: The type of binary operation, including the following five types:
      • cv2.THRESH_BINARY: take maxval for the part exceeding the threshold, otherwise 0
      • cv2.THRESH_BINARY_INV: Inversion of the previous one
      • cv2.THRESH_TRUNC: The part greater than the threshold is set to the threshold, otherwise it remains unchanged
      • cv2.THRESH_TOZERO: The part greater than the threshold does not change, otherwise it is 0
      • cv2.THRESH_TOZERO_INV: Inversion of the previous one
import cv2
import show_img
import matplotlib.pyplot as plt
import numpy as np

cat = cv2.imread('../data/cat.jpg')
cat = cat[200:700,200:700,:]	#原图太大了,这里只展示部分效果
cat_gray = cv2.imread('../data/cat.jpg',cv2.IMREAD_GRAYSCALE) #变成灰色图
cat_gray = cat_gray[200:700,200:700]
# show_img.show_img('cat',cat)
#调整颜色通道
b,g,r = cv2.split(cat)
cat = cv2.merge((r,g,b))
#图像阈值处理
ret,thresh1 = cv2.threshold(cat_gray,127,255,cv2.THRESH_BINARY)
ret,thresh2 = cv2.threshold(cat_gray,127,255,cv2.THRESH_BINARY_INV)
ret,thresh3 = cv2.threshold(cat_gray,127,255,cv2.THRESH_TRUNC)
ret,thresh4 = cv2.threshold(cat_gray,127,255,cv2.THRESH_TOZERO)
ret,thresh5 = cv2.threshold(cat_gray,127,255,cv2.THRESH_TOZERO_INV)

titles = ['Original Image','BINARY','BINARY_INV','TRUNC','TOZERO','TOZERO_INV']
images = [cat,thresh1,thresh2,thresh3,thresh4,thresh5]
# 展示不同方法进行的不同处理
for i in range(6):
    plt.subplot(2,3,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()
  • Effect display (don’t forget that 0 is black and 255 is white)
    Insert image description here

Guess you like

Origin blog.csdn.net/weixin_73044854/article/details/134652956