我正在研究图像中的物体检测,并使用来自主图像上的滑动窗口的矢量化图像切片阵列。我想将所有窗口调整为小尺寸,以便传递给物体探测器。不幸的是我发现当我使用opencv resize函数时,它只能在它停止工作之前立即调整我传递给它的一小块图像。
在下面代码的最后几行中,有两行指定了尝试一次调整大小的窗口数。对于给定的窗口大小,它使用512并且失败,有513个窗口。如果调整大小函数中存在最大数组大小限制,则可能是3481600和3488400数组值之间的差异。
我还没有能够在opencv文档中找到任何引用resize函数大小限制的内容,但有没有人遇到过这个问题?
在已知具有更大容量的矢量化图像切片收缩的不同模块中是否还有其他调整大小功能?
我目前的工作是循环调整图像数组,使其大小小于我实验确定的最大数组大小的块。
感谢任何建议/智慧!
import cv2
import numpy as np
img = cv2.imread('best_of_hope_kolosser_water_snow.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
## ---- these would normally be function arguments for this module -------
window_size = np.resize(np.array([int(100/1.45),100]),(1,2))
w = 0
strX_wPerc = 1/6
strY_wPerc = 1/6
data_patch_size = (30,46)
## ---- end of; locally defining function arguments for module --------
## ---- sliding window params -----
win_w = window_size[w,0]
win_h = window_size[w,1]
strideX = int(window_size[w,0]*strX_wPerc)
strideY = int(window_size[w,1]*strY_wPerc)
## ----------------------------------
print('img shape: ', img.shape)
# ^^ prints: img shape: (1200, 1920)
## -------- sliding window vectorization steps --------------------------
num_vert_windows = len(np.arange(0,img.shape[0]-window_size[w,1],strideY)) # number of vertical windows that will be created
indx = np.arange(0,img.shape[0]-window_size[w,1],strideY)[:,None]+np.arange(window_size[w,1]) # index that will be broadcasted across image
vertical_windows = img[indx] # array of windows win_h tall and the full width of the image
vertical_windows = np.transpose(vertical_windows,(0,2,1)) # transpose to prep for broadcasting
num_horz_windows = len(np.arange(0,vertical_windows.shape[1]-window_size[w,0],strideX)) # number of horizontal windows that will be created
indx = np.arange(0,vertical_windows.shape[1]-window_size[w,0],strideX)[:,None]+np.arange(window_size[w,0]) # index for broadcasting across vertical windows
all_windows = vertical_windows[0:vertical_windows.shape[0],indx] # array of all the windows
## -------- end of, sliding window vectorization ------------------------
total_windows = num_vert_windows*num_horz_windows
all_windows = np.transpose(all_windows,(3,2,1,0)) # rearrange for resizing and intuitive indexing
all_windows = np.resize(all_windows,(window_size[w,1],window_size[w,0],total_windows)) # resize to stack all windows
print('sliding windows height:',all_windows.shape[0],', width:',all_windows.shape[1],', number of windows:',all_windows.shape[2])
# ^^ prints: sliding windows height: 100 , width: 68 , number of windows: 11661
##num_windows_to_resize = all_windows.shape[2] # ideally this would resize them all at once
num_windows_to_resize = 512 # 513 fails
small_windows = cv2.resize(all_windows[:,:,0:num_windows_to_resize],data_patch_size,0,0,cv2.INTER_AREA)
print('final windows (height, width, # windows):',small_windows.shape)
# ^^ if resizing less than 513 windows, prints: final windows (height, width, # windows): (46, 30, 512)
# ^^ if resizing more than 512 windows, prints: final windows (height, width, # windows): (46, 30)
答案 0 :(得分:1)
只是澄清一下,你所要求的可以归结为以下内容
>>> cv2.resize(np.zeros([1200,1920,512]), (30,46)).shape
(46,30,512)
>>> cv2.resize(np.zeros([1200,1920,513]), (30,46)).shape
(46,30)
那么为什么第一行调整所有通道的大小,而第二行似乎只生成一个通道呢?
我发现默认情况下,OpenCV在单个图像中不支持超过512个通道。如cvdef.h
#define CV_CN_MAX 512
所以会发生的是cv2.resize
的输入仅减少到第一个频道。不建议更改最大通道,如下所述:http://answers.opencv.org/question/46296/increase-the-maximum-amount-of-channels-in-cvmat/
所以不幸的是,这意味着你必须分批调整大小。