Azzera filtri
Azzera filtri

Faster version of dec2bin and bin2dec function for an image

8 visualizzazioni (ultimi 30 giorni)
I am reading a tiff file (256*256) and converting each of the pixels values ranging from 0 to 255 to binary matrix, after processing the binary matrix is converted back into decimal matrix using the dec2bin and bin2dec functions.The original dec2bin function was too slow, time taken approx. 4sec on my machine (I3 processor, 2Ghz RAM and matlab R2010)for the conversion.
Is there anyway to speed things up to be under 1sec or even better 0.5sec, as every msec counts for my application?
Here is my code:
img = imread('cameraman.tif');
D1=256;D2=256;
for i=1:1:D1
for j=1:1:D2
b1{i,j,1}=dec2bin(img(i,j),8);
end
end
I = cell2mat(b1);
(Processing)
a1{D1,D2} = [];
for i=1:1:D1
for j=1:1:D2
a1{i,j,1}=I(i,:,j);
end
end
C1 = bin2dec(a1);
Thanks in advance
  3 Commenti
Gayathri Vijay
Gayathri Vijay il 26 Apr 2017
Is there any solution for the second part of the code?? faster versions of bin2dec function???
Thanks for the help. I have done the modification as suggested and its grt...
Gayathri Vijay
Gayathri Vijay il 26 Apr 2017
tic img = randi([0, 255], 256, 256); % Example data [D1,D2]=size(img); I2=mat2cell(dec2bin(img,8),ones(65536,1),8); I3=cell2mat(reshape(I2',256,256)); toc
tic lut = cell2mat(arrayfun(@(i)bitget([0:255]',9-i),1:8,'UniformOutput',0)); binI = reshape((lut(img(:)+1,:)),256,2048); I = reshape((sprintf('%d',binI)),256,2048); toc
tic; imgD = dec2bin(img(:)).'; I2 = reshape(permute(reshape(imgD, 8, [], D2), [2,1,3]), D1, D2 * 8); toc isequal(I3, I2)
Elapsed time is 0.876447 seconds. Elapsed time is 0.572147 seconds. Elapsed time is 0.091979 seconds.

Accedi per commentare.

Risposta accettata

Jan
Jan il 24 Apr 2017
Modificato: Jan il 24 Apr 2017
dec2bin is not the problem, but the repeated calling.
img = randi([0, 255], 256, 512); % Example data
tic;
[D1, D2] = size(img);
b1 = cell(D1, D2); % Pre-allocate!
for i=1:1:D1
for j=1:1:D2
b1{i,j} = dec2bin(img(i,j),8);
end
end
I1 = cell2mat(b1);
toc
tic;
imgD = dec2bin(img(:)).';
I2 = reshape(permute(reshape(imgD, 8, [], D2), [2,1,3]), D1, D2 * 8);
toc
isequal(I1, I2)
> Elapsed time is 2.501129 seconds.
> Elapsed time is 0.035261 seconds.
  1 Commento
Rik
Rik il 24 Apr 2017
This time you were faster ;)
(and your solution is about twice as fast as mine)

Accedi per commentare.

Più risposte (3)

Rik
Rik il 24 Apr 2017
I2=mat2cell(dec2bin(img,8),ones(65536,1),8);
I3=cell2mat(reshape(I2',256,256));
checkval=I-I3;
any(checkval(:))
This code should be around 3 times as fast

Steven Lord
Steven Lord il 24 Apr 2017
Consider using the bitget and bitset functions on your image array all at once, rather than processing each individual element. Note that the online documentation is for the most recent release, and there was at least one change to their capabilities between the release you're using and the current release, so you should review the documentation installed in your MATLAB installation to be certain of the capabilities of these functions in that release.

Rose Mahmudi
Rose Mahmudi il 5 Mag 2019
Modificato: Rose Mahmudi il 5 Mag 2019
please someone help me
heloo everybody
I desperately edited my code for 2 months and I couldn't get it done.
it took so much time
I thought it's because of my fuzzy fis I read or dec2bin code but I find out it's because this part of cod I'm obtaining the 8-conectivity pixles for each pixle:
for col = 2 : columns-1
output(index, 2) = col;
for row = 2 : rows-1
this3x3Window = B(row-1:row+1, col-1:col+1);
output(index, 1) = row;
output(index,1:3) = this3x3Window(1:3);
output(index,3:6) = this3x3Window(3:6);
output(index,6:9) = this3x3Window(6:9);
index = index + 1;
end
end
save('pixel_neighbors.mat', 'output');
how can I write it to be have a good tic toc :)
  3 Commenti
Rose Mahmudi
Rose Mahmudi il 5 Mag 2019
thank you for your answer.
the work I'm doing is I want to obtain 8-connectivity pixles for each pixle and save them in a matrix and use them in a fuzzy interfenc with 9 input and one output to change the graylevel value of each pixle based on fuzzy rules.
I want to change the gray level value of image by fuzzy.
the technics you sujesting to me I don't know if they are better or slower??
can you help me please
appreciate your help
Walter Roberson
Walter Roberson il 5 Mag 2019
You should start a new Question with your code and your problem description.
You have bugs in your code. You are overwriting column 2 (originally you save col there) and 1 (originally you save row there)

Accedi per commentare.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by