![]() |
钻头旋转操作
有没有办法使位旋转操作可逆?我的意思是,如果有图像X(大小256 * 256 * 3),则在进行位旋转时会获得图像Y。然后,对Y进行一点旋转,我们得到图像X。此外,如何解决位溢出问题,从而不会造成信息丢失。
[B]回答:[/B] [B]更新:[/B]我已经采用了下面发布的代码,并将其完善为具有错误检查,帮助文档[URL="http://www.mathworks.com/matlabcentral/fileexchange/30679-bitrotate"]的完整功能[/URL] ,并且能够对无符号整数[I]和[/I]双精度变量的数组进行操作,就像相关的内置函数[URL="http://www.mathworks.com/help/techdoc/ref/bitshift.html"]BITSHIFT一样[/URL] 。我建议使用上面链接到的新版本,而不要使用下面发布的旧版本。 MATLAB没有内置的位旋转功能,而[URL="http://www.mathworks.com/help/techdoc/ref/bitshift.html"]BITSHIFT[/URL]函数将丢弃溢出的位。但是,您可以根据[URL="http://www.mathworks.com/help/techdoc/ref/f16-42340.html#f16-7080"]现有的位操作[/URL]实现自己的位旋转功能。这是我放在一起的一个简单的首过版本(没有错误检查): function data = bit_rotate(data,nBits) dataBits = log2(double(intmax(class(data)))+1); %# Number of bits in data nBits = rem(nBits,dataBits); %# No need to rotate by dataBits bits or more if nBits == 0 %# No bit rotation needed, just return return end shiftedData = bitshift(data,nBits); %# Bit shift the data lostData = bitxor(data,bitshift(shiftedData,-nBits)); %# Find the lost bits rotatedData = bitshift(lostData,nBits-sign(nBits)*dataBits); %# Rotate them data = shiftedData+rotatedData; %# Add the rotated bits to the shifted bits end 这是一些测试数据: >> B = uint8(208); %# An unsigned 8-bit integer value >> disp(dec2bin(B,8)) %# Display the bit pattern of B 11010000 >> disp(dec2bin(bit_rotate(B,2),8)) %# Rotate left by 2 bits 01000011 >> disp(dec2bin(bit_rotate(B,-2),8)) %# Rotate right by 2 bits 00110100 请注意, bit_rotate还将对data任何大小矩阵输入进行操作,只要它是无符号整数类型即可。 [url=https://stackoverflow.com/questions/5081979]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 05:02。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.