質問編集履歴
3
インデントを修正しました。
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,5 +1,5 @@ | |
| 1 1 | 
             
            ###ソースコード
         | 
| 2 | 
            -
             | 
| 2 | 
            +
            ```
         | 
| 3 3 | 
             
            #import the necessary packages
         | 
| 4 4 | 
             
            from skimage.measure import structural_similarity as ssim
         | 
| 5 5 | 
             
            import matplotlib.pyplot as plt
         | 
| @@ -71,7 +71,7 @@ | |
| 71 71 | 
             
            compare_images(original, original, "Original vs. Original")
         | 
| 72 72 | 
             
            compare_images(original, contrast, "Original vs. Contrast")
         | 
| 73 73 | 
             
            compare_images(original, shopped, "Original vs. Photoshopped")
         | 
| 74 | 
            -
             | 
| 74 | 
            +
            ```
         | 
| 75 75 | 
             
            ###
         | 
| 76 76 | 
             
            ### やったこと
         | 
| 77 77 |  | 
| @@ -82,12 +82,12 @@ | |
| 82 82 | 
             
            原因はなんでしょうか?ご教示お願いします。
         | 
| 83 83 |  | 
| 84 84 | 
             
            ### 発生している問題・エラーメッセージ
         | 
| 85 | 
            -
             | 
| 85 | 
            +
            ```
         | 
| 86 86 | 
             
            Traceback (most recent call last):
         | 
| 87 87 | 
             
              File "C:\Users\Owner\compare.py", line 2, in <module>
         | 
| 88 88 | 
             
                from skimage.measure import structural_similarity as ssim
         | 
| 89 89 | 
             
            ImportError: cannot import name structural_similarity
         | 
| 90 | 
            -
             | 
| 90 | 
            +
            ```
         | 
| 91 91 | 
             
            ### 試したこと
         | 
| 92 92 |  | 
| 93 93 | 
             
            %matplotlib inlineだと「%」にinvalid syntaxエラーが吐かれた。
         | 
2
インデントを修正しました。
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,4 +1,5 @@ | |
| 1 1 | 
             
            ###ソースコード
         | 
| 2 | 
            +
            '''
         | 
| 2 3 | 
             
            #import the necessary packages
         | 
| 3 4 | 
             
            from skimage.measure import structural_similarity as ssim
         | 
| 4 5 | 
             
            import matplotlib.pyplot as plt
         | 
| @@ -70,7 +71,7 @@ | |
| 70 71 | 
             
            compare_images(original, original, "Original vs. Original")
         | 
| 71 72 | 
             
            compare_images(original, contrast, "Original vs. Contrast")
         | 
| 72 73 | 
             
            compare_images(original, shopped, "Original vs. Photoshopped")
         | 
| 73 | 
            -
             | 
| 74 | 
            +
            '''
         | 
| 74 75 | 
             
            ###
         | 
| 75 76 | 
             
            ### やったこと
         | 
| 76 77 |  | 
| @@ -81,11 +82,12 @@ | |
| 81 82 | 
             
            原因はなんでしょうか?ご教示お願いします。
         | 
| 82 83 |  | 
| 83 84 | 
             
            ### 発生している問題・エラーメッセージ
         | 
| 84 | 
            -
             | 
| 85 | 
            +
            '''
         | 
| 85 86 | 
             
            Traceback (most recent call last):
         | 
| 86 87 | 
             
              File "C:\Users\Owner\compare.py", line 2, in <module>
         | 
| 87 88 | 
             
                from skimage.measure import structural_similarity as ssim
         | 
| 88 89 | 
             
            ImportError: cannot import name structural_similarity
         | 
| 90 | 
            +
            '''
         | 
| 89 91 | 
             
            ### 試したこと
         | 
| 90 92 |  | 
| 91 93 | 
             
            %matplotlib inlineだと「%」にinvalid syntaxエラーが吐かれた。
         | 
1
ソースコードの表示がおかしかったのでその変更
    
        title	
    CHANGED
    
    | 
            File without changes
         | 
    
        body	
    CHANGED
    
    | @@ -1,101 +1,98 @@ | |
| 1 | 
            -
            ### 前提・実現したいこと
         | 
| 2 | 
            -
             | 
| 3 | 
            -
            http://kamonohashiperry.com/archives/699
         | 
