Python cv2 模块,COLOR_RGBA2BGR 实例源码

我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用cv2.COLOR_RGBA2BGR

项目:Camelyon17    作者:deepiano    | 项目源码 | 文件源码
def save_slide_cutting(file_path, save_location, level):

    slide = OpenSlide(file_path) 
    print('==> saving slide_lv_' + str(level) + ' at ' + save_location)

    x_lv_, y_lv_ = 0, 0
    w_lv_, h_lv_ = slide.level_dimensions[level]

    wsi_pil_lv_ = slide.read_region((0,0), level,\
        (w_lv_, h_lv_))

    wsi_ary_lv_ = np.array(wsi_pil_lv_)
    wsi_bgr_lv_ = cv2.cvtColor(wsi_ary_lv_, cv2.COLOR_RGBA2BGR)

    margin_top = int(round(h_lv_ / 12.))
    margin_bottom = int(round(h_lv_ / 32.))
    wsi_bgr_lv_[0:margin_top, :] = 255
    wsi_bgr_lv_[h_lv_ - margin_bottom:h_lv_, :] = 255

    cv2.imwrite(save_location, wsi_bgr_lv_)

#    plt.subplot(1,1,1), plt.imshow(wsi_bgr_lv_)
#    plt.xticks([]), plt.yticks([])
#    plt.show()
项目:Camelyon17    作者:deepiano    | 项目源码 | 文件源码
def save_slide_as_jpg_with_level(file_path, save_location, level):

    slide_tif = OpenSlide(file_path) 
    print(('==> saving slide_lv_%s at ' + save_location) % level)

    wsi_pil_lv_ = slide_tif.read_region((0, 0), level,\
        slide_tif.level_dimensions[level])
    wsi_ary_lv_ = np.array(wsi_pil_lv_)
    wsi_bgr_lv_ = cv2.cvtColor(wsi_ary_lv_, cv2.COLOR_RGBA2BGR)
    cv2.imwrite(save_location, wsi_bgr_lv_)
项目:dataArtist    作者:radjkarl    | 项目源码 | 文件源码
def _exportVideoThread(self):
        ww = self.display.widget
        n = None
        if self.pAnnotate.value():
            n = self.display.layerNames()

        videoWrite(self.pPath.value(), ww.image,
                   levels=ww.item.levels,
                   shape=(self.pHeight.value(), self.pWidth.value()),
                   frames=self.pFrames.value(),
                   annotate_names=n,
                   lut=ww.item.lut, updateFn=self._thread.sigUpdate)
#
#         fourcc = cv2.VideoWriter_fourcc(*'XVID')
#         ww = self.display.widget
#         im = ww.image
#         if self.pResize.value():
#             w, h = (self.pWidth.value(), self.pHeight.value())
#             im = [cv2.resize(i, (w, h)) for i in im]
#         else:
#             h, w = im.shape[1:3]
#         fr = self.pFrames.value()
#         pa = self.pPath.value()
#         assert pa[-3:] in ('avi',
#                            'png'), 'video export only supports *.avi or *.png'
#         isVideo = pa[-3:] == 'avi'
#         if isVideo:
#             cap = cv2.VideoCapture(0)
#             # im.ndim==4)
#             out = cv2.VideoWriter(pa, fourcc, fr, (w, h), isColor=1)
#
#         times = np.linspace(0, len(im), len(im) * fr)
#         interpolator = LinearInterpolateImageStack(im)
#
#         lut = ww.item.lut
#         if lut is not None:
#             lut = lut(im[0])
#
#         for n, time in enumerate(times):
#             # update progress:
#             self._thread.sigUpdate.emit(100 * n / len(times))
#             image = interpolator(time)
#
#             argb = makeRGBA(image, lut=lut,
#                             levels=ww.item.levels)[0]
#             cimg = cv2.cvtColor(argb, cv2.COLOR_RGBA2BGR)
#
#             if isVideo:
#                 out.write(cimg)
#             else:
#                 cv2.imwrite('%s_%i_%.3f.png' % (pa[:-4], n, time), cimg)
#
#         if isVideo:
#             cap.release()
#             out.release()
项目:Camelyon17    作者:deepiano    | 项目源码 | 文件源码
def save_patch(\
        file_path_tif,
        save_location_path_patch,
        patch_level,
        size_patch):

    print('=> Making patch ..')

    # Read file name list of slide directory
    file_name_list_tif = get_list_file_name(file_path_tif)

    for index in range(len(file_name_list_tif)):

        file_name_tif = file_name_list_tif[index]
        file_name_slide = file_name_tif.split('.')[0]
        file_name_csv = file_name_slide + '.csv'

        cur_file_path_tif =\
                os.path.join(file_path_tif, file_name_tif)
        cur_file_path_csv =\
                os.path.join(save_location_path_patch, file_name_slide)
        cur_file_path_csv =\
                os.path.join(cur_file_path_csv, file_name_csv)

        # Load slide image
        slide = OpenSlide(cur_file_path_tif)

        # Read patch position csv file
        csv_file = open(cur_file_path_csv, 'rb')
        csv_reader = csv.DictReader(csv_file)
        csv_data = list(csv_reader)

        # Make patch and Save 
        for i, row in enumerate(csv_data):

            x = int(row['X'])
            y = int(row['Y'])
            patch = slide.read_region((x, y),\
                                patch_level,\
                                (size_patch, size_patch))
            patch = np.array(patch)
            patch = cv2.cvtColor(patch, cv2.COLOR_RGBA2BGR)

            file_name_patch_jpg = file_name_slide + '_patch_' + str(i) + '.jpg'
            cur_save_path_patch_jpg =\
                    os.path.join(save_location_path_patch,\
                                 file_name_slide)
            cur_save_path_patch_jpg =\
                    os.path.join(cur_save_path_patch_jpg ,\
                                 file_name_patch_jpg)
            cv2.imwrite(cur_save_path_patch_jpg, patch)

        csv_file.close()

        print('=> Next..')
