到目前为止,我的代码将图像分割成网格,然后找到网格每个区域的平均像素值。我现在试图将网格的每个区域内的所有像素值更改为区域的平均值,即3 * 3网格图像将具有9个不同的像素值。
# import packages
import numpy as np
import cv2
import dateutil
import llist
from matplotlib import pyplot as plt
import argparse
#read in image
img = cv2.imread('images/0021.jpg')
scale = 3
#get x and y components of image
y_len,x_len,_ = img.shape
mean_values = []
for y in range(scale):
for x in range(scale):
#crop image 3*3 windows
cropped_image=img[(y*y_len)/scale:((y+1)*y_len)/scale,
(x*x_len)/scale:((x+1)*x_len)/scale]
mean_val=cv2.mean(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)
输出mean_values数组如下所示:
[[[ 25.91920573 32.65683594 79.10800781]
[ 47.4043099 67.91571615 159.6719401 ]
[ 140.98916667 172.33229167 225.72739583]]
[[ 27.79309896 36.55742188 100.57583333]
[ 42.1552474 55.85042969 148.40445313]
[ 92.89833333 105.89760417 156.51497396]]
[[ 23.05854167 25.96677083 63.69509115]
[ 28.53105469 34.806875 98.81148438]
[ 30.2763151 35.97891927 80.45804688]]]
因此,数组的第一个元素是第一个区域/网格/窗口的像素值。看了一遍寻求帮助,但所有例子似乎都需要像素坐标,我不一定知道它们。
感谢您花时间阅读:)