ฝึกเล่นบาคาร่าฟรี
ข่าว

bayerbayer

เวลาปล่อย:2023-01-24    เรียกดู:

bayerBryce BayerBryce Bayer

rgbrgbbayerbayer bayerrg

Bayer, .raw, PS.jpeg, .rawbayer1/2G1/4R1/4B

Image SensorGRGRGR.../BGBGBG...RGBSensorRGB1/3

RGBBayerRGB2

RB

abRB

GG

bayerRGB3bayer03

BayerGRGRGR.../BGBGBG...RGBopenCVBGR10B10Bayer0Bayer

BayerBayer

//

-*- coding: utf-8 -*- Created on Sat Jul 6 11:10:12 2019 @author: Carl import cv2 import numpy as np def Bayer(img_rgb): bayer mask = np.zeros(img_rgb.shape) Blue channel mask[:, :, 0][1::2, ::2] = 1 Green channel mask[:, :, 1][::2, ::2] = 1 mask[:, :, 1][1::2, 1::2] = 1 Red channel mask[:, :, 2][::2, 1::2] = 1 bayer = mask * img_rgb convert tuple to list size = list(img_rgb.shape) remove the the last element size.pop() calculate the bayer image img = np.zeros(tuple(size)) for i in range(size[0]): for j in range(size[1]): img[i][j] = np.max((bayer[:,:,0][i][j], bayer[:,:,1][i][j], bayer[:,:,2][i][j])) img = img.astype(np.uint8) bayer = bayer.astype(np.uint8) return img, bayer def Interpolation(img): mask1 = np.zeros(img.shape) mask1[1::2, ::2] = 1 img_b = img * mask1 size = list(img_b.shape) flag = np.zeros(img_b.shape) flag[1::2, ::2] = 1 for i in range(1, size[0]-1): for j in range(1, size[1]-1): if(img_b[i][j] == 0): if(img_b[i][j-1] != 0): flag[i][j] = -1 elif(img_b[i][j-1] == 0 and img_b[i-1][j] != 0): flag[i][j] = 2 else: flag[i][j] = -2 else: flag[i][j] = 1 for i in range(size[0]): for j in range(size[1]): if(flag[i][j] == -1): img_b[i][j] = (img_b[i][j-1] + img_b[i][j+1]) / 2 if(flag[i][j] == 2): img_b[i][j] = (img_b[i-1][j] + img_b[i+1][j]) / 2 if(flag[i][j] == -2): img_b[i][j] = (img_b[i-1][j-1] + img_b[i-1][j+1] + img_b[i+1][j-1] + img_b[i+1][j+1]) / 4 for i in range(size[1]): if(img_b[0][i] == 0): img_b[0][i] = img_b[1][i] for i in range(size[0]): if(img_b[i][size[1]-1] == 0): img_b[i][size[1]-1] = img_b[i][size[1]-2] for i in range(size[0]): if(img_b[i][0] == 0): img_b[i][0] = (img_b[i-1][0] + img_b[i+1][0]) / 2 for i in range(size[1]): if(img_b[size[0]-1][i] == 0): img_b[size[0]-1][i] = (img_b[size[0]-1][i-1] + img_b[size[0]-1][i+1]) / 2 mask3 = np.zeros(img.shape) mask3[::2, 1::2] = 1 img_r = img * mask3 size = list(img_r.shape) flag = np.zeros(img_r.shape) flag[::2, 1::2] = 1 for i in range(1, size[0]-1): for j in range(1, size[1]-1): if(img_r[i][j] == 0): if(img_r[i][j-1] != 0): flag[i][j] = -1 elif(img_r[i-1][j] != 0): flag[i][j] = 2 else: flag[i][j] = -2 else: flag[i][j] = 1 for i in range(size[0]): for j in range(size[1]): if(flag[i][j] == -1): img_r[i][j] = (img_r[i][j-1] + img_r[i][j+1]) / 2 if(flag[i][j] == 2): img_r[i][j] = (img_r[i-1][j] + img_r[i+1][j]) / 2 if(flag[i][j] == -2): img_r[i][j] = (img_r[i-1][j-1] + img_r[i-1][j+1] + img_r[i+1][j-1] + img_r[i+1][j+1]) / 4 for i in range(size[0]): if(img_r[i][0] == 0): img_r[i][0] = img_r[i][1] for i in range(size[1]): if(img_r[size[0]-1][i] == 0): img_r[size[0]-1][i] = img_r[size[0]-2][i] for i in range(size[1]): if(img_r[0][i] == 0): img_r[0][i] = (img_r[0][i-1] + img_r[0][i+1]) / 2 for i in range(size[0]): if(img_r[i][size[0]-1] == 0): img_r[i][size[0]-1] = (img_r[i-1][size[0]-1] + img_r[i+1][size[0]-1]) / 2 mask2 = np.zeros(img.shape) mask2[::2, ::2] = 1 mask2[1::2, 1::2] = 1 img_g = img * mask2 size = list(img_g.shape) flag = np.zeros(img_g.shape) mask2[::2, ::2] = 1 mask2[1::2, 1::2] = 1 for i in range(2, size[0]-2): for j in range(2, size[1]-2): if(img_g[i][j] == 0): if(mask1[i][j] == 1): if(abs(img_b[i][j-2]-img_b[i][j+2]) abs(img_b[i][j+2]-img_b[i][j-2])): img_g[i][j] = (img_g[i-1][j] + img_g[i+1][j]) / 2 elif(abs(img_b[i][j-2]-img_b[i][j+2]) abs(img_b[i][j+2]-img_b[i][j-2])): img_g[i][j] = (img_g[i][j-1] + img_g[i+1][j]) / 2 else: img_g[i][j] = (img_g[i-1][j] + img_g[i+1][j] + img_g[i][j-1] + img_g[i][j+1]) / 4 if(mask3[i][j] == 1): if(abs(img_r[i][j-2]-img_r[i][j+2]) abs(img_r[i][j+2]-img_r[i][j-2])): img_g[i][j] = (img_g[i-1][j] + img_g[i+1][j]) / 2 elif(abs(img_r[i][j-2]-img_r[i][j+2]) abs(img_r[i][j+2]-img_r[i][j-2])): img_g[i][j] = (img_g[i][j-1] + img_g[i+1][j]) / 2 else: img_g[i][j] = (img_g[i-1][j] + img_g[i+1][j] + img_g[i][j-1] + img_g[i][j+1]) / 4 if(img_g[0][size[1]-1] == 0): img_g[0][size[1]-1] = (img_g[0][size[1]-2] + img_g[1][size[1]-1] ) / 2 if(img_g[size[0]-1][0] == 0): img_g[size[0]-1][0] = (img_g[size[0]-2][0] + img_g[size[0]-1][1]) / 2 if(img_g[size[0]-1][size[0]-1] == 0): img_g[size[0]-1][size[0]-1] = (img_g[size[0]-1][size[0]-2] + img_g[size[0]-2][size[0]-1]) / 2 for i in range(size[0]): if(img_g[i][0] == 0): img_g[i][0] = (img_g[i-1][0] + img_g[i+1][0] + img_g[i][1]) / 3 for i in range(size[1]): if(img_g[0][i] == 0): img_g[0][i] = (img_g[0][i-1] + img_g[0][i+1] + img_g[1][i]) / 3 for i in range(size[0]): if(img_g[i][size[1]-1] == 0): img_g[i][size[1]-1] = (img_g[i-1][size[1]-1] + img_g[i+1][size[1]-1] + img_g[i][size[1]-2]) / 3 for i in range(size[1]): if(img_g[size[0]-1][i] == 0): img_g[size[0]-1][i] = (img_g[size[0]-1][i-1] + img_g[size[0]-1][i+1] + img_g[size[0]-2][i]) / 3 for i in range(size[0]): for j in range(size[1]): if(img_g[i][j] == 0): img_g[i][j] = (img_g[i-1][j] + img_g[i+1][j] + img_g[i][j-1] + img_g[i][j+1]) / 4 return img_b, img_g, img_r if __name__ == __main__ : img_rgb = cv2.imread(r..\image\bg6.jpg) img, ori_img = Bayer(img_rgb) img_b, img_g, img_r = Interpolation(img) int_img = np.zeros(img_rgb.shape) int_img[:, :, 0] = img_b int_img[:, :, 1] = img_g int_img[:, :, 2] = img_r int_img = int_img.astype(np.uint8) cv2.imshow(ori, img_rgb) cv2.imshow(img2, ori_img) cv2.imwrite(gray.jpg, img) cv2.imwrite(ori_img.jpg, ori_img) cv2.imshow(img3, int_img) cv2.imwrite(int_img.jpg, int_img) hmerge = np.hstack((img_rgb, ori_img, int_img)) cv2.imshow(merge, hmerge) cv2.waitKey(0) cv2.destroyAllWindows()

Copyright © 2002-2022 Thb999 All Rights Reserved