Faster version of dec2bin and bin2dec function for an image
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Gayathri Vijay
el 24 de Abr. de 2017
Comentada: Walter Roberson
el 5 de Mayo de 2019
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 comentarios
Respuesta aceptada
Jan
el 24 de Abr. de 2017
Editada: Jan
el 24 de Abr. de 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 comentario
Rik
el 24 de Abr. de 2017
This time you were faster ;)
(and your solution is about twice as fast as mine)
Más respuestas (3)
Rik
el 24 de Abr. de 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
0 comentarios
Steven Lord
el 24 de Abr. de 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
el 5 de Mayo de 2019
Editada: Rose Mahmudi
el 5 de Mayo de 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 comentarios
Rose Mahmudi
el 5 de Mayo de 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
el 5 de Mayo de 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)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!