Wrapping up 2008

It’s that time of the year, where one reminisces, remembers, recalls, regrets, reflects, and other activities starting with re’s. I was going through the posts I wrote this year, and some of the posts made me react with “Huh, I wrote that?”, “What was I thinking?!” and other epic reactions. So here are some of the posts, and if I can remember them, the unsaid reasons for writing them.

The post on Fibonacci sequence and its relation to the golden ratio was quite popular. It still comes up in search results every now and then. The story of the thriving rabbit population came from a childhood encyclopedia (yes, I still have the entire set).

The barber paradox is one of my favourite concepts. I read about it when flipping through my university textbook on logic. Basically, if you hit a stumbling block in your logic, you might find that challenging your original assumption reveals the answer to the stumbling block.

In May 2008, I managed to dupe persuade Guy Kawasaki into including me in the Programming section of Alltop. That was awesome, since I was trying to improve awareness of my blog and the concept I’m pushing for (multi-disciplines). Getting listed helped.

I also created a new site called Ragnarok Code. It was meant to be a code playground for me. I do a lot of .NET work and I want to do some stuff that I can’t on this blog (though there are ways to get around my PHP problem…). Wanted to code something with Silverlight and I never really got to it… That said, check out the OpenStreetMap application by Philip. It’s totally awesome.

Then I tried my hand at mixing storytelling and puzzles with the Mind Trap series. I planned for 5 parts, but failed to find interesting puzzles to weave into the story. In the end, I only had 3.

Alas, the full story of our trapped hero Ryan may never be revealed…

I also started a mini-series dubbed “Please ConvertToEnglish()“. It’s a play on the .NET functions such as Convert.ToInt32() and so on. What I had in mind was to explain certain code or human behaviour (mainly because of code) in simple English. I don’t see an end to it, since I draw inspiration from what I encounter… *smile*

There’s something else that came up often in my search results: The puzzle on 7 points and 6 lines (and the 2nd solution with corrections from a reader, Eric).

Then came the first (known) hack attempt on my blog. It was targetting SQL Server, so I was safe… I think… That analysis involved the use of a few pieces of information.

I also had a visit from Raymond Chen for something I wrote about rotating a matrix, which is different from a rotation matrix. Read the proof by Mathew which is simpler than mine, and it even looks like what I would write on paper with all the subscripts and superscripts.

My next embarrassing public humiliation was when I asked for advice on field of view (FOV). I was called on my article by two people no less (thanks to David and Jason aka xero)… That article sprung up from a university assignment, and was one of those questions where I never really got an answer. Like the one on using pointer arithmetic instead of array syntax. Wait, I didn’t write about that one…

In October, I wrote what I think was my crowning achievement then, the article on the use of bilinear interpolation in image rotation. It required the combination of a few pieces of information to work:

  • Knowledge of raster, Cartesian and polar coordinate systems
  • Linear interpolation and as a corollary, bilinear interpolation
  • The “direction” of assignment

Strangely, the post on the digital clock puzzle came up moderately frequently in search results. I guess a lot of people were stumped… Great answer by Steven, though I couldn’t award him any prizes. I’ll have to get my hands on some noteworthy gifts…

I also started to get involved in the Singapore social media slash blogging scene. Got to know some really nice people, and there were also a few programmers too!

Then I tackled the hitherto unimaginable math problem on swinging doors. It was interesting. It was spontaneous. It was … alright, I was a bit bored. I also made a major mistake while discussing that, which I corrected with another post. Talk about barber paradoxes…

I finally came to terms with the lengthy name of my blog, and shortened what I am to being a Polymer. I still want to publish something more on it, maybe a manifesto. Still collecting my thoughts on this…

And I was a guest in a Tech56 podcast, a local production about technology and general geeky stuff. I was also at their 100th episode celebration/podcast event.

On the topic of podcasts, I submitted a question which was answered in Stack Overflow podcast episode 33. I asked for an opinion on reducing costs (in a software business, since Jeff and Joel probably won’t know much about the industry of the company I’m working at). In summary, the answer was to stop worrying about reducing costs and start thinking about increasing value.

Recently, I talked about digital image processing. It was something I wanted to write about for a long time, but never really got down to it. I also didn’t do justice to the topic, and I originally planned for something more fleshed out. I’ll leave it as it is till I can’t stand it anymore and I’ll follow up on it. And that image processing course I took was one of the most interesting classes in my entire university life.

And that’s 2008 for me. Have fun with the rest of December, and I’ll see you next year.

Christmas equals Halloween

You might already know how Christmas is equal to Halloween. You don’t? Well, I came across this some time ago, and it took me a few moments to figure out how Christmas can be equal to Halloween. Now, let me prove to you the truthfulness of that statement.

