python 實現(xiàn)從高分辨圖像上摳取圖像塊
更新時間:2020年01月02日 10:22:42 作者:WYXHAHAHA123
今天小編就為大家分享一篇python 實現(xiàn)從高分辨圖像上摳取圖像塊,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
我就廢話不多說了,直接上代碼吧!
#coding=utf-8 import cv2 import numpy as np import os # 程序?qū)崿F(xiàn)功能: # 根據(jù)patch在高分辨率圖像上的索引值,crop出對應(yīng)區(qū)域的圖像 # 并驗證程序的正確性 ''' 對于當前輸入的3328*3328的高分辨率特征圖,首先resize到640*640 然后根據(jù)當前的patch文件名(包含了patch在高分辨率圖像上的行索引和列索引) 這個索引值是將高分辨率圖像劃分成多個沒有overlap的256*256的圖像塊之后的行索引和列索引 行索引range(1,11),列索引range(0,12) 3328=13*256 ''' index='IDRiD_03_3_12.jpg' raw_img_path='F:\\2\\eye_seg_con\\eye_seg\\joint_data\\raw_image\\train' patches_path='F:\\2\\eye_seg_con\\eye_seg\\joint_data\\patches\\train' true_patches=cv2.imread(os.path.join(patches_path,index))[:,:,::-1] print(os.path.join(raw_img_path,index.split('_')[0]+index.split('_')[1]+'.jpg')) hr_img=cv2.imread(os.path.join(raw_img_path,index.split('_')[0]+'_'+index.split('_')[1]+'.jpg'))[:,:,::-1] hr_img=cv2.resize(hr_img,(640,640))# hr_img RGB ''' 640/13=49.23076923076923 記作unit 將640*640的區(qū)域平均劃分成13*13份,每一份的像素點大小是unit*unit 然后將對應(yīng)位置(取整)的圖像塊摳出來,resize成256*256大小 ''' unit=640/13 patch_row_num = int(index[:-4].split('_')[2]) patch_col_num = int(index[:-4].split('_')[3]) row_start=round(patch_row_num*unit) row_end=round((patch_row_num+1)*unit) col_start=round(patch_col_num*unit) col_end=round((patch_col_num+1)*unit) my_patch=hr_img[row_start:row_end,col_start:col_end,:] my_patch=cv2.resize(my_patch,(256,256)) my_patch=np.array(my_patch,dtype=np.uint8) cv2.imshow('true_patches',true_patches[:,:,::-1]) cv2.waitKey(0) cv2.imshow('my_patch',my_patch[:,:,::-1]) cv2.waitKey(0) # # hr_img RGB # # # cv2.imshow('1',hr_img[:,:,::-1]) # # cv2.waitKey(0) # # hr_img2=cv2.imread(os.path.join(raw_img_path,index.split('_')[0]+'_'+index.split('_')[1]+'.jpg')) # hr_img2=cv2.resize(hr_img2,(640,640))[:,:,::-1]# hr_img2 RGB # # cv2.imshow('2',hr_img2[:,:,::-1]) # # cv2.waitKey(0) # # print(np.sum(hr_img2-hr_img))# 0 # 結(jié)論: # 對于cv2.resize函數(shù)而言,無論是先進行BGR的通道轉(zhuǎn)換,再resize,還是先進行resize,再進行BGR通道轉(zhuǎn)換 # 所得到的圖像是相同的,即resize和通道維度的變換可交換順序 # 實際上resize只發(fā)生在spatial dimension,而通道變換發(fā)生在channels dimension,所以空間維度上的插值變換 # 是在每個通道維度上獨立進行的。 # 另外,對于計算機而言,所讀取到的彩色圖像就是H*W*3的矩陣而已,它本身是沒有辦法區(qū)分究竟是BGR格式還是RGB格式的
以上這篇python 實現(xiàn)從高分辨圖像上摳取圖像塊就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
python next()和iter()函數(shù)原理解析
這篇文章主要介紹了python next()和iter()函數(shù)原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-02-02python神經(jīng)網(wǎng)絡(luò)學習利用PyTorch進行回歸運算
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)學習利用PyTorch進行回歸運算的實現(xiàn)代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-05-05Python pygorithm模塊用法示例【常見算法測試】
這篇文章主要介紹了Python pygorithm模塊用法,結(jié)合實例形式分析了pygorithm模塊的功能、安裝及針對常見算法的相關(guān)使用操作技巧,需要的朋友可以參考下2018-08-08Python中l(wèi)ogging.NullHandler 的使用教程
這篇文章主要介紹了Python中l(wèi)ogging.NullHandler 的使用教程,非常不錯,具有一定的參考借鑒價值 ,需要的朋友可以參考下2018-11-11