Skip to content

Instantly share code, notes, and snippets.

@Raniita
Created February 2, 2020 07:49
Show Gist options
  • Save Raniita/5e9a6e8a86cd2de06dfda82bdccf5684 to your computer and use it in GitHub Desktop.
Save Raniita/5e9a6e8a86cd2de06dfda82bdccf5684 to your computer and use it in GitHub Desktop.
RGB to YUV exam example
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Solucion
{
public partial class Form1 : Form
{
int[,] movementMatrix = new int[80, 100];
double[,] rMatrix = new double[80, 100];
double[,] gMatrix = new double[80, 100];
double[,] bMatrix = new double[80, 100];
public Form1()
{
InitializeComponent();
////////////////
// Apartado 1 //
////////////////
// Imagen de 800x640 / bloques de 8x8 / 100*80
// int por facilidad, se deberia cambiar a byte[] o boolean dependiendo del futuro uso
// Rellenar aleatoriamente
Random rand = new Random();
for (int i = 0; i < 79; i++)
{
for (int j = 0; j < 99; j++)
{
//Console.WriteLine("Filling out " + i + " " + j);
// We are on an even column
if(i%2 == 0)
{
// And even row
if (j % 2 == 0)
{
int randomPick = rand.Next(0, 2);
movementMatrix[i, j] = randomPick;
movementMatrix[i+1, j] = randomPick;
movementMatrix[i, j+1] = randomPick;
movementMatrix[i+1, j+1] = randomPick;
}
}
}
}
// Print movementMatrix to movementBox
string matrixString = "";
for (int i = 0; i < movementMatrix.GetLength(0); i++)
{
for (int j = 0; j < movementMatrix.GetLength(1); j++)
{
matrixString += movementMatrix[i, j].ToString();
matrixString += " ";
}
matrixString += Environment.NewLine;
}
this.movementBox.Text = matrixString;
////////////////
// Apartado 2 //
////////////////
// Una gran perdida para la reusabilidad del codigo
// Red Matrix
for (int i = 0; i < rMatrix.GetLength(0); i++)
{
for (int j = 0; j < rMatrix.GetLength(1); j++)
{
rMatrix[i,j] = rand.NextDouble() * (1.0 - 0.0);
}
}
string rMatrixString = "";
for (int i = 0; i < rMatrix.GetLength(0); i++)
{
for (int j = 0; j < rMatrix.GetLength(1); j++)
{
rMatrixString += rMatrix[i, j].ToString("0.000");
rMatrixString += " ";
}
rMatrixString += Environment.NewLine;
}
this.redBox.Text = rMatrixString;
// Green Matrix
for (int i = 0; i < gMatrix.GetLength(0); i++)
{
for (int j = 0; j < gMatrix.GetLength(1); j++)
{
gMatrix[i, j] = rand.NextDouble() * (1.0 - 0.0);
}
}
string gMatrixString = "";
for (int i = 0; i < gMatrix.GetLength(0); i++)
{
for (int j = 0; j < gMatrix.GetLength(1); j++)
{
gMatrixString += gMatrix[i, j].ToString("0.000");
gMatrixString += " ";
}
gMatrixString += Environment.NewLine;
}
this.greenBox.Text = gMatrixString;
// Blue Matrix
for (int i = 0; i < bMatrix.GetLength(0); i++)
{
for (int j = 0; j < bMatrix.GetLength(1); j++)
{
bMatrix[i, j] = rand.NextDouble() * (1.0 - 0.0);
}
}
string bMatrixString = "";
for (int i = 0; i < bMatrix.GetLength(0); i++)
{
for (int j = 0; j < bMatrix.GetLength(1); j++)
{
bMatrixString += bMatrix[i, j].ToString("0.000");
bMatrixString += " ";
}
bMatrixString += Environment.NewLine;
}
this.blueBox.Text = bMatrixString;
////////////////
// Apartado 3 //
////////////////
apartado3();
}
public void apartado3()
{
// Generacion formato yuv para el primer macroblock diferente de 0
double[,] coeficientes = new double[3, 3] { { 0.299, 0.587, 0.114 }, { -0.147, 0.289, 0.436 }, { 0.615, -0.515, -0.100 } };
// We will print a 3x1 matrix with the yuv encoded MBs
double[] yuvMatrix = new double[3];
// Encontrar primer MB != 0
for (int i = 0; i < 79; i++)
{
for (int j = 0; j < 99; j++)
{
// We are on an even column
if (i % 2 == 0)
{
// And even row
if (j % 2 == 0)
{
// And that block is not 0
if (movementMatrix[i, j] == 1)
{
yuvMatrix[2] = coeficientes[0, 0] * rMatrix[i, j] + coeficientes[0, 1] * gMatrix[i, j] + coeficientes[0, 2] * bMatrix[i, j];
yuvMatrix[1] = coeficientes[1, 0] * rMatrix[i, j] + coeficientes[1, 1] * gMatrix[i, j] + coeficientes[1, 2] * bMatrix[i, j];
yuvMatrix[0] = coeficientes[2, 0] * rMatrix[i, j] + coeficientes[2, 1] * gMatrix[i, j] + coeficientes[2, 2] * bMatrix[i, j];
Console.WriteLine(coeficientes[0, 0]);
Console.WriteLine(rMatrix[i, j]);
Console.WriteLine(coeficientes[0, 1]);
Console.WriteLine(gMatrix[i, j]);
Console.WriteLine(coeficientes[0, 2]);
Console.WriteLine(bMatrix[i, j]);
string yuvMatrixString = "";
for (int t = 0; t < yuvMatrix.GetLength(0); t++)
{
yuvMatrixString += yuvMatrix[t].ToString("0.000");
yuvMatrixString += " ";
yuvMatrixString += Environment.NewLine;
}
this.yuvBox.Text = yuvMatrixString;
// Exits infamous loops
return;
}
}
}
}
}
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment