MATLAB 中二进制数到十进制数的转换
我在将二进制转换为十进制方面有问题(似乎很长)。
%% Read
clear all;
close all;
clc;
I=imread('test.png');
imshow(I);
%% Crop
I2 = imcrop(I);
figure, imshow(I2)
w=size(I2,1);
h=size(I2,2);
%% LBP
for i=2:w-1
for j=2:h-1
J0=I2(i,j);
I3(i-1,j-1)=I2(i-1,j-1)>J0;
I3(i-1,j)=I2(i-1,j)>J0;
I3(i-1,j+1)=I2(i-1,j+1)>J0;
I3(i,j+1)=I2(i,j+1)>J0;
I3(i+1,j+1)=I2(i+1,j+1)>J0;
I3(i+1,j)=I2(i+1,j)>J0;
I3(i+1,j-1)=I2(i+1,j-1)>J0;
I3(i,j-1)=I2(i,j-1)>J0;
LBP(i,j)=I3(i-1,j-1)*2^8+I3(i-1,j)*2^7+I3(i-1,j+1)*2^6+I3(i,j+1)*2^5+I3(i+1,j+1)*2^4+I3(i+1,j)*2^3+I3(i+1,j-1)*2^2+I3(i,j-1)*2^1;
end
end
figure,imshow(I3)
figure,imhist(LBP)
是否可以将此行更改
LBP(i,j)=I3(i-1,j-1)*2^8+I3(i-1,j)*2^7+I3(i-1,j+1)*2^6+I3(i,j+1)*2^5+I3(i+1,j+1)*2^4+I3(i+1,j)*2^3+I3(i+1,j-1)*2^2+I3(i,j-1)*2^1;
为短的东西?
I have a problem in converting the binary to decimal (it seems very lengthy).
%% Read
clear all;
close all;
clc;
I=imread('test.png');
imshow(I);
%% Crop
I2 = imcrop(I);
figure, imshow(I2)
w=size(I2,1);
h=size(I2,2);
%% LBP
for i=2:w-1
for j=2:h-1
J0=I2(i,j);
I3(i-1,j-1)=I2(i-1,j-1)>J0;
I3(i-1,j)=I2(i-1,j)>J0;
I3(i-1,j+1)=I2(i-1,j+1)>J0;
I3(i,j+1)=I2(i,j+1)>J0;
I3(i+1,j+1)=I2(i+1,j+1)>J0;
I3(i+1,j)=I2(i+1,j)>J0;
I3(i+1,j-1)=I2(i+1,j-1)>J0;
I3(i,j-1)=I2(i,j-1)>J0;
LBP(i,j)=I3(i-1,j-1)*2^8+I3(i-1,j)*2^7+I3(i-1,j+1)*2^6+I3(i,j+1)*2^5+I3(i+1,j+1)*2^4+I3(i+1,j)*2^3+I3(i+1,j-1)*2^2+I3(i,j-1)*2^1;
end
end
figure,imshow(I3)
figure,imhist(LBP)
Is it possible to change this line
LBP(i,j)=I3(i-1,j-1)*2^8+I3(i-1,j)*2^7+I3(i-1,j+1)*2^6+I3(i,j+1)*2^5+I3(i+1,j+1)*2^4+I3(i+1,j)*2^3+I3(i+1,j-1)*2^2+I3(i,j-1)*2^1;
to something shorter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
选项 1:
简化您正在做的事情的一种方法是首先创建一个比例因子(即 2 的幂)的 3×3 矩阵并在循环之前对其进行初始化:
然后您可以用这些矢量化替换循环内的所有内容操作:
选项 2:
或者,我相信您可以完全删除两个循环,并将它们替换为对 NLFILTER 获取矩阵
LBP
:Option 1:
One way to simplify what you're doing is to first create a 3-by-3 matrix of scale factors (i.e. powers of two) and initialize it before your loops:
Then you can replace everything inside your loop with these vectorized operations:
Option 2:
Alternatively, I believe you can remove both of your loops entirely and replace them with this single call to NLFILTER to get your matrix
LBP
:我不太确定你在那里做什么,但是
bin2dec
做你想做的事吗?I'm not exactly sure what you're doing there, but does
bin2dec
do what you want?我正在使用 COLFILT 函数添加另一个解决方案。
它涉及将所有滑动窗口放入矩阵的列中,我们使用自定义函数对其进行处理,然后将结果重新排列到原始矩阵中。在内部它使用 IM2COL 和 COL2IM 函数。
这是一个示例:
与 @gnovice 的第二个答案,请阅读 NLFILTER 文档:
I'm adding another solution with the COLFILT function.
It involves placing all sliding window into columns of a matrix, which we process using a custom function, then it rearranges the result into the original matrix. Internally it uses the IM2COL and COL2IM functions.
Here is an example:
With comparison to @gnovice's second answer, read this tip from the NLFILTER documentation: