Python Forum
ValueError: could not broadcast input array from shape (75) into shape (25)
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Issues with Shape/Reshape for CNN moddy10 0 312 Oct-12-2021, 03:54 PM
Last Post: moddy10
  RandomForest --ValueError: setting an array element with a sequence JaneTan 0 532 Sep-08-2021, 02:12 AM
Last Post: JaneTan
  ValueError: Found input variables with inconsistent numbers of samples: [5, 6] bongielondy 6 16,592 Jun-28-2021, 05:23 AM
Last Post: ricslato
  Making a Basic Keras Model - Input Shape and Parameters MattKahn13 0 1,135 Aug-16-2020, 04:36 PM
Last Post: MattKahn13
  cannot reshape array of size 0 into shape Roro 2 4,124 Jun-14-2020, 11:28 AM
Last Post: Roro
  Python changed array shape without telling me BrendanGolledge 3 1,054 Jun-11-2020, 07:41 AM
Last Post: Yoriz
  ValueError: Found array with 0 samples marcellam 1 2,665 Apr-22-2020, 04:12 PM
Last Post: jefsummers
  ValueError: Found input variables with inconsistent numbers of sample robert2joe 0 2,789 Mar-25-2020, 11:10 AM
Last Post: robert2joe
  ValueError: Found input variables AhmadMWaddah 3 2,164 Mar-03-2020, 10:19 PM
Last Post: AhmadMWaddah
  Bad input shape for SVC Scott 1 3,266 Dec-07-2019, 08:03 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020