예제 #1
0
def get_val_test_data(filelist, images):
    val_x = []
    val_y = []
    for i in range(30):
        img = images[i][:128, :128]
        bayer_img = mosaic(img)
        debayered_img = cv2.demosaicing(bayer_img.astype(np.uint8), cv2.COLOR_BayerBG2RGB)
        val_x.append(debayered_img / 255)
        val_y.append(img / 255)
    val_x = np.array(val_x)
    val_y = np.array(val_y)

    # test_x = []
    # test_y = []
    # for i in range(30):
    #     f = np.random.randint(0, len(filelist))
    #     if images[f].shape[0] < 1024 or images[f].shape[1] < 1024:
    #         continue
    #     img = images[f][:1024, :1024]
    #     bayer_img = mosaic(img)
    #     debayered_img = cv2.demosaicing(bayer_img.astype(np.uint8), cv2.COLOR_BayerBG2RGB)
    #     test_x.append(debayered_img / 255)
    #     test_y.append(img / 255)
    # test_x = np.array(test_x)
    # test_y = np.array(test_y)
    # print (val_x.shape, test_x.shape)
    return val_x, val_y
예제 #2
0
def Demosaic(B_b, pattern):
    B_b = B_b * 255
    B_b = B_b.astype(np.uint16)

    if pattern == 1:
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerGB2BGR)
    elif pattern == 2:
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerGR2BGR)
    elif pattern == 3:
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerBG2BGR)
    elif pattern == 4:
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerRG2BGR)
    elif pattern == 5:
        lin_rgb = B_b

    lin_rgb = lin_rgb[:, :, ::-1] / 255.
    return lin_rgb
예제 #3
0
def pnm_read(__full_path):
    im_rows = 2048
    im_cols = 2448
    im_size = im_rows * im_cols
    with open(__full_path) as raw_image:
        img = np.fromfile(raw_image, np.dtype(np.uint8), im_size).reshape(
            (im_rows, im_cols))
        colour = cv2.demosaicing(img, cv2.COLOR_BAYER_GR2RGBA)
        colour = cv2.cvtColor(colour, cv2.COLOR_RGBA2BGR)
        # colour = cv2.resize(colour, (1280, 1070))
    return colour
예제 #4
0
def Demosaic(B_b, pattern):
     
    B_b = B_b * 255
    B_b = B_b.astype(np.uint16)

    #print(B_b)
    if pattern == 1:
        #lin_rgb = colour_demosaicing.bayer.demosaicing.malvar2004(B_b)
        #lin_rgb = demosaicing_CFA_Bayer_Malvar2004(B_b) 
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerGB2BGR)
    elif pattern == 2:
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerGR2BGR)
    elif pattern == 3:
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerBG2BGR)
    elif pattern == 4:
        lin_rgb = cv2.demosaicing(B_b, cv2.COLOR_BayerRG2BGR)
    elif pattern == 5:
        lin_rgb = B_b

    lin_rgb = lin_rgb[:,:,::-1] / 255.  
    return lin_rgb
 def raw_to_8bit(img_counter, filename_raw, dir_img_out):
     with open(filename_raw, "rb") as raw_file:
         data_in = np.right_shift(
             np.frombuffer(raw_file.read(),
                           dtype=np.uint16).reshape(3000, 4096), 4)
     debayered = cv2.demosaicing(data_in, cv2.COLOR_BAYER_RG2BGR)
     raw_bytes = debayered.view(dtype=np.uint8).flatten().reshape((-1, 2))
     shp = debayered.shape
     data_out = raw_bytes[:, 0].reshape(shp)
     out_fn_4 = os.path.join(dir_img_out, f"{img_counter}_4_rgb_8bit.tif")
     if cv2.imwrite(out_fn_4, data_out):
         return True
     return False
예제 #6
0
def image_generator(filelist, images):
    h, w = 128, 128
    while 1:
        train_X = []
        train_y = []
        f = np.random.randint(0, len(filelist), 32)
        for i in f:
            img = images[i]
            x = np.random.randint(0, img.shape[1] - w)
            y = np.random.randint(0, img.shape[0] - h)
            train_y.append(img[y:y+h, x:x+w])
            bayer_img = mosaic(img[y:y+h, x:x+w])
            debayered_img = cv2.demosaicing(bayer_img.astype(np.uint8), cv2.COLOR_BayerBG2RGB)
            train_X.append(debayered_img)
        train_X = np.array(train_X)
        train_y = np.array(train_y)
        yield (train_X / 255, train_y / 255)
예제 #7
0
파일: bayer.py 프로젝트: martok/rpi-tc
    def demosaic_array(array: np.ndarray, bayer_order: int, transform: int):
        hflip = transform & (1 << 0)
        vflip = transform & (1 << 1)
        transpose = transform & (1 << 2)
        assert transpose == 0, "Transpose data unimplemented"

        # from sensor manual:
        # htrans | vtrans | readout order | pixel image is | bayer_order
        #     0         0            RGGB   upright        |           3
        #     1         0            GRBG   hflipped       |           2
        #     0         1            GBRG   vflipped       |           1
        #     1         1            BGGR   180°           |           0

        # graphic is wrong, pixel image is always additionally hflipped
        # or: header(GRBG)^header(3) = GBRG, but needs BGGR (additional hflip) to decode correctly

        return cv2.demosaicing(array,
                               BAYER2CV[bayer_order ^ (transform & 0b11) ^ 1],
                               None, 3)
