Interesting Code Snippets
Every so often, I come up with an interesting snippet of code I want to hang on to or that I think might be helpful to somebody else.
Probability Distribution Visualization
I put this together to help illustrate some concepts behind probability for my sister. Copy and paste it into Mathematica and you’ll get a handy interactive pair of probability distributions.
Manipulate[
Histogram[
{
RandomReal[NormalDistribution[\[Mu]1, \[Sigma]1], n1],
RandomReal[NormalDistribution[\[Mu]2, \[Sigma]2], n2]
}, {-10, 10, .5}
],
{\[Mu]1, -5, 5, Appearance -> "Labeled"},
{\[Sigma]1, 1, 5, Appearance -> "Labeled"},
{n1, 25, 1000, Appearance -> "Labeled"},
{\[Mu]2, -5, 5, Appearance -> "Labeled"},
{\[Sigma]2, 1, 5, Appearance -> "Labeled"},
{n2, 25, 1000, Appearance -> "Labeled"}
]
It should look like this:

Using the “meta” key in Emacs over SSH using Terminal.app
I’d always had trouble figuring out how to use the “meta” key in Emacs while in an SSH session- I’ve got my local machine set up to use the “option” key as “meta”, but that never seemed to work while in Emacs via SSH. Turns out that it’s “escape”. So, for example, to launch a shell session from within Emacs, enter esc, then x and then type shell. This falls squarely under the category of “obvious stuff that everybody else probably already knew”, but just in case, I’ve recorded it here.
Inspecting pl/pgSQL functions from PSQL
Creating pl/pgSQL functions is easy: CREATE FUNCTION.... Inspecting those functions six months later, after you’ve forgotten what’s in them? Turns out that that’s easy too. To inspect the SQL behind a function named, say, foo, use \df+, like so:
dbname=# \df+ foo
This will print out, among other things, foo’s source code.
Getting an OpenCV IplImage from an NSImage
There are plenty of places online to find out how to get an NSImage from an OpenCV IplImage, but, for whatever reason, I had a devil of a time figuring out how to go in the other direction. This code seems to work, and as far as I can tell doesn’t leak memory—- but I could be completely wrong. If any Objective-C or OpenCV wizards happen across this code, please let me know if it’s got any glaring problems.
- (IplImage*) nsImageToIplImage:(NSImage*)img {
NSBitmapImageRep *orig = [[img representations] objectAtIndex: 0];
// [NSImage -representations] operates in-place, so we have to make
// a copy or else the color-channel shift that we do later on will affect the original NSImage!
NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[orig representationUsingType:NSTIFFFileType properties:NULL]];
int depth = [rep bitsPerSample];
int channels = [rep samplesPerPixel];
int height = [rep size].height;
int width = [rep size].width;
// note- channels had better be "3", or else the loop down below will act pretty funky...
// NSTIFFFileType seems to always give three-channel images, so I think it's okay...
IplImage* to_return = cvCreateImage(cvSize(width, height), depth, channels);
// found this cvSetData trick here: http://www.osxentwicklerforum.de/thread.php?postid=89767
cvSetData(to_return, [rep bitmapData], [rep bytesPerRow]);
// Reorder BGR to RGB
// no, I don't know why it's in BGR after cvSetData
for (int i = 0; i < to_return->imageSize; i += 3) {
uchar tempR, tempG, tempB;
tempR = to_return->imageData[i+2];
tempG = to_return->imageData[i+1];
tempB = to_return->imageData[i];
to_return->imageData[i] = tempR;
to_return->imageData[i+1] =tempG;
to_return->imageData[i+2] = tempB;
}
return to_return;
}



