数字图像处理汇总(1)

# #图像融合cv2.addWeightes()
# dst=cv2.addWeighted(img1,0.8,img2,0.4,0)
# #cv加法
# dst=cv2.add(img1,img2)
# #numpy加法
# # dst=img1+img2
# cv2.imshow('dst',dst)
# cv2.imshow('IR',img1)
# cv2.imshow('VIS',img2)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
#
# """我想把opencv标志放在图片上方。如果我添加两个图像,它将改变颜色。如果我混合它,我会得到一个透明的效果。但我希望它是不透明的。如果是矩形区域,
# 我可以像上一章那样使用ROI。但是opencv标志不是矩形的。因此,您可以使用以下按位操作来完成此操作:"""
# # I want to put logo on top-left corner, So I create a ROI
# rows,cols,channels = img2.shape
# roi = img1[0:rows, 0:cols ]
#
# # Now create a mask of logo and create its inverse mask also
# img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
# ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY)
# mask_inv = cv2.bitwise_not(mask)
#
# # Now black-out the area of logo in ROI
# img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)
#
# # Take only region of logo from logo image.
# img2_fg = cv2.bitwise_and(img2,img2,mask = mask)
#
# # Put logo in ROI and modify the main image
# dst = cv2.add(img1_bg,img2_fg)
# img1[0:rows, 0:cols ] = dst
#
# cv2.imshow('res',img1)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

"""实现图片线性相加"""
# def Addimage(img1,img2):
#     img1=np.asanyarray((img1))
#     img2=np.asanyarray((img2))
#     w,h,c=img1.shape
#     img3=np.zeros((w,h,c))
#     a=0.4
#     b=0.7
#     for i in range(w-1):
#         for j in range(h-1):
#             #print(img1[i,j,0])
#             img3[i,j,0]=float(img1[i,j,0]*a )+float(img2[i,j,0]*b)
#             img3[i, j,1] = float(img1[i, j, 1] * a) + float(img2[i, j, 1] * b)
#             img3[i, j,2] = float(img1[i, j, 2] * a) + float(img2[i, j, 2] * b)
#     cv2.imshow('img3',np.uint8(img3))
#     #cv2.imshow('img1',img1)
#     cv2.waitKey(0)
#     cv2.destroyAllWindows()
# Addimage(img1,img2)

b1,g1,r1=cv2.split(img1)
b2,g2,r2=cv2.split(img2)
w,h,c=img1.shape
#x=[b,g,r]
img3=np.zeros((w,h,c))
# for i in range(w):
#     for j in range(h):
#         img3[i,j,0]=img1[i,j,0]
# for i in range(c):
#     img3[:,:,i]=x[i]
# img3= cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)
#m,n=img3.shape
img4=np.zeros((w,h,3))

"""图像伪彩色操作"""
# for i in range(m):
#     for j in range(n):
#         #R
#         if img3[i,j]>=0 and img3[i,j]<=127:
#             img4[i,j,0]=0
#         elif img3[i,j]>=128 and img3[i,j]<=191:
#             img4[i,j,0]=4*img3[i,j]-510
#         elif img3[i,j]>=192 and img3[i,j]<=255:
#             img4[i,j,0]=255
#         #G
#         if img3[i,j]>=0 and img3[i,j]<=63:
#             img4[i,j,1]=254-4*img3[i,j]
#         elif img3[i,j]>=64 and img3[i,j]<=127:
#             img4[i,j,1]=4*img3[i,j]-254
#         elif img3[i,j]>=128 and img3[i,j]<=191:
#             img4[i,j,1]=255
#         elif img3[i,j]>=192 and img3[i,j]<=255:
#             img4[i,j,1]=1022-4*img3[i,j]
#         #B
#         if img3[i,j]>=0 and img3[i,j]<=63:
#             img4[i,j,2]=255
#         elif img3[i,j]>=64 and img3[i,j]<=127:
#             img4[i,j,2]=510-4*img3[i,j]
#         elif img3[i,j]>=128 and img3[i,j]<=255:
#             img4[i,j,2]=0
"""图像反转
S=L-1-R
S为反转后的像素值,L为灰度级最大范围,r为当前像素值
"""
# for i in range(m):
#     for j in range(n):
#         img4[i,j,0]=255-b[i,j]
#         img4[i, j, 1] = 255 - g[i, j]
#         img4[i, j, 2] = 255 - r[i, j]

"""对数变换S=Clog(1+r)"""
# c=40
# for i in range(m):
#     for j in range(n):
#         img4[i,j,0]=c*math.log(1+b[i,j])
#         img4[i, j, 1] = c*math.log(1+g[i,j])
#         img4[i, j, 2] = c*math.log(1+r[i,j])

"""幂次变换"""
# c=40
# a=0.5
# for i in range(m):
#     for j in range(n):
#         img4[i,j,0]=c*b[i,j]**a
#         img4[i, j, 1] = c*g[i,j]**a
#         img4[i, j, 2] = c*r[i,j]**a

"""直方图均衡化"""
# bh=cv2.equalizeHist(b)
# gh=cv2.equalizeHist(g)
# rh=cv2.equalizeHist(r)
# #合并每一个通道
# img4=cv2.merge((bh,gh,rh))

"""图像减法"""
#img4=cv2.merge((b1-b2,g1-g2,r1-r2))

"""平滑线性滤波,加权平均"""
# for i in range(1,w-1):
#     for j in range(1,h-1):
#         img4[i,j,0]=1/9*(b2[i-1,j-1]+b2[i-1,j]+b2[i-1,j+1]+b2[i,j-1]+b2[i,j]+b2[i,j+1]+b2[i+1,j-1]+b2[i+1,j]+b2[i+1,j+1])
#         img4[i, j, 1] = 1 / 9 * (g2[i - 1, j - 1] + g2[i - 1, j] + g2[i - 1, j + 1] + g2[i, j - 1] + \
#                                  g2[i, j] + g2[i, j + 1] + g2[i + 1, j -1] + g2[i + 1, j] + g2[i + 1, j + 1])
#         img4[i, j, 2] = 1 / 9 * (r2[i - 1, j - 1] + r2[i - 1, j] + r2[i - 1, j + 1] + r2[i, j - 1] + \
#                                  r2[i, j] + r2[i, j + 1] + r2[i + 1, j - 1] + r2[i + 1, j] + r2[i + 1, j + 1])

你可能感兴趣的:(python,OPENCV)