Reputation: 115
I need to reduce the actual resolution of a picture so that each pixel in 4-pixel rectangle is the mean value of those 4 pixels.
i.e.
p1 p2 p6 p7 a1 a1 a2 a2
p3 p4 p8 p9 ... -> a1 a1 a2 a2 where a1 = average(p1, p2, p3, p4), a2 = average(p6, p7, p8, p9) etc
Is it possible to efficiently do that using python/opencv not manipulating individual pixels? Does bilinear/nearest neighbor rescaling down and back to the original resolution produce the requested result(not just the visual effect)? The pictures are monochrome.
Upvotes: 1
Views: 2292
Reputation: 320
If you are interested in transparent numpy/indexing based solution try something like that:
I use some dummy data to test the solution:
img = np.array([range(0,4), range(4, 8), range(8, 12), range(12, 16)])
This gives us following 'image':
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
Then I create four subimages by taking every second element:
subimg1 = img[::2, ::2]
subimg2 = img[1::2, ::2]
subimg3 = img[::2, 1::2]
subimg4 = img[1::2, 1::2]
Now you can average the corresponding samples using numpy:
average = np.average([subimg1, subimg2, subimg3, subimg4], 0)
And create the output:
output = np.zeros(img.shape)
output[::2, ::2] = average
output[1::2, ::2] = average
output[::2, 1::2] = average
output[1::2, 1::2] = average
And the output for given test data is:
[[ 2.5 2.5 4.5 4.5]
[ 2.5 2.5 4.5 4.5]
[ 10.5 10.5 12.5 12.5]
[ 10.5 10.5 12.5 12.5]]
Upvotes: 2