灰度图像转换成彩色图像(伪

原理

b,g,r=cv2.split(img1)
w,h,c=img1.shape
x=[b,g,r]
img3=np.empty((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((m,n,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

cv2.imshow('img4',np.uint8(img4))
cv2.waitKey(0)
cv2.destroyAllWindows()

结果原图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值