微软的文档……

December 11, 2007 1:04 am GMT-0700 | In Tech | 3 Comments | hide

同样是字符串里找子串的函数,微软 VB 的文档:

Returns an integer specifying the start position of the first occurrence of one string within another.

Public Shared Function InStr(_
    ByVal String1 As String, _
    ByVal String2 As String, _
    Optional ByVal Compare As CompareMethod _
) As Integer

Parameters

String1
Required. String expression being searched.

String2
Required. String expression sought.

读完了一头雾水,TMD 到底哪个参数是要找的子串??being searched?sought??我英语不好也不要这么欺负我啊。再看微软 VC++ 类似函数的文档:

Return a pointer to the first occurrence of a search string in a string.

char *strstr(
   const char *str,
   const char *strSearch
); // C only

Parameters

str
Null-terminated string to search.

strSearch
Null-terminated string to search for.

Return Value
Returns a pointer to the first occurrence of strSearch in str

VC++ 里解释比 VB 的稍好一些,但是还是不清不楚的,功能说明为啥不写清楚哪个 string 是哪个呢?变量名为什么要写得这么诡异呢?参数说明里一个是 search,一个是 search for,TMD 又欺负我英语差!像这种常用函数,一般人都知道大概怎么用,只是偶尔不确定看一下参数顺序,你这么个文档还得让人费脑细胞琢磨,要你何用!再看 PHP 里类似的函数:

int strpos ( string $haystack, mixed $needle [, int $offset ] )

Returns the numeric position of the first occurrence of needle in the haystack string.

PHP 这个文档光看参数名就足以明白参数顺序了,看了功能解释就更清楚了。MacOS 的 man page:

char *strstr(const char *big, const char *little);

The strstr() function locates the first occurrence of the null-terminated string little in the null-terminated string big.

有一百种方法可以把这个函数的参数顺序写的很明确,但是微软偏偏选择了不明朗的写法。想起一般微软的程序很少有人去看 HELP 解决问题,而 MacOS 和 *nix 下面看文档则是家常便饭,看来微软文档无用论还是很有道理的……

Tags: , ,

几张 Maui 的照片

December 8, 2007 11:21 pm GMT-0700 | In Life | No Comments | hide

试了一下 HDR,发现即使土鳖相机也勉强可以拍,主要是三角架 + 锁定测光。下面三张小图片是同一位置三次拍摄的原始照片,曝光补偿分别是 -2,0,+2。下面的大图是用 Photoshop Merge HDR 的图片。

009-011hdr.jpg

下面这张是深夜 15 秒曝光的照片,我没有调过白平衡,当时的雾气/天空就是暗红色的,拍摄的时候周围也没有人造光影响:

img_7122.JPG

云上日出前看到的启明星:

img_6883_1.jpg

走到哪里,都要跪拜康神:

img_6728.JPG

img_6733.JPG

更多材料请出门右转

Tags: , , ,

Maui 总结

December 4, 2007 11:11 pm GMT-0700 | In Life | 1 Comment | hide

到 Maui (夏威夷第二大岛)去了一趟,小结如下:天气一般,赶上很不常见的大暴雨,不过天气好的时候星星很好很明亮;早上三点半起来上山看日出,云上日出很好很强大;下山穿浓云开车,能见度 10 米左右,很好很刺激;另有一段山路只有一条道,碰到需要在悬崖峭壁边倒车会车的情况,很好很惊险;早晨在海边看到了很清楚的二级彩虹(霓),很好很牛鼻。

Tags: ,

科学是一种信仰吗?

December 4, 2007 11:12 am GMT-0700 | In Study | No Comments | hide

推荐 Paul Davies 的文章:Taking Science on Faith

科学和信仰

另引用一下我 1997 年一片土鳖文章的土鳖观点:

……人们还认为“逻辑上唯一可行的理论即是自然的终极理论” 。这难道不是反映了人的高傲本性么?“逻辑上唯一”?是否有唯一?逻辑已经是完备的了?逻辑是万能的吗?人总是有这样一种高姿态,想从纯粹的思维上去把握实在,这的确是一种美的追求(简单性、和谐性),Einstein 也这样说过。但是这是一个长期的发展过程,逻辑与自然理论都尚需作进一步发展,至少在现在,纯粹的思维是难以把握实在的。

另引用霍金《时间简史》 第一章末的观点:

