element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Members
    Members
    • Benefits of Membership
    • Achievement Levels
    • Members Area
    • Personal Blogs
    • Feedback and Support
    • What's New on element14
  • Learn
    Learn
    • Learning Center
    • eBooks
    • STEM Academy
    • Webinars, Training and Events
    • More
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • More
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • More
  • Products
    Products
    • Arduino
    • Dev Tools
    • Manufacturers
    • Raspberry Pi
    • RoadTests & Reviews
    • Avnet Boards Community
    • More
  • Store
    Store
    • Visit Your Store
    • Choose Another Store
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
FPGA
  • Technologies
  • More
FPGA
Forum Correct way to use large arrays on MicroBlaze based microcontroller (Arty S7)
  • Blog
  • Forum
  • Documents
  • Events
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
FPGA requires membership for participation - click to join
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 7 replies
  • Subscribers 340 subscribers
  • Views 191 views
  • Users 0 members are here
  • fpga
  • spartan-7
  • Spartan_Migration
  • spartan-6
Related

Correct way to use large arrays on MicroBlaze based microcontroller (Arty S7)

cbohra00627
cbohra00627 13 days ago

I have been trying to implement an image processing algorithm (blurring an image) on my MicroBlaze based design. My block design is shown below:

Block Design

And here is my code:

#include <stdlib.h>
#include <sleep.h>
#include <xil_types.h>
#include <xparameters.h>
#include <xgpio.h>
#include <xuartlite.h>
#include "platform.h"
#include "image.h"

#define LED_CHANNEL 1

//Gpio and Uart Instances
XGpio Gpio;
XUartLite uart;

int main() {
	init_platform();

	//Width, Height and total number of pixels
	u16 WIDTH = 400;
	u16 HEIGHT = 400;
	u32 PIXELS = WIDTH*HEIGHT;

	//Initialize the Gpio and set data direction for the led channel as output
	XGpio_Initialize(&Gpio, XPAR_GPIO_0_DEVICE_ID);
	XGpio_SetDataDirection(&Gpio, LED_CHANNEL, 0b0000);

	//First LED is turned on to depict start of program
	XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0b1000);

	//Allocate memory for Blurred Image pixels
	u8 **BLUR_IMAGE = (u8**) malloc(160000*sizeof(u8*));
	for(u32 i=0; i<PIXELS; i++) {
		BLUR_IMAGE[i] = (u8*) malloc(3*sizeof(u8));
	}

	//Algorithm to get the blurred image
	for(u16 i=0; i<HEIGHT; i++) {
		for(u16 j=0; j<WIDTH; j++) {

			//Coordinates of the (5x5) pixel matrix
			u16 start_corner_row = (0>i-2) ? 0 : i-2;
			u16 start_corner_col = (0>j-2) ? 0 : j-2;
			u16 end_corner_row = (HEIGHT-1<i+2) ? HEIGHT-1 : i+2;
			u16 end_corner_col = (WIDTH-1<j+2) ? WIDTH-1 : j+2;

			//Represents the three R,G,B values of a pixel
			u16 blur_R = 0;
			u16 blur_G = 0;
			u16 blur_B = 0;

			//Adding all the values of R,G,B layers of the pixels in the pixel matrix
			for(u16 m=start_corner_row; m<=end_corner_row; m++) {
				for(u16 n=start_corner_col; n<=end_corner_col; n++) {
					u32 pixel_ind = m*WIDTH + n;
					blur_R += IMAGE[pixel_ind][0];
					blur_G += IMAGE[pixel_ind][1];
					blur_B += IMAGE[pixel_ind][2];
				}
			}

			//Calculating average
			blur_R /= 25;
			blur_G /= 25;
			blur_B /= 25;

			//Assigning to the BLUR_IMAGE variable
			BLUR_IMAGE[i*WIDTH + j][0] = blur_R;
			BLUR_IMAGE[i*WIDTH + j][1] = blur_G;
			BLUR_IMAGE[i*WIDTH + j][2] = blur_B;

		}
	}

	//Second LED is turned on to depict end of image processing
	XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0b1100);

	//Represents 50% of total pixel count
	u32 percent_50 = PIXELS/2;

	//Initialize the UART
	XUartLite_Initialize(&uart, XPAR_UARTLITE_0_DEVICE_ID);

	//Sending the blurred pixels 3 bytes at a time
	for(u32 i=0; i<PIXELS; i++) {
		XUartLite_Send(&uart, BLUR_IMAGE[i], 3);

		//Third LED is turned on to depict that 50% of pixels have been transmitted
		if(i>percent_50)
			XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0b1110);

		//Delay of 50 us to let the receiver capture the data correctly
		usleep(50);
	}

	//Fourth LED is turned on to depict that all the pixels have been sent
	XGpio_DiscreteWrite(&Gpio, LED_CHANNEL, 0b1111);

	cleanup_platform();
	return 0;
}