| 4 | 
            -
            上記のページの「以下は、類似度計算の実行用コードとなります。」
         | 
| 5 | 
            -
            以降のプログラムを実行した際invalid syntaxエラーを吐かれ
         | 
| 6 | 
            -
            エラーを吐いたところをコメント化して実行したら
         | 
| 7 | 
            -
            以下のようなエラーが起こりました。
         | 
| 8 | 
            -
            原因はなんでしょうか?ご教示お願いします。
         | 
| 9 | 
            -
             | 
| 10 | 
            -
            ### 発生している問題・エラーメッセージ
         | 
| 11 | 
            -
             | 
| 12 | 
            -
            Traceback (most recent call last):
         | 
| 13 | 
            -
              File "C:\Users\Owner\compare.py", line 2, in <module>
         | 
| 14 | 
            -
                from skimage.measure import structural_similarity as ssim
         | 
| 15 | 
            -
            ImportError: cannot import name 'structural_similarity' from 'skimage.measure' (C:\Anaconda3\envs\nnabla\lib\site-packages\skimage\measure\__init__.py)
         | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
            ### | 
| 1 | 
            +
            ###ソースコード
         | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 2 | 
            +
            #import the necessary packages
         | 
| 21 3 | 
             
            from skimage.measure import structural_similarity as ssim
         | 
| 22 4 | 
             
            import matplotlib.pyplot as plt
         | 
| 23 5 | 
             
            import numpy as np
         | 
| 24 6 | 
             
            import cv2
         | 
| 25 | 
            -
             | 
| 7 | 
            +
            %matplotlib inline
         | 
| 26 | 
            -
            ↑最初は%matplotlib inlineでした。
         | 
| 27 | 
            -
            そして実行するとinvalid syntaxエラーが起きました。
         | 
| 28 8 |  | 
| 29 9 | 
             
            def mse(imageA, imageB):
         | 
| 30 | 
            -
                 | 
| 10 | 
            +
                #the 'Mean Squared Error' between the two images is the
         | 
| 31 | 
            -
                 | 
| 11 | 
            +
                #sum of the squared difference between the two images;
         | 
| 32 | 
            -
                 | 
| 12 | 
            +
                #NOTE: the two images must have the same dimension
         | 
| 33 13 | 
             
                err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)
         | 
| 34 14 | 
             
                err /= float(imageA.shape[0] * imageA.shape[1])
         | 
| 35 15 |  | 
| 36 | 
            -
                 | 
| 16 | 
            +
                #return the MSE, the lower the error, the more "similar"
         | 
| 37 | 
            -
                 | 
| 17 | 
            +
                #the two images are
         | 
| 38 18 | 
             
                return err
         | 
| 39 19 |  | 
| 40 20 | 
             
            def compare_images(imageA, imageB, title):
         | 
| 41 | 
            -
                 | 
| 21 | 
            +
                #compute the mean squared error and structural similarity
         | 
| 42 | 
            -
                 | 
| 22 | 
            +
                #index for the images
         | 
| 43 23 | 
             
                m = mse(imageA, imageB)
         | 
| 44 24 | 
             
                s = ssim(imageA, imageB)
         | 
| 45 25 |  | 
| 46 | 
            -
                 | 
| 26 | 
            +
                #setup the figure
         | 
| 47 27 | 
             
                fig = plt.figure(title)
         | 
| 48 28 | 
             
                plt.suptitle("MSE: %.2f, SSIM: %.2f" % (m, s))
         | 
| 49 29 |  | 
| 50 | 
            -
                 | 
| 30 | 
            +
                #show first image
         | 
| 51 31 | 
             
                ax = fig.add_subplot(1, 2, 1)
         | 
| 52 32 | 
             
                plt.imshow(imageA, cmap = plt.cm.gray)
         | 
| 53 33 | 
             
                plt.axis("off")
         | 
| 54 34 |  | 
| 55 | 
            -
                 | 
| 35 | 
            +
                #show the second image
         | 
| 56 36 | 
             
                ax = fig.add_subplot(1, 2, 2)
         | 
| 57 37 | 
             
                plt.imshow(imageB, cmap = plt.cm.gray)
         | 
| 58 38 | 
             
                plt.axis("off")
         | 
| 59 39 |  | 
| 60 | 
            -
                 | 
| 40 | 
            +
                #show the images
         | 
| 61 41 | 
             
                plt.show()
         | 
