Reflections on the Fourier transform image is saved as a black solution of

Curriculum design problems encountered get in here to write about ...... In addition, it seems quite a long time did not write a title it is too busy ...... ...... ...... withered

    In these days get curriculum design, software development is a major image processing, then the amount of the basic processes responsible for transferring images is the mobile phone side, the service side receives and calls the appropriate python image processing program, dealt with, and then treatment back to the phone and display the image.

    When the program always get python encounter this situation, it is to save the image of the original processing time is always often becomes black, if you want to see the image after processing, always in need of matplotlib.pyplot imshow to see normal image, and, if you use savefig matplotlib.pyplot to be saved, there will frame the final image processing, preservation and processing of the picture quality is not good, and does not match the software I developed the original intent, so I wanted to find a way is not can not save the image matplotlib.pyplot can solve black problems.

    There had to go online to find a solution that meets my needs, the results looked around with all the matplotlib.pyplot of savefig ...... no fruit, only resolved itself!

    I have here is the Fourier transform, for example, image processing is below this.

clip_image002

    At the beginning, we deal with this program and save the image.

 1 import cv2
 2 
 3 import numpy as np
 4 
 5 from matplotlib import pyplot as plt
 6 
 7 img = cv2.imread('E://report_pic//test.jpg',0)
 8 
 9 f = np.fft.fft2(img)
10 
11 fshift = np.fft.fftshift(f)
12 
13 s1 = np.log(np.abs(fshift))
14 
15 fimg = np.log(np.abs(f))
16 
17 fimg = np.abs(s1)
18 
19 cv2.imwrite('E://report_pic//ntest.jpg',fimg)
View Code

 

    After processing is complete to view the images, so the following is found

clip_image004

    Was relatively ignorant force, in order to find the problem, the output of the processing matrix, as follows,

 1 import cv2
 2 
 3 import numpy as np
 4 
 5 from matplotlib import pyplot as plt
 6 
 7 img = cv2.imread('E://report_pic//test.jpg',0)
 8 
 9 f = np.fft.fft2(img)
10 
11 fshift = np.fft.fftshift(f)
12 
13 s1 = np.log(np.abs(fshift))
14 
15 print("fshift=")
16 
17 print(fshift)
18 
19 fimg = np.log(np.abs(f))
20 
21 fimg = np.abs(s1)
22 
23 print("len(fimg)=")
24 
25 print(len(fimg))
26 
27 print("len(fimg[0])=")
28 
29 print(len(fimg[0]))
30 
31 print("fimg=")
32 
33 print(fimg)
34 
35 cv2.imwrite('E://report_pic//ntest.jpg',fimg)
View Code

 

    The following result output,

clip_image006

    In fact, there has been fimg found the problem - the gray values ​​are very low, contact the gray value 0 when the color display is black. So save the image also understand the reason for the black - gray value image itself is very low. However, the image we need is a spectrum Yeah, such a low value, see also do not see it?

    In order to solve the "look" of the problem, I first used the max-min normalized, then the gray value of each normalized by multiplying the maximum grayscale value 255, we get what we want, "can be seen see the "spectrum of! code show as below,

 1 # coding: utf-8
 2 
 3 import cv2
 4 
 5 import numpy as np
 6 
 7 from matplotlib import pyplot as plt
 8 
 9 img = cv2.imread('E://report_pic//test.jpg',0)
10 
11 f = np.fft.fft2(img)
12 
13 fshift = np.fft.fftshift(f)
14 
15 s1 = np.log(np.abs(fshift))
16 
17 #print("fshift=")
18 
19 #print(fshift)
20 
21 fimg = np.log(np.abs(f))
22 
23 fimg = np.abs(s1)
24 
25 #print("len(fimg)=")
26 
27 #print(len(fimg))
28 
29 #print("len(fimg[0])=")
30 
31 #print(len(fimg[0]))
32 
33 #print("fimg=")
34 
35 #print(fimg)
36 
37 cv2.imwrite('E://report_pic//ntest.jpg',fimg)
38 
39 maxx=-1
40 
41 minn=100000
42 
43 for i in range(len(fimg)):
44 
45 if maxx<max(fimg[i]):
46 
47         maxx=max(fimg[i])
48 
49 if min>min(fimg[i]):
50 
51         minn=min(fimg[i])
52 
53 #print(maxx,minn)#fimg里的最大最小灰度值
54 
55 for i in range(len(fimg)):
56 
57 for j in range(len(fimg[i])):
58 
59         fimg[i][j]=255*(fimg[i][j]-minn)/(maxx-minn)
60 
61 cv2.imwrite('E://report_pic//rtest.jpg',fimg)
View Code

 

    The result is processing the picture below, we can see very clearly found!

clip_image008

    As for the case of the other image processing to save "all-black" , whether it is only one channel of grayscale, or have an RGB three channels , I think you can try to write a program processing in this way, chances are successful.

    We hope to help Ha ~

Guess you like

Origin www.cnblogs.com/memocean/p/12587032.html