I have stored the pixel data of my image in an array and included it in my main source file:

Pixel Data

Then I am trying to implement a sliding window approach to blur the image. The blurred image is to be stored in another array "BLUR_IMAGE". Then I am transferring this "BLUR_IMAGE" to my PC over uartlite. The problem is that the data that I am receiving on my PC has same elements for each pixel as shown below:

Blur Image

At first, I though there is some problem while sending the data over uartlite. So, I tried to send the "IMAGE" array over uartlite and I received it correctly. Then, I though there might be some problem with my algorithm. So, I ran the same algorithm on my PC and it could successfully blur the image and write it to a file.

Then, I commented out the algorithm part and the uartlite part. And tried to assign "BLUR_IMAGE" some constant values {1,2,3} to each element and then print it on vitis serial terminal using xil_printf(). But on the terminal it didn't print {1,2,3}. Instead, it printed {28,222,192} as I was receiving over uartlite. So, I think there is some problem with the way I am declaring my array or the way I am assigning values to it.

Can anybody suggest me some solutions?

  • Reply
  • Cancel
  • Cancel

Top Replies

  • cbohra00627
    cbohra00627 13 days ago in reply to Andrew J +1

    Its working now, after declaring it as an static variable!

  • Andrew J
    Andrew J 13 days ago

    I’m. It a natural C programmer so there’s stuff here that isn’t immediately clear, such as why you can’t just declare your blur array like you do your image array, e.e BLUR_IMAGE[PIXELS][3].  Notwithstanding, is writing u16 values to an array allocated by using the size of u8 causing an issue?

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • Cancel
  • cbohra00627
    cbohra00627 13 days ago in reply to Andrew J

    I thought preacllocating memory is preferred sometimes, right?? Anyways I will try it with BLUR_IMAGE[PIXELS][3]. And i checked assigning u16 to a u8 on c compilor it was working fine... actually only the last 8 bits from that u16 variable is required for me.

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • Cancel
  • cbohra00627
    cbohra00627 13 days ago in reply to Andrew J

    I used malloc on my PC also to implement the same code, it was working fine then!

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • Cancel
  • cbohra00627
    cbohra00627 13 days ago in reply to Andrew J

    I tried with BLUR_IMAGE[PIXELS][3] but it seems to be stuck for over 20 mins!

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • Cancel
  • cbohra00627
    cbohra00627 13 days ago in reply to Andrew J

    Its working now, after declaring it as an static variable!

    • Cancel
    • Vote Up +1 Vote Down
    • Reply
    • Cancel
  • Andrew J
    Andrew J 13 days ago in reply to cbohra00627

    Well done.  I meant to say “I’m not a natural C programmer…”. I tend to keep things as simple and understandable (to me) as possible so when I see stuff like that it just looks unnecessarily complex.  Obviously not necessarily wrong and for someone more experienced with C it may well be the best way.  Good job for finding the problem.

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • Cancel
  • cbohra00627
    cbohra00627 13 days ago in reply to Andrew J

    Yeah true... I was going through the complex way indeed! :)

    A simple search on google solved my problem. I should have done this earlier! Sweat smile

    • Cancel
    • Vote Up 0 Vote Down
    • Reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2022 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • Facebook
  • Twitter
  • linkedin
  • YouTube