Skip to content
The Chat Archive
gpt-3.5-turbo-0613· OpenAI WildChat-4.8M (AI2)

import cv2 import numpy as np import matplotlib.pyplot as plt def robertsoperator(I): M,…

  1. User

    import cv2 import numpy as np import matplotlib.pyplot as plt

    def roberts_operator(I): M, N, _ = I.shape GX = 0 GY = 0 FI = 0 T = 0 for x in range(1, M - 1): for y in range(1, N - 1): GX = I[x - 1, y + 1] + 2 * I[x, y + 1] + I[x + 1, y + 1] - I[x - 1, y - 1] - 2 * I[x, y - 1] - I[ x + 1, y - 1] GY = I[x + 1, y - 1] + 2 * I[x + 1, y] + I[x + 1, y + 1] - I[x - 1, y - 1] - 2 * I[x - 1, y] - I[ x - 1, y + 1] SXY = np.sqrt(GX ** 2 + GY ** 2) if np.any(SXY > T): FI = FI + SXY return FI ** 2

    def tenengrad_operator(I): M, N, _ = I.shape FI = 0 for x in range(0, M - 2): for y in range(0, N): F1 = I[x + 2, y] - I[x, y] FI += F1 ** 2 return FI

    def brenner_operator(I): M, N, _ = I.shape FI = 0 # 变量,暂时存储每一幅图像的Brenner值 for x in range(0, M - 2): for y in range(0, N): F1 = I[x + 2, y] - I[x, y] FI += F1 * F1 return FI

    def plot_curves(): N1 = 15 A = np.zeros(N1) A1 = np.zeros(N1) A2 = np.zeros(N1) X = np.zeros(N1) X1 = np.zeros(N1) X2 = np.zeros(N1)

    for L in range(1, N1 + 1):
        I = cv2.imread(str(L) + '.jpg')
        I = np.double(I)
    
        FI_roberts = roberts_operator(I)
        A[L - 1] = np.sum(FI_roberts)
    
        FI_tenengrad = tenengrad_operator(I)
        A1[L - 1] = np.sum(FI_tenengrad)
    
        FI_brenner = brenner_operator(I)
        A2[L - 1] = np.sum(FI_brenner)
    
    C = np.max(A)
    D = np.min(A)
    E = C - D
    X = (A - D) / E
    
    C1 = np.max(A1)
    D1 = np.min(A1)
    E1 = C1 - D1
    X1 = (A1 - D1) / E1
    
    C2 = np.max(A2)
    D2 = np.min(A2)
    E2 = C2 - D2
    X2 = (A2 - D2) / E2
    
    x1 = np.array([1, 7, 15, 27, 34, 46, 55, 64, 78, 82, 94, 101, 121, 134, 154])
    
    y1 = np.array([X[0], X[1], X[2], X[3], X[4], X[5], X[6], X[7], X[8], X[9], X[10], X[11], X[12], X[13], X[14]])
    y2 = np.array(
        [X1[0], X1[1], X1[2], X1[3], X1[4], X1[5], X1[6], X1[7], X1[8], X1[9], X1[10], X1[11], X1[12], X1[13], X1[14]])
    y3 = np.array(
        [X2[0], X2[1], X2[2], X2[3], X2[4], X2[5], X2[6], X2[7], X2[8], X2[9], X2[10], X2[11], X2[12], X2[13], X2[14]])
    
    plt.plot(x1, y1, color='green', linestyle='-', label='roberts')
    plt.plot(x1, y2, color='blue', label='tenengrad')
    plt.plot(x1, y3, color='yellow', label='brenner')
    plt.legend()
    plt.show()
    

    plot_curves()代码运行结果,无蓝色拟合曲线,

  2. Assistant

    There could be several reasons why the blue curve representing the tenengrad operator is not visible in the plot. Some possible reasons are:

    1. Calculation error: The tenengrad_operator function may have a calculation error that results in all values being zero or very close to zero. Verify that the calculations are correct and that the function is returning non-zero values.

    2. Data range: The values returned by the tenengrad_operator function may be in a much smaller range compared to the other operators, resulting in a very small or invisible curve on the plot. Try adjusting the scale or normalization of the data being plotted.

    3. Incorrect data points: Double-check that the x and y values being plotted for the tenengrad operator are correct. Verify that they correspond to the correct image numbers or measurements.

    To solve the issue, you can start by debugging the tenengrad_operator function and checking the values it returns for different images. You can also print the x and y values being plotted for the tenengrad operator to see if they are correct. Additionally, you can try changing the normalization or scaling of the tenengrad operator values to make them more visible on the plot.