Is there an efficient way to set some of the Least Significant Bits (LSBs) to zero in RGB image with less computation speed?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Manisha N
le 30 Jan 2021
Commenté : Manisha N
le 30 Jan 2021
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.
0 commentaires
Réponse acceptée
Walter Roberson
le 30 Jan 2021
A = imread('flamingos.jpg');
tic;
I = dec2bin(A);
I(:,5:8) = '0';
J = reshape(uint8(bin2dec(I)), size(A));
toc
tic;
J = bitand(A, 0xf0u8);
toc
tic
J = A - mod(A, uint8(16));
toc
tic
J = uint8(floor(double(A)/16)*16);
toc
tic
J = bitshift(bitshift(A, 4), -4);
toc
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 commentaires
Walter Roberson
le 30 Jan 2021
Odd, that is surprisingly slow.
A = imread('flamingos.jpg');
tic;
J = idivide(A,16,'floor')*16;
toc
Plus de réponses (0)
Voir également
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!