Regarding the error calculation of two python images

Asked 2 years ago, Updated 2 years ago, 36 views

By averaging the areas specified in the image, the error between the image and the image before filtering is determined by the difference between pixel values for each pixel, and the location of the area when it becomes maximum and minimum, and the processed image.
The error is defined by the sum of the differences between the rgb values of the images before and after processing.

After checking the operation of the error calculation as follows, the operation is very heavy and the work does not progress.
I would appreciate it if you could let me know what the problem is and how to solve it.

 from PIL import Image
import numpy as np
import cv2
import matplotlib.pyplot asplt
import path


defmain():
    F=cv2.imread("gazou.jpg")
    m,n,c = F.shape
    h = 30
    M=math.floor(m/h)
    N=math.floor(n/h)
    im = F [1:M*h, 1:N*h]
    list = [ ]
    for i in range(h):
        for jin range(h):
            D=im [i:(i+(M-1)*h-1), j:(j+(N-1)*h-1)]
            P = cv2.blur(D,(h,h))
            List = [ ]
            for x in range ((M-1)*h-1):
                for y in range ((N-1)*h-1):
                    Db, Dg, Dr = D[x,y]
                    Pb, Pg, Pr = P[x, y]
                    e=power(int(Dr)-int(Pr), 2)+power(int(Dg)-int(Pg), 2)+power(int(Db)-int(Pb), 2)
                    List.append(e)
            E=sum(List)
            list.append(E)
    Emin=min(list)
    Emax=max(list)
    print(Emin)
    print (Emax)


if__name__=='__main__':
    main()

python python3

2022-09-30 21:45

1 Answers

After all, I'm concerned about the quadruple loop. NumPy itself is implemented in C language, so it should be a little faster, but if you write for sentence, it won't work and it will be slow.

For the time being, the two for statements on the inside can be easily lost, so I rewritten them as follows.

defmain2():
    F=cv2.imread("gazou.jpg")
    m,n,c = F.shape
    h = 30
    M=math.floor(m/h)
    N=math.floor(n/h)
    im = F [1:M*h, 1:N*h]
    list = [ ]
    for i in range(h):
        for jin range(h):
            D=im [i:(i+(M-1)*h-1), j:(j+(N-1)*h-1)]
            P = cv2.blur(D,(h,h))
            E=((D.astype(np.int)-P.astype(np.int))**2).sum()
            list.append(E)
    Emin=min(list)
    Emax=max(list)
    print(Emin)
    print (Emax)

When I measured the speed at %time to input the 256x256 image, the original code was

CPU times: user1min26s, sys:56ms, total:1min27s
Wall time—1 min 27s

However, by changing to main2,

CPU times:user346ms,sys:4 ss,total:346ms
Wall time—345 ms

is now (only one case of output match has been checked).

As for the flow of thought,

 for x in range ((M-1)*h-1):
    for y in range ((N-1)*h-1):
        Db, Dg, Dr = D[x,y]
        Pb, Pg, Pr = P[x, y]
        e=power(int(Dr)-int(Pr), 2)+power(int(Dg)-int(Pg), 2)+power(int(Db)-int(Pb), 2)
        List.append(e)
E=sum(List)

once

 for x in range ((M-1)*h-1):
    for y in range ((N-1)*h-1):
        For c in range (3):
            e=power(int(D[x,y,c])-int(P[x,y,c]), 2)
            List.append(e)
It makes sense to take E=sum(List)#sum 

If you replace List with (D.astype(np.int)-P.astype(np.int))**2, you realize that you can get rid of the for statement.

When using NumPy, how you can reduce the number of for statements for the NumPy array should be one of the major indicators for fast code writing.


2022-09-30 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.