We have Christmas on 25 December. Let’s put that here:
DEC 25
= 25 [let's focus on the day, ok?]
= 24 + 1 [it's obvious I know...]
= (8^1) * 3 + (8^0) * 1 [some fancy arithmetic]
= 31 [of base 8. Much easier to work with than base 3]
= OCT 31 [let's use the short form of octal]

Wait a minute, OCT 31 looks awfully familiar… Hey it’s Halloween, which falls on 31 October! Thus is Christmas equal to Halloween.

Q.E.D.

Alright, in case you’re not following, the “proof” transformed Christmas and Halloween into their date representations. The date representations happen to be of the form “base-short-form number”. So Christmas became decimal 25, and Halloween became octal 31.

Here’s a lesson to take away. Sometimes, the problem you’re working on is easier to solve when it’s transformed into another representation. For example, rotating an image is easier when you transform all the coordinates from raster to Cartesian to polar, and then rotate in polar coordinates. Or change a colour in RGB format into HSL, so you can change colour just by varying the “hue” part.

That’s it for now. Happy holidays!

Matching textures with heuristics

I was in my 4th year in university. The course was on digital image processing, touching on both theory and application in equal measure. There were only 3 students, including me.

The course was interesting, albeit mind-numbing when some of the equations march into the lecture. The programming assignments were more fun, since we got to apply the theories. One of them was a rotating-an-image assignment, which formed the basis of my bilinear interpolation code. That was fun.

There’s this assignment where the professor gave us a set of texture images as samples. I can’t remember how many there were, so let’s say there were 200 of them. Then he gave us, say, 50 images. The assignment was to match those 50 images with the controlled set of textures. All textures were greyscale to simplify the assignment.

The 50 unknowns didn’t match pixel for pixel with the controlled samples. But they were of the same texture. For example, the controlled samples had one of marbled floor. One of the unknown images was taken with that marble floor, but in a different position. Of course, the professor could have given us red herrings to match, but he said all 50 were taken from the sample set.

Then there’s the fact that he wanted to play with his new camera back then (he admitted to it), and took lots of pictures to give us as assignments… There was an assignment with a picture of a rubber ducky…

I can’t remember exactly all the tests I used to match the textures. What I did was come up with a theory/test, and compute that test for all the samples. Then I did the same thing for the unknown textures. Then I match the unknowns with the knowns. If they were within some threshold of acceptance, that unknown texture was deemed matched to the respective sample texture.

Basically, I’m matching the textures using heuristics.

One of the tests used histograms. Basically I charted from 0 to 255 the number of pixels with a specific greyscale value. Pure white pixels have a value of 255, and pure blacks have 0 value. Then I matched the unknowns with the samples using mean squared error. If the sample matched with the least error was less than some threshold I set, then that sample was the matched texture.

I had another test involving Fast Fourier Transforms (FFT). I think I discarded the complex values and matched the unknowns using the real values part.

There’s another test involving median filtering. The idea was to capture the groups of neighbouring pixels as some usable data. So instead of a 128 by 128 pixel sample, I reduced it to a 16 by 16 matrix. You know, this one’s a bit iffy… I can’t remember whether I actually did it, or I just came up with it writing this…

Anyway, there’s a test to capture “pattern” data. The histogram test involves all pixels. The median filter test (if I actually did one) cluster pixel information in groups. Let me see if I can explain this better…

Marble texture

In the image above, the top right corner has more black swirly thingies close together than other parts of the image. The histogram test cannot detect that the top right corner has more black. It can only detect how much black in total there is in the image. Positional information is lost. Hence the need for a pattern test.

The histogram test is objective. Test results are verifiable and repeatable. However, matching the unknown textures require that I set a threshold. This is where the tests become subjective. Who’s to say a particular threshold value is more accurate than another?

In the end, I think I had 5 or 6 tests, and gotten a 94 (or was it 96?) percent accuracy. I was tweaking my threshold values so I could yield higher accuracy rates. See how subjective those tests of mine were? *smile*

The programming language of choice was MATLAB (yes, Will?), as dictated by the professor. So everything was coded in MATLAB. Which was good, because I’d hate to implement FFT on my own…

There’s something else too. I weighted those test results. Say test A was supposedly more accurate than test B. Then I gave the results of test A more weight in my final calculation. Thus, roughly speaking, if 3 tests out of 6 say texture A was the one, then that’s the one. It could also mean 2 tests had more sway if both carried high weights, and the other 4 tests weren’t conclusive enough.

One of my classmates got higher accuracy rates (97 or 98 percent) than I did, no matter how much I tweaked threshold values and weights, no matter how many kinds of tests I added (or took out).

But here’s the thing, and I want you to note this. Given a larger sample size, and a different set of unknown textures to match, my set of tests might actually yield better results than those of that irritatingly smug classmate of mine.

Here’s another takeaway. No one test can conclusively confirm and match the unknowns (even with some error margin). It took a few tests working in concert to obtain a relatively high accuracy rate. Think about that.

Digital image processing is mostly for aesthetics

I intended to write an explanation of some digital image processing methods, followed by the point of this article’s existence (see title). Since I can’t remember much about the topic already, I rummaged through my pile of stuff to find my university textbook. Found it, and skimmed over the content pages to see what I needed to research on.

I am ashamed to say, I have forgotten a lot about the topic already… This is hard! There’s graphs, functions, formulas, integration, summations and matrices.

The book is “Digital Image Processing” (second edition) by Rafael C. Gonzalez and Richard E. Woods, published by Prentice Hall. Unfortunately, it’s not for sale in USA, Mexico or Canada, so if you’re from those countries, sorry. But it’s really good.

[UPDATE: Commentor Will has pointed out that Gonzalez's and Woods' book is in fact available in the United States. Go get it.]

Anyway, I was planning on stunning you with my brilliance on histograms, median filtering, Gaussian blurs, pixel neighbours and the like. Sadly, I’m woefully ill-equipped to do that… At least, not without doing some serious reading and research first. Well, I can’t do that and still write about it in one night.

But I want to tell you about something my professor said. There are many things we do to process images. We apply median filtering to remove noise. We do some operation to sharpen images and blur them. Images can be modified to black and white, or set to a faded tone to simulate old photographs.

There are other kinds of information we can extract after processing images, such as detection of edges, shapes and even faces. But as far as I can tell, they are mostly subjective. Subjective because it is up to us, humans, who finally dictate whether the final image is what’s required.

For example, noise reduction. Simply put, image noise is the small specks of “something” in the image. They could be caused by an inferior camera, or simply because of the environment (such as in space images). Median filtering means for each pixel, check out the neighbouring pixels, and if they are mostly of one colour, then that’s the “correct” colour to be.

But there are different strengths when applying the median filter. The software cannot tell you how strong the filter should be. It can guess, it can have default values, it can apply the strength most commonly used. But it’s ultimately up to the user to decide when enough is enough. Perhaps at the highest strength, the algorithm produces an image that’s somehow pleasing (or useful) to the user, even if that result isn’t what the user wanted originally.

And this was what my professor said. Image processing is mostly about aesthetics, digital or analog. Digital computations just make it faster and easier to play around with the images. You can physically process images to produce sepia tones. Or you could do it digitally.

I’m introducing the concept of image processing because I have another story to tell you. And it’ll make more sense if you know about image processing first.

Have you written code to do image processing before? Please share your experience with your comments.

Thoughts on Stack Overflow podcast 33

I submitted a question to the Stack Overflow podcast, hosted by Jeff Atwood and Joel Spolsky, and it’s answered! Hear about it at podcast 33. I’m at around the 49m50s mark. I sound only marginally better than the Tech65 podcast I was in… So some thoughts about the discussion in the podcast…

24 by 7 on call

For a period of time in my programming career, I was to be available, 24 by 7, to attend to some rare disaster in the unlikely event that critical programs failed spectacularly. Usually it’s a program malfunction or data problem. When I first received my pager (yes, it’s a long time ago…), I was apprehensive. I placed it on a stool and moved the stool beside my bed, just in case I couldn’t hear the beeps when I was asleep.

After a while though, it turned out that the crises were critical, but not that critical. I could still attend to them the next day. The staff on shift duty would page me, and all they really needed was for me to tell them that the error was duly noted, and that I’ll attend to it the next day.

And being the dedicated fella that I am (was?), during one such fine day, when it was a public holiday, when my home country was celebrating her birthday, I returned to the office to attend to one such error after receiving such a page. *bows floridly*

Killer triggers

I agree with Joel Spolsky on this. Triggers should never have been invented. I’m maintaining an application that relied heavily on triggers to do business logic. I’d be firing some innocuous insert statement, and many other tables get updated.

This gets very irritating when you’re testing out code, and you need some data in that first table. It gets worse when you factor in foreign keys and so on… It’s a nightmare…

The only reasonable use for triggers is for logging an audit trail. Even then, I believe SQL Server has in-built capabilities to do that, so you don’t need to create an actual table to store the audit logs. Too bad I’m using Sybase…

That favourite word

It’s just an observation. Alright, I get a little irritated about it. I find it pathological that Jeff Atwood has a pathological fondness for the word “pathological”. There. Although, in his defense, he didn’t use the word in the last couple of podcasts. Some of his blog posts on the other hand…

There was a period of time, when the programming sites I visited had a fad of using the word “egregious”. It means bad, like what-in-tarnation-were-you-thinking bad. Usually used in the context of WTF-like code. Perhaps the programmers felt a need to exert their linguistic capabilities…

If you watch travel documentaries, you might have come across Samantha Brown. I love her Passport to Europe series. She too, has her favourite word. It’s “quintessential”. As in “This is the quintessential town”. I started watching out for when she uses the word.

I’m starting to do that for Jeff and “pathological” too. You know, that’s pathological of me… let me go find something better to do. At least I listen to the podcasts in a safe environment. Jeff nearly killed someone who was driving.