python - 使用滑动窗口时尝试保存图像时出现问题
问题描述
我想将应用滑动窗口算法时获得的图像保存在一个 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
解决方案
推荐阅读
- flutter - 如何使用底部导航栏从脚手架主体内部推送路线?
- c# - ASP.NET MVC - DB EntityValidation 和模型状态冲突
- azure-devops - AzureDevops Winappdriver如何设置DontShowUI
- sas - SAS - 如何将列名作为值获取到另一列(基于其值作为条件)
- excel - 我想检测包含特定值的第一列和最后一列
- python - Windows:ImportError:DLL 加载失败(python、sklearn)
- python-3.x - 读取子文件夹中的 Excel 文件以及如何使用子文件夹名称作为新列值?
- jenkins - Artifactory 没有缓存来自 https://registry.npmjs.org 404 Not Found 的一些 npm 包]
- c++ - 为什么这个 constexpr 不会产生错误?
- r - 更改 `gtsummary::tbl_regression` 列