Now, if you believe that the universe is not arbitrary, but is governed by definite laws, you ultimately have to combine the partial theories into a complete unified theory that will describe everything in the universe. But there is a fundamental paradox in the search for such a complete unified theory. The ideas about scientific theories outlined above assume we are rational beings who are free to observe the universe as we want and to draw logical deductions from what we see. In such a scheme it is reasonable to suppose that we might progress ever closer toward the laws that govern our universe. Yet if there really is a complete unified theory, it would also presumably determine our actions. And so the theory itself would determine the outcome of our search for it! And why should it determine that we come to the right conclusions from the evidence? Might it not equally well determine that we draw the wrong conclusion? Or no conclusion at all?

The only answer that I can give to this problem is based on Darwin’s principle of natural selection. The idea is that in any population of self-reproducing organisms, there will be variations in the genetic material and upbringing that different individuals have. These differences will mean that some individuals are better able than others to draw the right conclusions about the world around them and to act accordingly. These individuals will be more likely to survive and reproduce and so their pattern of behavior and thought will come to dominate. It has certainly been true in the past that what we call intelligence and scientific discovery have conveyed a survival advantage. It is not so clear that this is still the case: our scientific discoveries may well destroy us all, and even if they don’t, a complete unified theory may not make much difference to our chances of survival. However, provided the universe has evolved in a regular way, we might expect that the reasoning abilities that natural selection has given us would be valid also in our search for a complete unified theory, and so would not lead us to the wrong conclusions.

Tags: , , ,

Defective Pixels in Canon A570 IS

November 25, 2007 6:12 pm GMT-0700 | In Tech | 8 Comments | hide

We know almost every pixel array (CCD sensor in camera, LCD screen, etc) has defective pixels. For camera CCD sensor, a few defective pixels can ruin good pictures, but we usually don’t see those pixels in the JPEG picture. Here’s a short story about those defective pixels.

I installed CHDK on my Canon A570 IS camera and shot a few test pictures to find out defective pixels of my camera. To my surprise, this 7.1 mega-pixel CCD sensor has couple thousands defective pixels. The following picture represents the raw output from CCD sensor when I shot a 15 sec picture with ridiculously bright light source: (The green border is just showing image boundary, not from the actual picture.)

0486.png

The raw file is 3152 columns by 2340 rows, 10 bits per pixel, so the file size is 3152 x 2340 x 10 bits = 9219600 bytes. The first few columns and rows are used for background subtraction (masked pixels, their values are usually around 30-40 on this 210 = 1024 scale), so the active area is actually 3072 x 2304 pixels. The above picture is RAW CCD output down scaled to 0-255 and converted to 8-bit gray PNG (i.e., before demosaicing/gamma correction/any other process taken place). As you can see, there’re a lot (9205) of 0-valued defective pixels in the active area. There is no defective pixel in the masked area.

When I shot with faster shutter speed, I have less (2991) defective pixels. The following was shot with 1/250 sec shutter speed, showing CCD value 0 as black (defective pixels), 1-40 as gray (masked pixels), and 40-267 as white. No pixel has value more than 267.

0488.png

I shot a lot of RAW pictures and found the number of defective pixels is either 9205 or 2991. Generally speaking, the slower the shutter speed, the more chance I get 9205 defective pixels. But I did have 2991 defective pixels with 1/30 sec picture, and 9205 defective pixels with 1/250 sec picture, with all other settings the same. So I have no idea what determines the defective pixel pattern. Also what’s interesting is those 2991 defective pixels are subset of the 9205 defective pixels.

If I go ahead and demosaic, combining RGB channels together, This is part of the image I get: (You can see defective pixels in different color channels.)

0405-with-bad-pixel-small.png

Instead, most RAW format processors find those 0-valued (sub)pixels and interpolate over with neighbor pixels. By doing that, here’s what I get: (white balance etc. was tuned a little bit as well)

0405-without-bad-pixel-small.png

Nicely white, no annoying dots any more, right? This “cheating” process is done inside the camera when saving JPEG file, and also done in most RAW format conversion. DNG specification states that:

Defective pixels should be mapped out (interpolated over) before the raw data is stored as DNG.

Enjoy your camera with 0.1% defective pixels!

Tags: , , , , , , ,

看火星,拍照片

November 24, 2007 10:20 pm GMT-0700 | In Study, Tech | 1 Comment | hide

前几天做实验做得比较晚,同事发现天顶有颗很亮的红色星星,经考证,为火星。下面是我拿 Canon A570 IS 拍摄的照片的缩略图,如果熟悉星空,抬头找到双子座和猎户座,几乎是不可能没看到火星的。

Mars photo smaller

(点进去可看原始分辨率大图)我的拍摄时间/地点是美西 11 月 24 日凌晨 1 点,西经 122 度,对照下面的 Stellarium 软件给出的星空图,我拍摄照片没有漏掉猎户座(Orion)和双子座(Gemini)的任何一颗哪怕再暗的星星,而当时我用肉眼根本认不全星座。

