Is there an efficient way to set some of the Least Significant Bits (LSBs) to zero in RGB image with less computation speed?

9 visualizaciones (últimos 30 días)
Suppose, I have a image, A, and I have to set some of the LSBs to zero.
Say, Bit 0 to Bit 3 is set to zero. To do so, I have implemented the following code:
%%
A = imread('image.jpg'); % Reading a RGB image
tic;
I = dec2bin(A); %converting the image into binary
I(:, 5:8) = '0'; % replacing the 4 LSB bits (Bit 0 to Bit 3) to zero
J = bin2dec(I); % converting back to the original format
J = uint8(reshape(J, size(A))); %reshaping the output image to the input image size
toc;
%%%%%
This code performs the required task. However, it takes around 4.2 seconds for a single image. I need to perform this on more than 200 images.
So, is there any other efficient way to perform above said task?
Any support will be much appreciated. Thank you in advance.

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de En. de 2021
A = imread('flamingos.jpg');
tic;
I = dec2bin(A);
I(:,5:8) = '0';
J = reshape(uint8(bin2dec(I)), size(A));
toc
Elapsed time is 5.740375 seconds.
tic;
J = bitand(A, 0xf0u8);
toc
Elapsed time is 0.003683 seconds.
tic
J = A - mod(A, uint8(16));
toc
Elapsed time is 0.005185 seconds.
tic
J = uint8(floor(double(A)/16)*16);
toc
Elapsed time is 0.020407 seconds.
tic
J = bitshift(bitshift(A, 4), -4);
toc
Elapsed time is 0.003269 seconds.
So bitshifts is fastest of the methods tried, then bitand(), then subtracting the mod, and the classic approach of division and multiplication is slowest of the mathematical transforms. Relative timings of bitshift compared to bitand is inconsistent; some of the measurements showed bitand to be faster than the bitshift approach, but more of the time bitshift was slightly faster.
Note: you might be tempted to try just plain A/16*16 but that will not work. Division in integer data types is defined to round not to truncate, so for example uint8(3)/2 would be 2 (1.5 rounded) not 1 (1.5 truncated)
I would be more likely to use the bitand() approach rather than the double bit-shift: it provides more flexibility with less thought.
  3 comentarios
Walter Roberson
Walter Roberson el 30 de En. de 2021
Odd, that is surprisingly slow.
A = imread('flamingos.jpg');
tic;
J = idivide(A,16,'floor')*16;
toc
Elapsed time is 0.045668 seconds.
Manisha N
Manisha N el 30 de En. de 2021
Thank you very much! I am able to execute this much faster now.
In the case of bitshift, shouldn't it be bitshift(bitshift(A, -4), 4) instead of bitshift(bitshift(103, 4), -4)?

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by