[英]Sliding window over an image OpenCV
I am trying to define a window that scans across an image, I want to find the average RGB values in each window and output them. 我试图定义一个扫描图像的窗口,我想在每个窗口中找到平均RGB值并输出它们。
I have managed to get the average RGB values for the entire image like this: 我设法得到整个图像的平均RGB值,如下所示:
img = cv2.imread('images/0021.jpg')
mean = cv2.mean(img)
print mean[0]
print mean[1]
print mean[2]
Gives: 得到:
#Output
51.0028081597
63.1069849537
123.663025174
How could I apply this mean function to a moving window and output the values for each window? 如何将此均值函数应用于移动窗口并输出每个窗口的值?
EDIT: 编辑:
Here is what I have now: 这就是我现在拥有的:
img = cv2.imread('images/0021.jpg')
def new(img):
rows,cols = img.shape
final = np.zeros((rows, cols, 3, 3))
for x in (0,1,2):
for y in (0,1,2):
img1 = np.vstack((img[x:],img[:x]))
img1 = np.column_stack((img1[:,y:],img1[:,:y]))
final[x::3,y::3] = np.swapaxes(img1.reshape(rows/3,3,cols/3,-1),1,2)
b,g,r = cv2.split(final)
rgb_img = cv2.merge([r,g,b])
mean = cv2.mean(rgb_img)
print mean[0]
print mean[1]
print mean[2]
But now I am getting zero output. 但现在我的零输出。
I wrote a script similar to the given links. 我写了一个类似于给定链接的脚本。 It basically divides your img to 3*3 parts and then computes mean (and standard deviation) of each part. 它基本上将你的img分成3 * 3部分,然后计算每个部分的平均值(和标准偏差)。 With a little array optimization I think you can use it real time/on video. 通过一些小数组优化,我认为您可以实时/在视频上使用它。
PS: Divisions should be integer division PS:除法应该是整数除法
EDIT: now the script gives 9 outputs each represent a mean of its own region. 编辑:现在脚本提供9个输出,每个输出代表其自己区域的平均值。
import numpy as np
import cv2
img=cv2.imread('aerial_me.jpg')
scale=3
y_len,x_len,_=img.shape
mean_values=[]
for y in range(scale):
for x in range(scale):
cropped_image=img[(y*y_len)/scale:((y+1)*y_len)/scale,
(x*x_len)/scale:((x+1)*x_len)/scale]
mean_val,std_dev=cv2.meanStdDev(cropped_image)
mean_val=mean_val[:3]
mean_values.append([mean_val])
mean_values=np.asarray(mean_values)
print mean_values.reshape(3,3,3)
The output is bgr mean values of each window: 输出是每个窗口的bgr平均值:
[[[ 69.63661573 66.75843063 65.02066449]
[ 118.39233345 114.72655391 116.14441964]
[ 159.26887164 143.40760348 144.63208436]]
[[ 75.50831044 107.45708276 103.0781851 ]
[ 108.46450034 141.52005495 139.84878949]
[ 122.67583265 154.86071992 153.67907072]]
[[ 83.67678571 131.45284169 128.27706902]
[ 86.57919815 129.09968235 128.64439389]
[ 90.1102402 135.33173999 132.86622807]]]
[Finished in 0.5s]
Filter with a kernel of shape equal to your window, and values all equal to 1/window_areas. 使用形状等于窗口的内核进行过滤,值均等于1 / window_areas。 The result is local average you seek (also known as a "box blur" operation). 结果是您寻找的局部平均值(也称为“盒子模糊”操作)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.