예제 #8
0
def img_fn_to_img(img_filename,
                  bayer_mode,
                  resize_factor,
                  img_width=4096,
                  img_height=3000,
                  target_bitdepth=8):
    """
    makes an 8-bit downsampled image from the .bin passed as 1st argument
    """
    if bayer_mode == '12p':
        expected_file_size = (img_height * img_width * 12) // 8
    elif bayer_mode == '10p':
        expected_file_size = (img_height * img_width * 10) // 8
    else:
        expected_file_size = (img_height * img_width * 8) // 8
    while os.path.getsize(img_filename) != expected_file_size:
        print("file too small", img_filename, expected_file_size,
              os.path.getsize(img_filename))
        time.sleep(0.001)

    with open(img_filename, "rb") as fin:
        print(img_filename)
        data_array = np.frombuffer(fin.read(), dtype=np.uint8)

    if bayer_mode == '12p':
        data_array = np.right_shift(
            read_raw_12p(data_array),
            4)  #only keep lowest 8 bits for compatibility with PIL
        data_array = data_array.view(dtype=np.uint8)[0::2]
    elif bayer_mode == '10p':
        data_array = np.right_shift(read_raw_10p(data_array), 2)
        data_array = data_array.view(dtype=np.uint8)[0::2]

    image = cv2.demosaicing(data_array.reshape((img_height, img_width)),
                            cv2.COLOR_BAYER_RG2BGR_EA)
    image = cv2.resize(image, (0, 0), fx=resize_factor, fy=resize_factor)
    histo_plot = image_to_histogram(image, g_hist_width, g_hist_height, 100)
    image_tk = ImageTk.PhotoImage(Image.fromarray(image))
    #image = (image/3).sum(axis=-1).astype(np.uint8) ##TEMP
    #image_tk = ImageTk.PhotoImage(Image.fromarray(image, "L"))
    histo_plot_tk = ImageTk.PhotoImage(Image.fromarray(histo_plot))
    return image_tk, histo_plot_tk
예제 #9
0
def img_post_processing(img):

    # Adjust Image intensity [0-255]

    width = img.shape[1]
    height = img.shape[0]
    R = img[:,:,0]
    G=img[:,:,1]
    B=img[:,:,2]
    R= imadjust(R)
    G= imadjust(G)
    B= imadjust(B)
    # ***White balance***
    rgb_med = [np.mean(R), np.mean(G), np.mean(B)]
    rgb_scale = np.max(rgb_med)/rgb_med
    # Scale each color channel, to have the same median.
    R = R*rgb_scale[0]
    G = G*rgb_scale[1]
    B = B * rgb_scale[2]

    # ***restore bayer mosaic BGGR***
    I =np.zeros((height*2,width*2))
    I[0:height*2:2, 0:width*2:2] = B
    I[0:height* 2:2, 1:width* 2:2] = G
    I[1:height* 2:2, 1:width* 2:2] = R
    # image interpolation
    T = cv2.resize(G, (2*width,2*height),interpolation=cv2.INTER_CUBIC)
    I[1:height * 2:2, 0:width * 2:2] = T[1:height * 2:2, 0:width * 2:2]
    print ("image interpolation ", T.shape)

    I =  np.clip(I,0, 1)
    # **gamma correction**
    gamma = 0.6060
    I = I**gamma
    I = np.round(I*255)
    ##print ("**** ", I[27,27])
    I= np.uint8(I)

    RGB = cv2.demosaicing(I, cv2.COLOR_BayerBG2RGB_VNG)
    img = cv2.resize(RGB, (width,height),interpolation=cv2.INTER_CUBIC)
    return img
예제 #10
0
    2: cv2.COLOR_BayerGB2BGR,
    3: cv2.COLOR_BayerBG2BGR,
}
for bayer_order, bayer_name in enumerate(bayer_order_enum):
    print(bayer_name, ":")
    print("  hflip = ", bayer_order_enum[bayer_order ^ 1])
    print("  vflip = ", bayer_order_enum[bayer_order ^ 2])
    ((ry, rx), (gy, gx), (Gy, Gx), (by, bx)) = BAYER_OFFSETS[bayer_order]
    ff = np.empty((4, 4), dtype=str)
    ff[ry::2, rx::2] = 'r'
    ff[gy::2, gx::2] = 'g'
    ff[Gy::2, Gx::2] = 'G'
    ff[by::2, bx::2] = 'b'
    print(ff)
    bayer = np.zeros((16, 16, 3), dtype=np.uint8)
    ((ry, rx), (gy, gx), (Gy, Gx), (by, bx)) = BAYER_OFFSETS[bayer_order]
    bayer[ry::2, rx::2, 0] = 255  # Red
    bayer[gy::2, gx::2, 1] = 255  # Green
    bayer[Gy::2, Gx::2, 1] = 255  # Green
    bayer[by::2, bx::2, 2] = 255  # Blue

    if 1:
        upscaled = cv2.resize(bayer,
                              (bayer.shape[0] * 16, bayer.shape[1] * 16),
                              interpolation=cv2.INTER_NEAREST)
        cv2.imshow(bayer_name[-4:], cv2.cvtColor(upscaled, cv2.COLOR_RGB2BGR))
    if 0:
        bgr = cv2.demosaicing(bayer, BAYER2CV[bayer_order])
        cv2.imshow(bayer_name[-4:, bgr])
cv2.waitKey()
예제 #11
0
 def console_run(bayerconfig):
     array = cv2.demosaicing(gui.vs, code=bayerconfig)
     gui.show(array)