| 62 42 |  | 
| 63 | 
            -
             | 
| 43 | 
            +
            #load the images -- the original, the original + contrast,
         | 
| 64 | 
            -
             | 
| 44 | 
            +
            #and the original + photoshop
         | 
| 65 45 | 
             
            original = cv2.imread("resize/re_pic006.jpg")
         | 
| 66 46 | 
             
            contrast = cv2.imread("resize/re_pic005.jpg")
         | 
| 67 47 | 
             
            shopped = cv2.imread("resize/re_pic003.jpg")
         | 
| 68 48 |  | 
| 69 | 
            -
             | 
| 49 | 
            +
            #convert the images to grayscale
         | 
| 70 50 | 
             
            original = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
         | 
| 71 51 | 
             
            contrast = cv2.cvtColor(contrast, cv2.COLOR_BGR2GRAY)
         | 
| 72 52 | 
             
            shopped = cv2.cvtColor(shopped, cv2.COLOR_BGR2GRAY)
         | 
| 73 53 |  | 
| 74 | 
            -
             | 
| 54 | 
            +
            #initialize the figure
         | 
| 75 55 | 
             
            fig = plt.figure("Images")
         | 
| 76 56 | 
             
            images = ("Original", original), ("Contrast", contrast), ("Photoshopped", shopped)
         | 
| 77 57 |  | 
| 78 | 
            -
             | 
| 58 | 
            +
            #loop over the images
         | 
| 79 59 | 
             
            for (i, (name, image)) in enumerate(images):
         | 
| 80 | 
            -
                 | 
| 60 | 
            +
                #show the image
         | 
| 81 61 | 
             
                ax = fig.add_subplot(1, 3, i + 1)
         | 
| 82 62 | 
             
                ax.set_title(name)
         | 
| 83 63 | 
             
                plt.imshow(image, cmap = plt.cm.gray)
         | 
| 84 64 | 
             
                plt.axis("off")
         | 
| 85 65 |  | 
| 86 | 
            -
             | 
| 66 | 
            +
            #show the figure
         | 
| 87 67 | 
             
            plt.show()
         | 
| 88 68 |  | 
| 89 | 
            -
             | 
| 69 | 
            +
            #compare the images
         | 
| 90 70 | 
             
            compare_images(original, original, "Original vs. Original")
         | 
| 91 71 | 
             
            compare_images(original, contrast, "Original vs. Contrast")
         | 
| 92 72 | 
             
            compare_images(original, shopped, "Original vs. Photoshopped")
         | 
| 93 73 |  | 
| 74 | 
            +
            ###
         | 
| 75 | 
            +
            ### やったこと
         | 
| 76 | 
            +
             | 
| 77 | 
            +
            http://kamonohashiperry.com/archives/699
         | 
| 78 | 
            +
            上記のページの「以下は、類似度計算の実行用コードとなります。」
         | 
| 79 | 
            +
            以降のプログラムを実行した際、
         | 
| 80 | 
            +
            以下のようなエラーが起こりました。
         | 
| 81 | 
            +
            原因はなんでしょうか?ご教示お願いします。
         | 
| 82 | 
            +
             | 
| 83 | 
            +
            ### 発生している問題・エラーメッセージ
         | 
| 84 | 
            +
             | 
| 85 | 
            +
            Traceback (most recent call last):
         | 
| 86 | 
            +
              File "C:\Users\Owner\compare.py", line 2, in <module>
         | 
| 87 | 
            +
                from skimage.measure import structural_similarity as ssim
         | 
| 88 | 
            +
            ImportError: cannot import name structural_similarity
         | 
| 94 89 | 
             
            ### 試したこと
         | 
| 95 90 |  | 
| 96 91 | 
             
            %matplotlib inlineだと「%」にinvalid syntaxエラーが吐かれた。
         | 
| 97 92 | 
             
            なので#でコメント化した。
         | 
| 93 | 
            +
            python3.7でやっていたので元ページと同じくpython2.7にした。
         | 
| 98 94 |  | 
| 99 95 | 
             
            ### 補足情報(FW/ツールのバージョンなど)
         | 
| 100 | 
            -
             | 
| 101 | 
            -
             | 
| 96 | 
            +
            pip install ssimをやって、
         | 
| 97 | 
            +
            ssim-0.2.2
         | 
| 98 | 
            +
            をインストールした。
         | 
