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
  Why I get the ValueError vokoyo 0 101 Mar-16-2019, 06:38 PM
Last Post: vokoyo
  How convert multidimensional array to two dimensional array tkkhan44 1 170 Feb-20-2019, 05:00 AM
Last Post: scidam
  ValueError: shapes naveen73 0 119 Jan-27-2019, 01:15 PM
Last Post: naveen73
  ValueError: Found input variables with inconsistent numbers of samples: [0, 3] ayaz786amd 2 1,071 Nov-27-2018, 07:12 AM
Last Post: ayaz786amd
  Help to arrange correct input array structure mogensen24 1 296 Oct-05-2018, 06:09 PM
Last Post: j.crater
  Array input with time module (Python 3) alrm31015 4 672 Jul-26-2018, 08:52 PM
Last Post: alrm31015
  ValueError: could not convert the string to float Grin 3 3,738 Jun-14-2018, 08:17 PM
Last Post: killerrex
  ValueError: The truth value of an array with more than one element is ambiguous. Eliza5 1 5,361 Apr-02-2018, 12:03 AM
Last Post: scidam
  Error: ValueError: could not convert string to float: 'L200 1.6 D/C' Jaarroy 2 2,462 Jan-18-2018, 02:00 PM
Last Post: Jaarroy
  ValueError: could not convert string to float: Close?? BlackHeart 3 14,529 Oct-27-2017, 08:42 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)