Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 ValueError: could not broadcast input array from shape (75) into shape (25)
#1
I am a Python beginner. I am trying to follow the tutorial posted here about change detection for satellite images.

I am receiving a value error. Can anyone guide me towards resolving the issue.


import cv2
import numpy as np
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from collections import Counter
from scipy.misc import imread, imresize, imsave

def find_vector_set(diff_image, new_size):
   
    i = 0
    j = 0
    vector_set = np.zeros(((int(new_size[0]) * int(new_size[1])) // 25, 25))
    while i < vector_set.shape[0]:
        while j < new_size[0]:
            k = 0
            while k < new_size[1]:
                block   = diff_image[j:j+5, k:k+5]
                feature = block.ravel()
                vector_set[i, :] = feature
                k = k + 5
            j = j + 5
        i = i + 1
        
    print ('vector_set shape', vector_set.shape)
            
    mean_vec   = np.mean(vector_set, axis = 0)    
    vector_set = vector_set - mean_vec
    
    return vector_set, mean_vec
    
  
def find_FVS(EVS, diff_image, mean_vec, new):
    
    i = 2 
    feature_vector_set = []
    
    while i < new[0] - 2:
        j = 2
        while j < new[1] - 2:
            block = diff_image[i-2:i+3, j-2:j+3]
            feature = block.flatten()
            feature_vector_set.append(feature)
            j = j+1
        i = i+1
        
    FVS = np.dot(feature_vector_set, EVS)
    FVS = FVS - mean_vec
    print ("feature vector space size", FVS.shape)
    return FVS

def clustering(FVS, components, new):
    
    kmeans = KMeans(components, verbose = 0)
    kmeans.fit(FVS)
    output = kmeans.predict(FVS)
    count  = Counter(output)

    least_index = min(count, key = count.get)            
    change_map  = np.reshape(output,(new[0] - 4, new[1] - 4))
    
    return least_index, change_map

   
def find_PCAKmeans(imagepath1, imagepath2):
    
    print ('Operating')
    
    image1 = imread(imagepath1)
    image2 = imread(imagepath2)
    
    new_size = np.asarray(image1.shape) // 25
    image1 = imresize(image1, (new_size)).astype(np.int16)
    image2 = imresize(image2, (new_size)).astype(np.int16)
    
    diff_image = abs(image1 - image2)   
    imsave('diff.jpg', diff_image)
    print ('Both images resized to ',new_size)
        
    vector_set, mean_vec = find_vector_set(diff_image, new_size)
    
    pca     = PCA()
    pca.fit(vector_set)
    EVS = pca.components_
        
    FVS     = find_FVS(EVS, diff_image, mean_vec, new_size)
    
    print ('computing k means')
    
    components = 3
    least_index, change_map = clustering(FVS, components, new_size)
    
    change_map[change_map == least_index] = 255
    change_map[change_map != 255] = 0
    
    change_map = change_map.astype(np.uint8)
    kernel     = np.asarray(((0,0,1,0,0),
                             (0,1,1,1,0),
                             (1,1,1,1,1),
                             (0,1,1,1,0),
                             (0,0,1,0,0)), dtype=np.uint8)
    cleanChangeMap = cv2.erode(change_map,kernel)
    imsave("changemap.jpg", change_map)
    imsave("cleanchangemap.jpg", cleanChangeMap)

    
if __name__ == "__main__":
    a = 'Argentina_01131994_md.jpg'
    b = 'Argentina_01202014_md.jpg'
    find_PCAKmeans(a,b)    
    
OUTPUT :
File "D:/Numer8/change_detection4.py", line 26, in find_vector_set
vector_set[i, :] = feature

ValueError: could not broadcast input array from shape (75) into shape (25)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  valueError: expected 2d, got 1d instead brkolvr 1 113 Aug-16-2019, 02:43 AM
Last Post: boring_accountant
  ValueError: The truth value of a Series is ambiguous ? firebird 1 185 Aug-01-2019, 12:33 AM
Last Post: scidam
  ValueError: Input contains infinity or a value too large for dtype('float64') Rabah_r 1 1,119 Apr-06-2019, 11:08 AM
Last Post: scidam
  How to use pandas.rolling mean for 3D input array? Prv_Yadv 1 385 Mar-26-2019, 11:49 AM
Last Post: scidam
  Why I get the ValueError vokoyo 0 447 Mar-16-2019, 06:38 PM
Last Post: vokoyo
  How convert multidimensional array to two dimensional array tkkhan44 1 340 Feb-20-2019, 05:00 AM
Last Post: scidam
  ValueError: shapes naveen73 0 286 Jan-27-2019, 01:15 PM
Last Post: naveen73
  ValueError: Found input variables with inconsistent numbers of samples: [0, 3] ayaz786amd 2 3,052 Nov-27-2018, 07:12 AM
Last Post: ayaz786amd
  Help to arrange correct input array structure mogensen24 1 489 Oct-05-2018, 06:09 PM
Last Post: j.crater
  Array input with time module (Python 3) alrm31015 4 890 Jul-26-2018, 08:52 PM
Last Post: alrm31015

Forum Jump:


Users browsing this thread: 1 Guest(s)