0% found this document useful (0 votes)
13 views

Image Mat Util Code

This code defines two functions that take an image file path or image data and return matrices representing the image points and colors. The image2mat function iterates over pixels, storing x, y, and color values in dictionaries which are then used to create matrices for the points and colors representing the image.

Uploaded by

munezfranq
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Image Mat Util Code

This code defines two functions that take an image file path or image data and return matrices representing the image points and colors. The image2mat function iterates over pixels, storing x, y, and color values in dictionaries which are then used to create matrices for the points and colors representing the image.

Uploaded by

munezfranq
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Here is the block of code related to loading a color image using the

'image_mat_util.py' file:

```python
def file2mat(path, row_labels = ('x', 'y', 'u')):
"""input: a filepath to an image in .png format
output: the pair (points, matrix) of matrices representing the image."""
return image2mat(image.file2image(path), row_labels)

def image2mat(image, row_labels = ('x', 'y', 'u')):


""" input: an image in list-of-lists format
output: a pair (points, colors) of matrices representing the image.
Note: The input list-of-lists can consist of either integers n [0, 255] for
grayscale
or 3-tuple of integers representing the rgb color coordinates
"""
h = len(image)
w = len(image[0])
rx, ry, ru = row_labels
ptsD = (set(row_labels), {(x,y) for x in range(w+1) for y in range(h+1)})
ptsF = {}
colorsD = ({'r', 'g', 'b'}, {(x,y) for x in range(w) for y in range(h)})
colorsF = {}
for y in range(h+1):
for x in range(w+1):
pt = (x,y)
ptsF[(rx, pt)] = x
ptsF[(ry, pt)] = y
ptsF[(ru, pt)] = 1
if x < w and y < h:
col = image[y][x]
if type(col) is int:
red, green, blue = col, col, col
else:
red, green, blue = col
colorsF[('r', pt)] = red
colorsF[('g', pt)] = green
colorsF[('b', pt)] = blue
return mat.Mat(ptsD, ptsF), mat.Mat(colorsD, colorsF)
```

This code defines two functions, `file2mat` and `image2mat`, that take an image
file path or an image in list-of-lists format, respectively, and return a pair of
matrices representing the image: one for the points and one for the colors.

The `image2mat` function first determines the height and width of the input image,
then creates two dictionaries, `ptsD` and `colorsD`, to store the metadata about
the points and colors in the image. It then iterates over the pixels in the image,
storing the x, y, and u (always 1) coordinates in the `ptsF` dictionary, and the
red, green, and blue values in the `colorsF` dictionary.

Finally, the function returns a pair of `mat.Mat` objects, one for the points and
one for the colors, which can be used to display the image.

You might also like