MATLAB 中二进制数到十进制数的转换

发布于 2024-12-04 07:38:05 字数 929 浏览 0 评论 0原文

我在将二进制转换为十进制方面有问题(似乎很长)。

%% 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

∝单色的世界 2024-12-11 07:38:05

选项 1:

简化您正在做的事情的一种方法是首先创建一个比例因子(即 2 的幂)的 3×3 矩阵并在循环之前对其进行初始化:

scale = 2.^[8 7 6; 1 -inf 5; 2 3 4];

然后您可以用这些矢量化替换循环内的所有内容操作:

temp = (I2(i-1:i+1,j-1:j+1) > I2(i,j)).*scale;
LBP(i,j) = sum(temp(:));

选项 2:

或者,我相信您可以完全删除两个循环,并将它们替换为对 NLFILTER 获取矩阵 LBP

LBP = nlfilter(I2,[3 3],@(x) sum((x(:) > x(5)).*scale(:)));

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:

scale = 2.^[8 7 6; 1 -inf 5; 2 3 4];

Then you can replace everything inside your loop with these vectorized operations:

temp = (I2(i-1:i+1,j-1:j+1) > I2(i,j)).*scale;
LBP(i,j) = sum(temp(:));

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:

LBP = nlfilter(I2,[3 3],@(x) sum((x(:) > x(5)).*scale(:)));
芸娘子的小脾气 2024-12-11 07:38:05

我不太确定你在那里做什么,但是 bin2dec 做你想做的事吗?

I'm not exactly sure what you're doing there, but does bin2dec do what you want?

岁月流歌 2024-12-11 07:38:05

我正在使用 COLFILT 函数添加另一个解决方案。

它涉及将所有滑动窗口放入矩阵的列中,我们使用自定义函数对其进行处理,然后将结果重新排列到原始矩阵中。在内部它使用 IM2COLCOL2IM 函数。

这是一个示例:

I = imread('coins.png');
fun = @(b) sum( bsxfun(@times, 2.^(8:-1:1)', ...
           bsxfun(@gt, b([1 4 7 8 9 6 3 2],:), b(5,:))) );
II = colfilt(I, [3 3], 'sliding', fun);
imshow(II, [])

screenshot

@gnovice 的第二个答案,请阅读 NLFILTER 文档:

nlfilter 处理大图像可能需要很长时间。在某些情况下,
colfilt 函数可以更快地执行相同的操作。

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:

I = imread('coins.png');
fun = @(b) sum( bsxfun(@times, 2.^(8:-1:1)', ...
           bsxfun(@gt, b([1 4 7 8 9 6 3 2],:), b(5,:))) );
II = colfilt(I, [3 3], 'sliding', fun);
imshow(II, [])

screenshot

With comparison to @gnovice's second answer, read this tip from the NLFILTER documentation:

nlfilter can take a long time to process large images. In some cases,
the colfilt function can perform the same operation much faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文