for (int i = 0; i < numberOfSpaces; i++) {
System.out.print(" ");
}
这种方法并不简便,尤其多处需要输出的时候。
利用String.format可以简单的实现。
String space = String.format("%" + numberOfSpaces + "s", " ");
System.out.println(space);
颜色布局描述符 Color Layout Descriptor - CLD是用来描述颜色在图像中空间分布的颜色描述符。MPEG-7标准推荐了很多颜色描述符。包括可伸缩颜色描述符、颜色布局描述符和边缘直方图描述符等用于表示颜色。其中CLD因为可以有效的表示颜色的空间布局,广泛应用于图像检索技术。
这里不对CLD多做介绍(可参看http://en.wikipedia.org/wiki/Color_layout_descriptor)。在网上有很多关于CLD的文献,但是CLD在Matlab中的实现却很少,也很不完整。
以下是刚完成的CLD在Matlab中的实现,因为刚开始用Matlab,可能代码存在不足。
function Descriptor = ColorLayout(Image)
Img = Image;
%%颜色分割%%
%%代表色选择%%
[m,n,k] = size(Img);
cnum = 8;
ch = floor(m/cnum); cw = floor(n/cnum);
t1 = (0:(cnum-1))*ch + 1; t2 = (1:cnum)*ch;
t3 = (0:(cnum-1))*cw + 1; t4 = (1:cnum)*cw;
I = zeros([cnum,cnum,3]);
for i = 1 : cnum
for j = 1 : cnum
lstart=(i-1)*ch;
hstart=(j-1)*cw;
temp = Img(t1(i):t2(i), t3(j):t4(j), :);
for p=1:ch
for q=1:cw
temp(p,q,1)=Img(p+lstart,q+hstart,1);
temp(p,q,2)=Img(p+lstart,q+hstart,2);
temp(p,q,3)=Img(p+lstart,q+hstart,3);
end
end
temp1=temp(:,:,1);
temp2=temp(:,:,2);
temp3=temp(:,:,3);
tmp1 = temp1(:)';
tmp2 = temp2(:)';
tmp3 = temp3(:)';
I(i,j,1) = mean(tmp1);
I(i,j,2) = mean(tmp2);
I(i,j,3) = mean(tmp3);
end
end
%%DCT转换%%
I=rgb2ycbcr(I); %转换为YCbCr色彩空间
DCTY = I(:,:,1);
DCTCb = I(:,:,2);
DCTCr = I(:,:,3);
T=dctmtx(8);
DCTcoe1=blkproc(DCTY,[8,8],'P1*x*P2',T,T');
DCTcoe2=blkproc(DCTCb,[8,8],'P1*x*P2',T,T');
DCTcoe3=blkproc(DCTCr,[8,8],'P1*x*P2',T,T');
mask=[1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B1=blkproc(DCTcoe1,[8 8],'P1.*x',mask);
B2=blkproc(DCTcoe2,[8 8],'P1.*x',mask);
B3=blkproc(DCTcoe3,[8 8],'P1.*x',mask);
I(:,:,1)=blkproc(B1,[8 8],'P1*x*P2',T',T);
I(:,:,2)=blkproc(B2,[8 8],'P1*x*P2',T',T);
I(:,:,3)=blkproc(B3,[8 8],'P1*x*P2',T',T);
% Set up array for fast conversion from row/column coordinates to
% zig zag order. 下标从零开始,从MPEG的C代码拷贝过来的
zigzag = [ 0, 1, 8, 16, 9, 2, 3, 10, ...
17, 24, 32, 25, 18, 11, 4, 5, ...
12, 19, 26, 33, 40, 48, 41, 34, ...
27, 20, 13, 6, 7, 14, 21, 28, ...
35, 42, 49, 56, 57, 50, 43, 36, ...
29, 22, 15, 23, 30, 37, 44, 51, ...
58, 59, 52, 45, 38, 31, 39, 46, ...
53, 60, 61, 54, 47, 55, 62, 63];
zigzag = zigzag + 1;
% 将输入块变成3x64的向量
D(1,:) = reshape(I(:,:,1)',1,64);
D(2,:) = reshape(I(:,:,2)',1,64);
D(3,:) = reshape(I(:,:,3)',1,64);
Descriptor = D(:,zigzag); % 对I按照查表方式取元素,得到 zig-zag 扫描结果
[r,c,d] = size(rgbImage);
In that line of code:
ris the number of rows in the image (i.e. the size of the first dimension).cis the number of columns in the image (i.e. the size of the second dimension).dcan be referred to as the "depth" or "planes" of the image (i.e. the size of the third dimension).
For an RGB (or Truecolor) image, d is always 3. The first plane contains the degree of red in each pixel of the image,
the second plane contains the degree of green in each pixel of the image, and the third plane contains the degree of
blue in each pixel of the image.
[From]http://stackoverflow.com/questions/2612113/what-are-the-3-dimensions-of-an-rgb-image-in-matlab
-
为什么不使用float / double?
使用java时会遇到money类型的选择问题,首先想到的是float / double。如果只是简单的货币计算,很难发现用float会有问题。出现问题的原因是使用float / double(已经相应的包装类Float / Double)会出现舍入误差(rounding errors),不能精确的表示十进制数。例如下面的例子:
1 public class countMoney {
2 public static void main(String[] args) {
3 float m1 = 0.1f;
4 float m2 = 9.0f;
5 System.out.println(mulMoney(m1, m2));
6 }
7
8 private static float mulMoney(float m1, float m2) {
9 return m1 * m2;
10 }
11 }
结果输出0.90000004,而不是0.9。十进制数0.1用二进制存储,被表示的值为0.0999999999999999996。所以得到的结果并不是预期的0.9。
-
怎样表示money?
使用BigDecimal来表示货币。现在来重新实现这个例子:
1 public class countMoney {
2 public static void main(String[] args) {
3 BigDecimal m1 = new BigDecimal("0.1");
4 BigDecimal m2 = new BigDecimal("9.0");
5 System.out.println(mulMoney(m1, m2));
6 }
7
8 private static BigDecimal mulMoney(BigDecimal m1, BigDecimal m2) {
9 return m1.multiply(m2);
10 }
11 }
输出0.90。
BigDecimal可表示任意精度的小数,不管它的范围有多大。BigDecimal是用array来存储数字的,每一次输入表示一个数字。因此,BigDecimal可以表示任意大小的数。
另外,BigDecimal是一个类,不是基本类型,需要调用方法来实现数的运算。它有四中方法:add, subtract, multiply, 和 divide。这里m1和m2相乘采用了multiply。
-
表示money的其他方法?
用currency可表示世界货币。
使用int / long表示penny。
-
参考文献:
学习Java遇到各种API,快速查询和了解使用技巧成了问题。Java API Search(点击进入下载页面)添加了支持在Chrome
omnibox快速查找和自动完成Java 7 API类的功能。

安装这个插件后,只要在Chrome搜索栏中输入java加空格,就会出现Java API Search标志,如下图。

然后输入你要查询的函数名即可。
关于Ominibox:
可以把个人常用搜寻项搜集起来,例如,输入Halo,就可查询单词(中英文都可以)。

