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()
灰度图像转换成彩色图像(伪
最新推荐文章于 2024-09-09 17:57:39 发布