Home > Enterprise >  How to sweep an squared area in a dataframe of pixels in python to find the highest values?
How to sweep an squared area in a dataframe of pixels in python to find the highest values?

Time:09-24

I have a directory with hundreds of csv files that represent thermographic images (288x383), currently the code scans each image, takes the mean of the central square region (10 rows x 10 columns), and delivers the mean value in the dataset that correlates each csv file with the mean value found .

import os
import glob
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib as m
%matplotlib inline

files = glob.glob('*.csv')
result = []

for file in files: 
    data = pd.read_csv(file, header = None)
    # Central squared area to extract values
    area_temp_measured = data.iloc[134:154, 181:201]

    avg_rect_temp = area_temp_measured.to_numpy().sum() / 400

    # append to the list
    result.append((file, avg_rect_temp))

df = pd.DataFrame(result, columns = ['Full_name', 'Temperature'])

df.head()

Out:

                  Full_name  Temperature  
0   2021-09-13_13-42-16.csv     19.53025  
1   2021-09-13_13-42-22.csv     17.79425  
2   2021-09-13_13-42-29.csv     17.62500  
3   2021-09-13_13-42-35.csv     18.06100  
4   2021-09-13_13-42-47.csv     18.73850 

However, I want to update the code so that it can completely scan each file, in a similar square area (11x11) but in each location, find the highest average value of that same area, and deliver the value to the dataset along with the central coordinate of that area.

Example:

01 | 02 | 03 | 01 | 07 | 08 | 09 | 04 | 02 | 08
03 | 12 | 22 | 00 | 14 | 01 | 04 | 06 | 17 | 00
04 | 11 | 09 | 03 | 22 | 23 | 11 | 01 | 09 | 18
12 | 19 | 01 | 02 | 19 | 02 | 05 | 09 | 00 | 03
31 | 02 | 13 | 17 | 02 | 07 | 14 | 29 | 04 | 01

...

01 | 02 | 03 | 01 | 07 | 08 | 09 | 04 | 02 | 08
03 | 12 | 22 | 00 | 14 | 01 | 04 | 06 | 17 | 00
04 | 11 | 09 | 03 | 22 | 23 | 11 | 01 | 09 | 18
12 | 19 | 01 | 02 | 19 | 02 | 05 | 09 | 00 | 03
31 | 02 | 13 | 17 | 02 | 07 | 14 | 29 | 04 | 01

...

01 | 02 | 03 | 01 | 07 | 08 | 09 | 04 | 02 | 08
03 | 12 | 22 | 00 | 14 | 01 | 04 | 06 | 17 | 00
04 | 11 | 09 | 03 | 22 | 23 | 11 | 01 | 09 | 18
12 | 19 | 01 | 02 | 19 | 02 | 05 | 09 | 00 | 03
31 | 02 | 13 | 17 | 02 | 07 | 14 | 29 | 04 | 01

                  Full_name  Temperature    Coord_row  Coord_col
0   2021-09-13_13-42-16.csv     22.52000          144        200  
1   2021-09-13_13-42-22.csv     12.27240          250        150  
2   2021-09-13_13-42-29.csv     17.04500          123        300
3   2021-09-13_13-42-35.csv     38.04000          100        012
4   2021-09-13_13-42-47.csv     49.08500          220        180

CodePudding user response:

You want to do a 2D convolution on your image data and find the maximum value in the resulting image.

Make your DataFrame a NumPy array:

img = df.values

Blur the image with a (11, 11) kernel:, as per Calculate mean across area in a 2D array:

blurred = cv2.blur(img, (11, 11))

If you don't want to install OpenCV, look at the other solutions in the same question, but be aware that the SciPy 2D convolution is much slower.

Find the coordinates of the maximum, as per https://stackoverflow.com/a/9483964/463796:

coord_row, coord_col = np.unravel_index(blurred.argmax(), blurred.shape)

temp = blurred[coord_row, coord_col]
  • Related