项目:camelyon17    作者:hyosari    | 项目源码 | 文件源码
def patch_generation(tif_dir,mask_dir,save_fdir,save_pdir,save_cdir,slide_level,patch_size,patch_num,tumor_win=False):
    """
     save patch image and extraction point with csv, jpg image on the directory

     tif_dir : the tif directory
     mask_dir : the mask directory 
     save_fdir : patch saved directory
     save_pdir : point jpg directory 
     save_cdir : Y,X csv directory 
     slide : slide_level that mask image was applied with 
     patch_size : patch size square 
     patch_num : the number of patches in a whole slide 
     tumor_win : tumor window patch sampling true


        tif file and mask file sholud be one to one mached and same ordered 
    """

    list_tif_name = filelist_in_directory(tif_dir)
    list_mask_name = filelist_in_directory(mask_dir) 

    for slide_idx in range(len(list_tif_name)): 
        pwd_tif = join(tif_dir,list_tif_name[slide_idx])
        pwd_msk = join(mask_dir,list_mask_name[slide_idx]) 
        filename = splitext(list_tif_name[slide_idx])[0]

        # open slide, csv, BGR_image, mask
        slide = OpenSlide(pwd_tif) 
        f = open(save_cdir+filename+".csv",'wt')
        c_writer = csv.writer(f)
        c_writer.writerow(('Y','X'))
        rgba_pil = slide.read_region((0,0),slide_level,slide.level_dimensions[slide_level])
        bgr_im = cv2.cvtColor(np.array(rgba_pil),cv2.COLOR_RGBA2BGR)
        mask = cv2.imread(pwd_msk,cv2.IMREAD_GRAYSCALE)

        #if mask tunes 255 change to 1 
        if np.max(mask) == 255:
            mask = mask == 255
            mask = mask.astype(int)

        # sampling patches
        if tumor_win:
            patch_list,patch_point = tumor_patch_sampling_using_centerwin(slide,slide_level,mask,patch_size,patch_num)
        else:
            patch_list,patch_point = patch_sampling_using_integral(slide,slide_level,mask,patch_size,patch_num)
        p_l_size = patch_size/ slide.level_downsamples[slide_level] 
        p_l_size = int(p_l_size)

        #image wirte and save patches 
        for f_th in range(len(patch_list)): 
            cv2.imwrite(save_fdir+filename+"_patch_"+str(f_th)+"_"+str(patch_point[f_th][1])+"_"+str(patch_point[f_th][0])+"_"+str(patch_size)+".jpg",patch_list[f_th]) 

            c_writer.writerow((patch_point[f_th][1],patch_point[f_th][0]))
            cv2.rectangle(bgr_im,(patch_point[f_th][1],patch_point[f_th][0]),(patch_point[f_th][1]+p_l_size,patch_point[f_th][0]+p_l_size),(255,0,0),1)

        cv2.imwrite(save_pdir+filename+"_selection_point.jpg",bgr_im)
        print "complete patch extraction about "+ list_tif_name[slide_idx]
        f.close()

    return 0