Yeshvanth Muniraj
Published

RGB to Grayscale with HW on Zynq

Two methods to convert RGB to Grayscale & Accuracy of the methods

BeginnerProtip3 hours122
RGB to Grayscale with HW on Zynq

Things used in this project

Hardware components

Zybo Z7: Zynq-7000 ARM/FPGA SoC Development Board
Digilent Zybo Z7: Zynq-7000 ARM/FPGA SoC Development Board
×1

Software apps and online services

Vivado Design Suite
AMD Vivado Design Suite

Story

Read more

Code

PS Code

C/C++
Xilinx SDK
/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc.  All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/

/*
 * helloworld.c: simple test application
 *
 * This application configures UART 16550 to baud rate 9600.
 * PS7 UART (Zynq) is not initialized by this application, since
 * bootrom/bsp configures it to baud rate 115200
 *
 * ------------------------------------------------
 * | UART TYPE   BAUD RATE                        |
 * ------------------------------------------------
 *   uartns550   9600
 *   uartlite    Configurable only in HW design
 *   ps7_uart    115200 (configured by bootrom/bsp)
 */

#include <stdio.h>
#include "platform.h"
#include "xil_printf.h"

#define BITP_RED					0
#define BITP_GREEN					8
#define BITP_BLUE					16

#define BYTE_MASK					0xFF
#define BITM_RED					BYTE_MASK
#define BITM_GREEN					BYTE_MASK
#define BITM_BLUE					BYTE_MASK

#define BITP_GRAY_AVG				0
#define BITP_GRAY_LUM				8

#define BITM_GRAY_LUM				BYTE_MASK
#define BITM_GRAY_AVG				BYTE_MASK

typedef struct
{
	uint32_t RGB_IR;
	uint32_t GRAY_OR;
} RGB_to_Gray_t;

RGB_to_Gray_t* pRGB_to_Gray = (RGB_to_Gray_t* )XPAR_RGB_TO_GRAYSCALE_NEW_0_S00_AXI_BASEADDR;

uint8_t RGB[16][3] =
{
	{198, 186, 158}, {123, 197, 239}, {126, 3,   99 }, {51,  85,  212},
    {73,  34,  128}, {227, 53,  8  }, {91,  136, 195}, {127, 148, 165},
    {249, 19,  81 }, {43,  209, 48 }, {164, 250, 183}, {83,  3,   227},
    {22,  59,  222}, {21,  213, 217}, {176, 40,  255}, {84,  102, 171}
};

uint8_t GrayScale[16][2];

int main()
{
    init_platform();

    uint32_t red, green, blue;
    //uint32_t gray_avg, gray_lum;

    for (uint8_t i = 0; i < 16; i++)
    {
    	red   = RGB[i][0];
    	green = RGB[i][1];
    	blue  = RGB[i][2];

		pRGB_to_Gray->RGB_IR = (((blue &  BITM_BLUE) << BITP_BLUE) | ((green & BITM_GREEN) << BITP_GREEN) | (red & BITM_RED));

		GrayScale[i][0] = ((pRGB_to_Gray->GRAY_OR >> BITP_GRAY_AVG) & BITM_GRAY_AVG);
		GrayScale[i][1] = ((pRGB_to_Gray->GRAY_OR >> BITP_GRAY_LUM) & BITM_GRAY_LUM);
    }

    printf("Grayscale Average:");
    for (uint8_t i = 0; i < 16; i++)
    {
    	printf("%u, ", GrayScale[i][0]);
    }

    printf("\nGrayscale Luminosity:");
	for (uint8_t i = 0; i < 16; i++)
	{
		printf("%u, ", GrayScale[i][1]);
	}

    cleanup_platform();
    return 0;
}

Credits

Yeshvanth Muniraj
21 projects • 38 followers
Hands-on experience in Embedded Systems and IoT. Good knowledge of FPGAs and Microcontrollers.
Contact

Comments

Please log in or sign up to comment.