STripathy
STripathy

Reputation: 59

How to store the 2D image pixels into 1d array?

I want to store the m(i,j) values in a 1D array of 2 rows. 1st row wd be the m(ij) counter and 2nd row w'd be m(ij) value for each iteration.How can i do that,? Please Help. want to convert 2D to 1D array like array(2,count).

     [e,f] = size(outImg);    
    m = zeros(e,f);    
    counter=0
    w = [1 2 4 8; 16 32 64 128; 256 512 1024 2048; 4096 8192 16384 32768];    
    for i = 1:e-3    
        for j = 1:f-3    
            I = double(outImg((i:i+3),(j:j+3)));  
            counter=counter+1;  
            m(i,j) = sum(sum(I .* w));   
        end     
    end

Upvotes: 0

Views: 1470

Answers (1)

rayryeng
rayryeng

Reputation: 104474

In your code, the quickest way (though not necessarily the most efficient, but most compatible with your code...) would be to allocate a 2D array and populate this array at each iteration. Referencing my previous post to you, the total number of times this loop iterates is (e-3) x (f-3) times. Therefore, allocate a 2D array like how you said in your post. Only this time, replace count with (e-3)*(f-3).

Something like the following:

[e,f] = size(outImg);    
m = zeros(e,f);    
counter=0;
counts = zeros(2, (e-3)*(f-3)); %// NEW
w = [1 2 4 8; 16 32 64 128; 256 512 1024 2048; 4096 8192 16384 32768];    
for i = 1:e-3    
    for j = 1:f-3    
        I = double(outImg((i:i+3),(j:j+3)));  
        counter=counter+1;  
        m(i,j) = sum(sum(I .* w));  
        counts(:,counter) = [counter; m(i,j)]; %// NEW
    end     
end

Therefore, you will have a 2D array called counts of size 2 x (e-3)*(f-3), where each column denotes the "iteration number" and the value of the summation for each pixel neighbourhood at each iteration in the loop.

Upvotes: 1

Related Questions