首页 > 解决方案 > 使用滑动窗口时尝试保存图像时出现问题

问题描述

我想将应用滑动窗口算法时获得的图像保存在一个 3 维矩阵中。当我应用 ## 后面的更改时使用此代码时,我得到一个错误。我认为代码是正确的,有什么想法吗?

window 是我想保存在 A 中的矩阵(100,100)

## import the necessary packages

import cv2
import matplotlib.pyplot as plt
import numpy as np
import imutils
import time

img= cv2.imread("2.jpg") # your image path
image = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)

def pyramid(image, scale=1.5, minSize=(30, 30)):
    # yield the original image
    yield image

    # keep looping over the pyramid
    while True:
        # compute the new dimensions of the image and resize it
        w = int(image.shape[1] / scale)
        image = imutils.resize(image, width=w)

        # if the resized image does not meet the supplied minimum
        # size, then stop constructing the pyramid
        if image.shape[0] < minSize[1] or image.shape[1] < minSize[0]:
            break

        # yield the next image in the pyramid
        yield image

def sliding_window(image, stepSize, windowSize):
    # slide a window across the image
    for y in range(0, image.shape[0], stepSize):

        for x in range(0, image.shape[1], stepSize):
            # yield the current window
            yield (x, y, image[y:y + windowSize[1], x:x + windowSize[0]])

(winW, winH) = (100, 100)   

# declarate the matrix 

A=np.empty((100,winW,winH))
b=1

window=[]
#%%
# loop over the image pyramid
for resized in pyramid(image, scale=2):
    # loop over the sliding window for each layer of the pyramid

    for (x, y, window) in sliding_window(resized, stepSize=32, windowSize=(winW, winH)):

        # if the window does not meet our desired window size, ignore it
        if window.shape[0] != winH or window.shape[1] != winW:
            continue

        # THIS IS WHERE YOU WOULD PROCESS YOUR WINDOW, SUCH AS APPLYING A
        # MACHINE LEARNING CLASSIFIER TO CLASSIFY THE CONTENTS OF THE
        # WINDOW

        # since we do not have a classifier, we'll just draw the window
        clone = resized.copy()
        cv2.rectangle(clone, (x, y), (x + winW, y + winH), (0, 255, 0), 2)
        cv2.imshow("Window", clone)
        cv2.waitKey(1)
        time.sleep(0.25)
   #______ ###### the problem #### ______ i want to save the windows obtein in A, but I get the error in the next two lines of code
   ##   A[b,:,:]=window
    ##  b=b+1;

cv2.destroyAllWindows()

我想将 windows obtein 保存在 A 中,但在接下来的两行代码中出现错误 ## A[b,:,:]=window

标签: pythonopencvsliding-window

解决方案


推荐阅读