简体   繁体   中英

How to make open CV show the next file name and the next value on thermal-image processing?

I'm trying to get a temperature value from several image using the openCV by using the RGB value, by mouse clicking on the point that i want to see.

# Thermal-processing_v1.py

from PIL import Image
import math
import numpy as np
import os
import pandas as pd
import cv2

# Load images module
def loadImages():
    temp = os.listdir('.')
    temp.sort()
    gbr = []
    for i in temp:
        if(i.endswith('jpg')):
            gbr.append(i)

    return [os.path.join('.',f) for f in os.listdir('.') if f.endswith('.jpg')]

# Load images to python
filenames = loadImages()
images = []
gs = []

for file in filenames:
    gambar = cv2.imread(file)
    images.append(gambar)

# List of list
B = []
G = []
R = []
suhu = []
red = []
green = []
blue = []


# Mouse GUI   
def mouseRGB(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN: 
        colorsB = gambar[y,x,0]
        colorsG = gambar[y,x,1]
        colorsR = gambar[y,x,2]
        colors = gambar[y,x]
        R.append(colorsR)
        G.append(colorsG)
        B.append(colorsB)

# Mouse Window
cv2.namedWindow('mouseRGB')
cv2.setMouseCallback('mouseRGB',mouseRGB)

# Grayscale processing
for gray in images:
    gray_image = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
    gs.append(gray_image)

    while(1):
        cv2.imshow('mouseRGB',gray_image)
        if cv2.waitKey(0) & 0xFF == 27 :
            for r in R:
                red.append(float(r))
            for g in G:
                green.append(float(g))
            for b in B:
                blue.append(float(b))
            nr = [i * 0.3 for i in red]
            ng = [j * 0.59 for j in green]
            nb = [k * 0.11 for k in blue]
            lum = nr + ng + nb
            tem = [z * 0.1502339357 for z in lum]
            break       
    cv2.destroyAllWindows()
    temp = sum(tem)
    suhu.append([file,temp])
    print(suhu)
# Appending data to csv
img_df = pd.DataFrame(suhu ,columns = ['Image', 'Temperature'])
img_df.to_csv('Olah_Suhu.csv')

Everything works fine for one image file and value, and if i click "esc" it continue to go to the next image. But the value and the file name it's still the same from the first one. And the value became the sum of the value that i click.

['./190530_0002T.jpg', 34.989483624529996]]
[['./190530_0002T.jpg', 34.989483624529996], ['./190530_0002T.jpg', 69.97896724905999]]
[['./190530_0002T.jpg', 34.989483624529996], ['./190530_0002T.jpg', 69.97896724905999], ['./190530_0002T.jpg', 104.96845087358999]]
[['./190530_0002T.jpg', 34.989483624529996], ['./190530_0002T.jpg', 69.97896724905999], ['./190530_0002T.jpg', 104.96845087358999], ['./190530_0002T.jpg', 139.95793449812004]]
  1. Once you come out of the for loop where you load each image in the directory, ie, for file in filenames: , the variable file will contain the name of the last image in the list filenames . So if the list filenames contains 3 images, let's say, filenames = [1.jpg, 2.jpg, 3.jpg] , the variable file will have the value of 3.jpg once you have come out of the loop and this is what you are appending into the suhu list in the line suhu.append([file,temp]) . That is why you are seeing the same image name. To solve this, use enumerate in the loop you are using to get each image to convert them to greyscale. This will give you each image and the index of the image. So replace the line for gray in images: with for index, gray in enumerate(images): and use index to get the corresponding file name by replacing suhu.append([file,temp]) to suhu.append([filenames[index], temp]) .

  2. You are not clearing your B , G , R , red , green and blue lists. For each image, the values from the previous image get added to the list since you are using .append . You should clear the lists after you are done with one image using the list_name.clear() function if using Python 3.3 or higher or del list[:] for any version.

  3. The mouseRGB() function will only be called on the image window 'mouseRGB' instance created with cv2.namedWindow('mouseRGB') and assigned using cv2.setMouseCallback('mouseRGB',mouseRGB) . After the user hits ESC and you break from the while(1) loop, you call cv2.destroyAllWindows() which will close/destroy the mouseRGB window which had the call back function assigned so your mouseRGB() is not getting called after the first image. You only get the R, G, B values from the first image and since you were not clearing the lists, these first values were getting appended and added after each image. Don't call the cv2.destroyAllWindows() within the for loop and this will solve the problem. You can call it once you have read all images, after the for loop and right before you apped data to the csv.

I have modified your code with the changes I mentioned above and this works fine:

# Thermal-processing_v1.py

from PIL import Image
import math
import numpy as np
import os
import pandas as pd
import cv2

# Load images module
image_dir = '.'
def loadImages():
    temp = os.listdir(image_dir)
    temp.sort()
    gbr = []
    for i in temp:
        if(i.endswith('jpg')):
            gbr.append(i)
    return [os.path.join(image_dir, f) for f in os.listdir(image_dir) if f.endswith('.jpg')]

# Load images to python
filenames = loadImages()

images = []
gs = []

for file in filenames:
    gambar = cv2.imread(file)
    images.append(gambar)

# List of list
B = []
G = []
R = []
suhu = []
red = []
green = []
blue = []


# Mouse GUI   
def mouseRGB(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDOWN: 
        colorsB = gambar[y,x,0]
        colorsG = gambar[y,x,1]
        colorsR = gambar[y,x,2]
        colors = gambar[y,x]
        R.append(colorsR)
        G.append(colorsG)
        B.append(colorsB)        

# Mouse Window
cv2.namedWindow('mouseRGB')
cv2.setMouseCallback('mouseRGB',mouseRGB)

# Grayscale processing
for index, gray in enumerate(images):
    gray_image = cv2.cvtColor(gray, cv2.COLOR_BGR2GRAY)
    gs.append(gray_image)

    while(1):
        cv2.imshow('mouseRGB',gray_image)
        if cv2.waitKey(0) & 0xFF == 27 :
            for r in R:
                red.append(float(r))
            for g in G:
                green.append(float(g))
            for b in B:
                blue.append(float(b))
            nr = [i * 0.3 for i in red]
            ng = [j * 0.59 for j in green]
            nb = [k * 0.11 for k in blue]
            lum = nr + ng + nb
            tem = [z * 0.1502339357 for z in lum]
            break       
    #cv2.destroyAllWindows()    
    temp = sum(tem)    
    suhu.append([filenames[index], temp])

    print(suhu)

    R.clear();
    G.clear();
    B.clear();

    red.clear();
    green.clear();
    blue.clear();

cv2.destroyAllWindows()    
# Appending data to csv

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