Проблема OpenCV с M1 Mac: локальная переменная ‘x’, на которую ссылаются перед назначением

#python #opencv #image-processing

Вопрос:

Я использовал этот код для настройки выравнивания изображений на компьютерах с Windows и Raspberry pi без каких-либо проблем. Но когда я недавно перешел на macbook на базе M1, я получил сообщение об ошибке ‘UnboundLocalError: локальная переменная ‘CFIgray’, на которую ссылаются перед назначением’. Я изменил свою среду на Anaconda, Spyder и OpenCV, поскольку изначально предполагал, что это связано с совместимостью программного обеспечения и M1 mac. Но это создает проблему с моим функциональным кодом ‘Img_Alignment’. Не могли бы вы дать мне советы по решению этой проблемы? Если код ‘Img_Alignment’ всегда вызывает проблему, я могу устранить это с помощью альтернативного метода, который может настроить выравнивание изображения на основе эталонного изображения. С нетерпением ждем любых советов!

 import csv
import cv2
import numpy as np
from matplotlib import pyplot as plt
import glob
import pandas as pd

#function to adjust alignment of unaliment image
def Img_Alignment(Image_for_alignment,Reference):
    #the referecen: https://learnopencv.com/image-alignment-ecc-in-opencv-c-python/
    #1st parameter -> Image_for_alignment: an image that should be adjusted
    #2nd parameter -> Reference: a reference alignment image for alignment adjustment
    Before = cv2.imread(Image_for_alignment)#Read image that is before the alignment adjustment
    Before_gray = cv2.cvtColor(Before,cv2.COLOR_BGR2GRAY) #convert the image as gray scale
    sz = Reference.shape #find size of the reference image
    warp_mode = cv2.MOTION_TRANSLATION # Define the motion model
    # Define 2x3 or 3x3 matrices and initialize the matrix to identity
    if warp_mode == cv2.MOTION_HOMOGRAPHY:
        warp_matrix = np.eye(3, 3, dtype=np.float32)
    else:
        warp_matrix = np.eye(2, 3, dtype=np.float32)
    # Specify the number of iterations.
    number_of_iterations = 5000;
    # Specify the threshold of the increment
    # in the correlation coefficient between two iterations
    termination_eps = 1e-10;
    # Define termination criteria
    criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, number_of_iterations,  termination_eps)
    # Run the ECC algorithm. The results are stored in warp_matrix.
    (cc, warp_matrix) = cv2.findTransformECC(Reference,Before_gray,warp_matrix, warp_mode, criteria, None, 1)
    if warp_mode == cv2.MOTION_HOMOGRAPHY :
        # Use warpPerspective for Homography
        Aligned = cv2.warpPerspective (Before, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR   cv2.WARP_INVERSE_MAP)
    else:
    # Use warpAffine for Translation, Euclidean and Affine
        Aligned = cv2.warpAffine(Before, warp_matrix, (sz[1],sz[0]), flags=cv2.INTER_LINEAR   cv2.WARP_INVERSE_MAP);
    Aimg=str(Image_for_alignment).replace('.png','')
    cv2.imwrite(Aimg "_aligned.jpg",Aligned)#it generates 16 bit images

#Index Extractor based on Multispectral Images
#User defined function to creat index image and extract the average and std of the index value over the projected canopy size
def IEMI(imageformat, folder):
    path = glob.glob(folder "/*/")#directory 
    path = (pd.Series(path)   imageformat).tolist()#find all files in a folder of the directory
    
    for i in path:
        fileList=glob.glob(i)
        for fdx, filename in enumerate(fileList): #A For loop statment: iteration from all indices (filenames) within the folder
            fname = filename.rsplit(".", 1)[0] #Treating the string into a list following the separator "."
            fname = fname.replace('ch7', '')#remove string that is redundant 
            fname = fname.replace('mask', '')#remove string that is redundant
            if filename.find('mask')!= -1:#mask = Chlorophyll fluorescence image used to generate mask image
                CFIimg = cv2.imread(filename) #Define 'CFIimg' that reads an CFI image corresponding to a filename;
                CFIgray = cv2.cvtColor(CFIimg, cv2.COLOR_BGR2GRAY)#Convert 'CFIimg' as grayscale image
            
            elif filename.find('ch6')!= -1:#ch6 = 870 nm, here the program fix the alignment
                Img_Alignment(filename, CFIgray)#CFIgray)
                Pch6 = cv2.imread(fname "_aligned.jpg",cv2.IMREAD_ANYDEPTH) #open 16 bit image (the aligned image) as 8 bit image

IEMI('/*.png',"/Users/Desktop/picture")
 

Комментарии:

1. Ошибка очевидна. Вы предполагаете, что CFIgray = cv2.cvtColor(CFIimg, cv2.COLOR_BGR2GRAY) это будет выполнено раньше Img_Alignment(filename, CFIgray)#CFIgray) , но ничто не гарантирует, что это произойдет.

2.Пробовали ли вы, например, копировать и вставлять UnboundLocalError: local variable 'CFIgray' referenced before assignment в поисковую систему?

3. Я просмотрел предыдущие сообщения или любые другие документы в поисковой системе, но они всегда говорили об объявлении переменной integer. Однако в моем случае объявляется переменная NumPy array (image), и я понятия не имею, как это сделать (пожалуйста, потерпите меня, потому что я пользователь python начального уровня, и я узнал все с нуля и поиска …). Кроме того, эта проблема никогда не возникала, когда я ее использовална компьютере с Windows., что меня совсем смущает.

Ответ №1:

Я думаю, что проблема связана с этим блоком кода

 if filename.find('mask')!= -1:#mask = Chlorophyll fluorescence image used to generate mask image
                CFIimg = cv2.imread(filename) #Define 'CFIimg' that reads an CFI image corresponding to a filename;
                CFIgray = cv2.cvtColor(CFIimg, cv2.COLOR_BGR2GRAY)#Convert 'CFIimg' as grayscale image
            
elif filename.find('ch6')!= -1:#ch6 = 870 nm, here the program fix the alignment
                Img_Alignment(filename, CFIgray)#CFIgray)
                Pch6 = cv2.imread(fname "_aligned.jpg",cv2.IMREAD_ANYDEPTH) #open 16 bit image (the aligned image) as 8 bit image

 

Если первый оператор if не вычисляется, переменная CFIgray никогда не присваивается. Затем с помощью вашего elif вы ссылаетесь на него, даже если вы никогда не создавали экземпляр переменной.

Комментарии:

1. На самом деле, я не могу избавиться от этих операторов ‘if’ или ‘elif’, поскольку изображение с определенным символом должно обрабатываться одновременно. Не могли бы вы дать мне совет объявить переменную, в которой NumPy array? Я искал похожие проблемы, но обычно они говорят о целочисленной переменной, что не мой случай.