sky map

看起来民用相机要拍个简单的星空还是有可能的。由于镜头不够广角,我是拍了两个位置的照片拼接起来的,每个位置拍了三张 15 秒曝光的照片。照相机是标准 35 mm 镜头,横向视角 54 度覆盖 3072 个像素,所以在这 15 秒钟的曝光中,星星大概移动过 15 sec/(3600*24 sec) * 360 deg * 3072 pixel/54 deg = 四个像素;而且,15 秒曝光需要额外 15 秒去噪和一些存照片的时间,所以相邻两张照片的间隔大概是 40 秒钟,也即相差大概 10 个像素。下面是同一位置三张连续拍摄的照片直接叠加后的猎户座参宿四(Betelgeuse)区域的放大:

pos1.png

可以看到,星星的移动确实比较符合上面的计算。我需要在同一个位置拍三张的目的是为了取得更长时间曝光的效果,并且同时也能够平均掉一些噪声的影响(理论上说,在 CHDK 上写脚本拍摄可以获得更好的效果)。整个后期处理过程是:用 Matlab 自动寻找亮星位置确定偏移量叠加同一个位置的三张照片,然后到 Photoshop 旋转合成两个位置的照片,最后调整对比度和亮度以获得比较好的效果。

最后说一下火星,火星大概每两年绕太阳一圈,地球也会每两年接近一次火星,由于火星轨道在地球外面,所以接近的时候可以在夜晚很清楚的看到火星。现在看火星已经问题不大,不过要等到下个月中旬火星才最接近地球(火星轨道图),而且那个时候夜空没有月亮,看火星将会更加合适。

Tags: , , , ,

颜色的学问

November 18, 2007 10:48 pm GMT-0700 | In Study | 6 Comments | hide

一般人都知道各类颜色可以由三原色调出来,最常用的三原色是红绿蓝(RGB)。不过这个颜色的问题深究一下,貌似还是一个蛮复杂的问题……用最简单的话说,环境光线在物体表面反射到人眼睛里面,刺激视网膜产生神经电信号传送到大脑,大脑就有了颜色的感觉。

光有一个最基本的特性就是波长,不同波长的单色光颜色是不一样的:

visibal light spectrum

可以看到,可见的光波长大概是 400 纳米到 700 纳米。环境光在物体表面反射,加上物体自己辐射的光,进入人眼睛就会刺激视网膜上的视锥细胞。视锥细胞有三种类型,对波长的感应不一样:

cone response

三种视锥细胞最敏感的光波长分别是:S 细胞 424 纳米,M 细胞 530 纳米,L 细胞 560 纳米(这三个波长上的光其实并不是红绿蓝,所以比方把 L 细胞叫做红视锥细胞不是很准确。 )。物体反射光的频谱刺激三种视锥细胞产生三个不同的响应,然后后面就会有一连串的生理电反应直到刺激大脑视觉系统(这个具体的过程现在还没有最终定论)。可以想象,不同的频谱可能会产生相同的视锥细胞响应,比如将单色的红绿蓝三种光用适当比例混合产生的白光,和太阳白光看起来没什么区别,但频谱却完全不同(Metamerism 现象)。用集合论的话说,真正的物体颜色(用反射频谱表示)到人眼感觉颜色的映射是多对一的映射。也正是由于这个原因,照相机才可以用三原色模拟视锥细胞感应来过滤光线采集 RGB 三个颜色通道,而显示器也只要用 RGB 三个色彩来还原照片即可,完全不需要模拟物体反射的复杂频谱。

在实际中,摄影会受到光照的影响,因为物体反射的光频谱实际上就是物体反射率(一个波长的函数) 乘以环境光频谱。幸运的是,很多情况下,环境光和闪光灯频谱基本上是纯白,也即各个波长上的光强度差不多,所以物体反射的光频谱基本只由物体本身的反射率函数决定。一般环境光的特性用色温来表示,表示这个环境光的频谱最接近什么温度的黑体辐射。对于热发光的物体,这个色温基本就是发光体的温度。比如太阳表面 5500 K,正午太阳白光的色温也差不多是这个温度。蜡烛火温度稍低,只有 2000 K 左右,于是光也偏红一些。对于非热发光的物体(比如日光灯是荧光发光,完全不同的发光机理),就需要看它大概和什么温度下的黑体辐射频谱对人眼产生一样的刺激来决定色温。不同色温的光线其表现颜色也不同:

color temperature

