Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bashmocha/riscv-center-of-mass-locator

RISC-V project that locates the center of mass of an image formatted in RGB.
https://github.com/bashmocha/riscv-center-of-mass-locator

assembly rars risc-v

Last synced: about 1 month ago
JSON representation

RISC-V project that locates the center of mass of an image formatted in RGB.

Awesome Lists containing this project

README

        

# RISC-V Center of Mass Locator

The aim is to develop a program in Assembly RISC-V to locate characters in an image.
Given a file with an image in RGB format, the program should generate a new image that identifies the character chosen by the user.






It is seen that each character has a different color tone in the image. Master Yoda has more shades
of green, Darth Maul is more red, and Boba Fett is closer to cyan. These characters can be distinguished by their color tones.

Each of them has a dominant color tone which is mentioned above. The best way to locate a character
is to find the center of mass of the character by the dominant pixels.

## Simulator
The project was developed in [rars](https://github.com/TheThirdOne/rars) simulator. To run the program be sure the simulator is installed correctly.

## RGB
In this project, the image is an RGB formatted file with 8 bits of color depth. Which means every
pixel contains three bytes corresponding to the RGB values. The image can be represented as a matrix
where each value is a pixel with three components. However, image pixels are stored sequentially in a
file in raw major1 order:





In order to convert the image into an RGB-formatted file, `ImageMagick` is used. With the use of the
proper commands images can be converted into different formats.

### Installing `ImageMagick` on Linux

```shell
sudo apt install imagemagick
```

Before turning into PNG or JPG from RGB, these parameters should be used:
```shell
-size 320x180 and -depth 8
```

Some examples of converting between JPEG, PNG and RGB formats are as follows:
```shell
convert imagem.jpg imagem.rgb
convert -size 320x180 -depth 8 imagem.rgb imagem.png
convert -size 320x180 -depth 8 imagem.rgb imagem.jpg
```

## HSV Color Space
The HSV color space encodes colors into Hue, Saturation, and Value components:


  • Hue represents hue on a color wheel.

  • Saturation represents the purity of the color. A pure color is said to be saturated. Mixing white,
    the color fades and is less saturated. Gray has zero saturation.

  • Value represents the lighting. The zero value represents darkness, that is, black. A high value
    means good lighting and colors are clearly visible.




## Image Segmentation
To find the characters, the first step is to identify the pixels for each. This process is called image
segmentation. Since each character has a different tone from the others, we can only distinguish them
with the Hue component. The tone ranges that can be used for segmentation are shown below.




## Center of Mass
In order to calculate the center of mass, the program will traverse the image pixel by pixel. It
will call a function called ’indicator’ that checks if the pixel belongs to the character or not. The
coordinates (cx, cy) correspond to the “center of mass”. If it is, the coordinates of the pixel will be
added to the center of mass values. And every time it hits a valid pixel, a counter will be increased.




The calculation is simply the average of the coordinates that belong to the character. The calculation is done in integer arithmetic (do not use floating point instructions).

## C Implementation
There is a [C](https://github.com/CheesyFrappe/riscv-center-of-mass-indicator/tree/master/src/c) implementation of the project in the repository. It is recommended to do the C implementation first before jumping
into Assembly. These two implementations contain all of the functions described below.

## Functions
There are seven functions in this project. Here are the explanations for each one:

- `main`:
It’s the main function of the program. First, prompts a question to select a character. If it’s chosen
then, call the rest of the functions respectively.

- `read_rgb_image`:
Reads a file with an image in RGB format into an array in memory called `buffer`. It’s a void function
and takes no arguments. First opens the file and checks if there is an exception occurring. Then, reads
the file into `buffer` within the length of `172800` which is the number of all pixels in the image. Then,
closes the file and returns.

- `write_rgb_image`:
Creates a new file with an image in RGB format. It’s a void function and takes no arguments. First
opens a file called `output.rgb` and checks if there is an error or not. Then, write the values of `buffer`
into the file. Then, closes the file and returns.

- `hue`:
Calculates the Hue component from the R, G, and B components of a pixel. Takes three arguments
and returns only one argument which is a0. These arguments are stored in a0, a1, and a2. There are
6 if conditions to find the range of hue value of these RGB values. If those RGB values don’t fit into
any range, the function returns 1, otherwise returns 0.

- `indicator`:
Indicates whether or not RGB values belong to the character. Takes four arguments and returns only
one argument which is a0. The first argument indicates which character will the function look for.
And the rest represents the RGB values respectively. First calls `hue` function to get the hue value of
these values. And it checks if the hue value fits into the character’s hue interval. if it does return 1,
otherwise returns 0.

- `location`:
Calculates “center of mass” for the character. Takes two arguments and returns two values. Argument
values are ’character’ and ’ `buffer` address’. It iterates the whole image and calls an `indicator` for each
pixel. If all RGB values are the same, the pixel is not included in the calculation. If the pixel is one
of the pixels of the character, x and y values are added up into registers. And the counter for the
calculation is increased. After the iteration cx and cy values are divided with the counter. The
function returns these values.

Here is the center of mass values for each character:



Character
Center of Mass




Master Yoda
67 - 55


Darth Maul
66 - 170


Boba Fett
65 - 265

- `draw_crosshair`:
Draws a crosshair at the center of mass. Takes three arguments. These are ’`buffer` address’, ’x co-
ordinate of the center of mass’ and, ’y coordinate of the center of mass’. Iterates the image with the
given `buffer` address and finds the pixels around the center of mass. Changes the RGB values for
each pixel in order to draw a pure red crosshair at the center of mass. It does not return anything
since it’s a void function.

## Output
Here is a sample output for Master Yoda: