Title: Tutorial 2: Basic Image/Matrix Operations

Basic Image/Matrix Operations

The top corner of the Lena image (the first 256 rows and columns) can be loaded by combining a function call to imread and matrix indexing:

img = imread("lena_big.tif")[0..255,0..255,0..2]

This image can then be normalized such that all intensity values lie within [0,1]:

mini = min(img)
maxi = max(img)
image = (img - mini)/(maxi - mini)

Next we convert this color image to a luminance (grayscale) image (0.21 Red + 0.72 Green + 0.07 Blue):

grayimage = image[:,:,0]*0.21 + image[:,:,1]*0.72 + image[:,:,2]*0.07

To improve the contrast of the image we take for each pixel the square of the intensity.

grayimage = grayimage.^2
imshow(grayimage)

The complete code is therefore:

img = imread("lena_big.tif")[0..255,0..255,0..2]
mini = min(img)
maxi = max(img)
image = (img - mini)/(maxi - mini)
grayimage = grayimage.^2
imshow(grayimage)

The resulting image is shown below:

tutorial2_result.jpg

Interestingly, when compiling the complete code, the Quasar compiler displays the following information:

tutorial2.q - Line 6: A kernel function has been automatically generated for '((image[:,:,$t1]*$t2)+(image[:,:,$t3]*$t4)+(image[:,:,$t5]*$t6))'

The compiler has generalized the expression to calculate the luminance image and generated a kernel function to perform this operation in one pass in parallel, using the available CPU/GPU. Whenever other expressions are encountered in the input program, but for example using other parameters $t1…,$t6, the same kernel function will be used.