低色温的光主要能量都在红外(长波长),光看起来偏红;很高色温的光主要能量移向紫外(短波长),光就看起来偏蓝;中间温度就是白光,也即所有可见光波段的能量都差不多多。比较有趣的是一般蓝色被认为是冷色调,而高色温的光源却是偏蓝的。这个的解释是日常生活中的光不会到一万 K 的色温,人们一般认为东西变红就是热,于是只有红黄等色才可能是暖色调;而湖水啊冰啊什么的都会呈现绿色蓝色,于是这些就被认为是冷色调,实际上这个蓝色出现的机理并不一样。

到此为止,没有任何问题。照相机只要模拟人的视锥细胞感应,就可以真实的反映视网膜上的电信号。可是要命的是视觉系统有一个主观的成分在里面,比如一个红苹果,在阳光下是红的,到了篝火旁边,人们当然认为它也是红的,但是拿科学仪器一测就知道,这两种情况下苹果反射的频谱是不一样的,原因是环境光频谱不一样。如果照相机在两种情况下拍照都不加以调整,拍出来的照片就不真实了,这个不真实不是因为照相机没照对频谱,而是因为人在不同环境光下会主观的对视锥细胞的感应作修整,这个现象叫做 Color constancy。很有趣的是,尽管这是一个主观的过程,但不同的人做的修正几乎都是一样的,因此,照相机也可以对 RGB 三个通道的强度来做修正以模拟人脑,一般这个过程叫做调节白平衡。

另外一些有趣的问题:

鸟类有四种视锥细胞,多出来的一种对紫外光比较敏感,科学家们发现雄性鸟类的羽毛在紫外段的反射很强也很多变,于是推测这种特殊的视觉在求偶方面有特殊的作用。

人类视锥细胞的基因是在 X 染色体上的,由于男性只有一条 X 染色体,所以色盲的比例要比女性高很多,比如红绿色盲就是因为 L 和 M 两种视锥细胞少了一种,因为 S 视锥细胞在分辨红绿上几乎没有作用。

有研究声称某些人携带了四种视锥细胞的基因,也就是说有四色视觉系统(Tetrachromacy),有研究甚至号称有一半女性都有这种基因,只不过很多人不知道如何使用这种能力。这个研究貌似没有定论,不过以前有笑话说某人帮老婆买布,买回来老婆说不是这种红,是那种红,结果某人看了两种红觉得完全没区别,现在看到四色视觉系统的研究,觉得女人对颜色更敏感这件事貌似是有生理依据的。

人视网膜上除了视锥细胞还有一种视杆细胞也会感光,但只在光比较弱视锥细胞不能工作的时候才开始工作。视杆细胞只有一种,所以夜晚灯光不足的时候人不能分辨颜色,只能看到黑白的世界。

在特殊的场合,RGB 照片模拟物体颜色会丢失掉有用的信息,这种情况下比较好的办法就是把反射频谱直接测下来。医学上有时候对病变组织颜色的采样就需要这样做。

两个物体在太阳光下看起来颜色一样,到了日光灯下面就未必颜色一样,这是因为他们有不同的反射率函数。在太阳光下,它们的反射光刚好对三种视锥细胞产生比例相同的刺激;而当环境光频谱变化的时候,他们反射出来的频谱就变化了,人就会看到不同的颜色。在工业上,有时候需要两种不同的材料在各类光照条件下匹配色彩,这就需要尽量匹配它们的反射率函数,一般这样的颜色匹配叫做频谱色彩匹配。

色彩学涉及物理学、生理学、心理学、脑科学、计算机科学等,网上有很多介绍文章,我就不写了。可以在 wikipedia 查询下列关键字:color, color temperature, color vision, color theory, metamerism, tetrachromacy, white balance, gamma correction。

鸟类视觉系统:Papers on avian vision,这里也讲到进化演化过程,为什么鸟类比人类多一种视锥细胞。

一个讲解数字摄影基本原理的网站:Digital Photography Tutorial,文字清晰,说理到位,图片丰富。

Tags: , , , , , , , , ,

某人用苹果电脑…

November 12, 2007 1:08 am GMT-0700 | In Life | 7 Comments | hide

某人用 Mac 好几个月了,前几天需要重启动系统,死活找不到重启动的地方,电话我才得知需要按屏幕左上角的小苹果。这件事情说明了两个问题:

  • Mac 系统很好很强大,电脑盲使用几个月都从来没有需要重启动过。
  • Mac 的 usability 是不是还存在问题,特别是对于极笨的使用者……

Tags: ,

« 上一页下一页 »

This weblog is licensed under a Creative Commons License.
Powered by WordPress. Theme based on Pool by Borja Fernandez.