简体   繁体   中英

Calculating the average pixel intensity for neighboring pixels only for specific points of an image and storing inside a n-dimensional array

Im working on a face recognition task where i would like to extract face features but just from specific key locations in the face image. But for such task, i would need to calculate the average pixel value in the neighboring region of that specific part. I am doing so by hand, due to having no algorithm. This is an exhaustive process. This is how i am doing it:

img = plt.imread(path)
img[25, 40] = 0
img[25, 41] = 0
img[25, 39] = 0
img[26, 40] = 0
img[26, 39] = 0
img[26, 41] = 0
img[24, 39] = 0
img[24, 40] = 0
img[24, 41] = 0
img[25, 110] = 0
img[25, 111] = 0
img[25, 109] = 0
img[24, 109] = 0
img[24, 110] = 0
img[24, 111] = 0
img[26, 109] = 0
img[26, 110] = 0
img[26, 111] = 0
img[25, 170] = 0
img[25, 171] = 0
img[25, 169] = 0
img[24, 170] = 0
img[24, 171] = 0
img[24, 169] = 0
img[26, 170] = 0
img[26, 169] = 0
img[26, 171] = 0
img[40, 40] = 0
img[40, 41] = 0
img[40, 39] = 0
img[41, 40] = 0
img[41, 41] = 0
img[41, 39] = 0
img[39, 40] = 0
img[39, 39] = 0
img[39, 41] = 0
img[50, 110] = 0
img[50, 111] = 0
img[50, 109] = 0
img[51, 110] = 0
img[51, 111] = 0
img[51, 109] = 0
img[49, 110] = 0
img[40, 170] = 0
img[40, 171] = 0
img[40, 169] = 0
img[39, 170] = 0
img[39, 171] = 0
img[39, 169] = 0
img[41, 170] = 0
img[41, 171] = 0
img[41, 169] = 0

plt.imshow(img)

What i would like to do basically is have a better way to calculate the average value of a neighboring 3x3 pixels around the center for 20 coordinates in a image and store inside a n-dimensional vector. Or for more clarification: for a choosen keypoint 1 calculate the average value in a 3x3 neighborhood, store this value. keypoint 2 calculate the average value in a 3x3 neighborhood, store the value. For any given keypoint that corresponds to a coordinate x and y in the image. 在此处输入图像描述

Each keypoint is an average value of a grid that has 3x3 pixels to which i need to grab the average value and store inside a 20d array. Whats the best way to do it?

In Python/OpenCv, just use cv2.blur() to do a 3x3 block average of the whole image. Then just get the pixel values in the blurred image at the points you desire.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM