Articles

Vivado HLS – FPGA Image Processing Histogram #09.1

July 12, 2019



hello guys so actually now we reach the left in our Republic s course that you can start to do some prideful stuff I choose to do this now some on image processing algorithms okay we are going to follow this book here read over it is for image analysis and we're going to start to start in the chapter 2 that is about calculating Eastern and performing contrast adjustment probably on this video we want you to to cut it in three phases we're first I'm going to teach how to calculate the histogram how to create an IP cord to the histogram then the how with the information of the Eastern to an algorithm called Easter demonstration that is going to increase the quality of the contrast and probably a third one where I'm going to join all these pieces into impure system in the sink we're going to do this automatically I hope you guys enjoyed I'm going to switch to the world and if you guys really like the future's give me a thumbs up and let's start so guys let's talk a little bit about instrument okay consider that we have an image okay which is grayscale which means it has values from 0 to 255 okay and the resolution of these images for this is 320 and 240 okay so what Instagram is all about the instrument imagine instrument as a quad okay where it has the amount of times that a particular pixel value repeat only image okay so if you have here this grayscale image okay you can have a pot like this and this basically shows how many times a certain value for instance how many times the zero value appear in image okay and what is what is important about these terms okay for Instagram you can extract some some nice information for instance what are the maximum pixel in the image here is simple to see if we Traverse in the backward direction okay in the histogram you can see that the first pitch so no zero will be something like probably 128 and this will be the maximum quality image and for the living as well going from 0 to this direction here would be like something problem 10 or some twenties and what is the relation of the histogram and the contours of the image okay basically if the histogram is like we're concentrating and not well sparks throughout the debate the possible values of your image you want to have a bad manners okay I'm going to switch on the lab in a club and I show this laner picture when you can see the revolution where the contrast is really low and we play a mural with instrument to to improve the cultures and then we're going to do an algorithm that does exactly that it's called the histogram stretch okay let's go to the lab and we're going to see how we do this HLS hello guys so here if you value HLS let's now create our Instagram core okay so let's just call it as a lab 9 HLS now we're going to create this source and in the test bench okay and basically the histogram we're going to have an input later by the way we are also going to change this input string to this string to the derivative Challis string type okay and the output of this is going to be it's going to be some signals that are going to control a biram that is actually going to store our instagram okay let's call it okay actually let's do different let's create a header file okay and here is the is the include that is going to have the definition of the strip datatype okay here is the we are going to use the Telos to keep the user NTID side channel from the stream so we actually we are here defining a data type that is going to be unsigned int eight okay so let's just change here as well on the on our C file because it change a little bit we're going to include our header file and well basically the the usage the calculation of the Instagram is quite simple we're going to have in control theorem this theorem is going to be addressed by the the pixel that comes in in our input stream okay and for every pre for every pixel value that is addressed in our case would be a grayscale image it's going to to increment a value inside the biram so that's how we form the histogram for instance imagine that the in this stream you have a series of values that are like four inches zero so imagine that it's coming 0 0 0 and then this is going to address this in these this block ram with the address 0 and incremented what we have there so it's going to actually is going to store the number of times that a certain pixel repeats in the image which by the case is the definition of the interval okay and why we use the histogram because if the Instagram we can we can for instance extract information like the maximum pixel in the image with the medium value of the the minimum and stuff like this which is which is used when we want to calculate when we want to do the contrast adjustment with the with the histogram stretchability okay so basically let's use this pragmas here to to define to revalue hls how to implement them so the input stream okay is going to be an access stream okay hls interface access the the histogram port is going to be at the type interface biram okay and well here we are we actually do we doing that the return value actually there is no return value for this function but when we define this we say that the port return of of our function this means that the core that is going to be generated from this function here without the core that is going to be generated from this function we will have some acts like control for starting and checking if the the algorithm done is calculation and stuff like this okay so basically this is asking to have the start and stop signals available through axle ID okay now one thing that we need to do is that every time that we call the histogram again we need to resect the content of our beer and if we don't do this all the time that we calculate the histogram again and again actually going to merge the results of the previous image so this part of your of the code is basically doing that every time that we call this function we're going to reset the the histogram to zero so it's going to be ready for the image whoo-hoo you arrived okay now let's just go to the to the algorithm selfies but okay I will just copy paste and explain to you guys actually this part here is not needed because we are not going to put nothing so basically how the algorithm works for is just just a for loop by the way our image will have this dimension here okay will be 320 by 240 okay and actually what we do we just read the the content of our stream okay for instance imagine that he arrived the valley then okay and then we are just addressed our biram okay don't forget that here we said that the histogram will be an interface for biram we are just addressing this value okay inside this array histogram and incremented by one okay this pragma here we just instruct Viva – Alice – to minimize the latency by doing a pipeline here this for loop so what is actually means it is going to for instance this case here we are setting the histogram to zero but in a pipeline fashion means that we actually cascading the these 256 operation to decrease the latins okay and will be quite simple we're just going to load we are not going to talk about open CV now but we value HLS also support open CV but we are going to be here use open Sevilla's to open the image and and calculate the Instagram okay so the test is quite simple as I mentioned we are using open CV here just to load an image okay here we just use am read and we pass an image path okay then we populate our stream with the data that we load from our image okay by the way this is quite simple stuff on open CV just to access a certain pixel Val inside the image later probably what tutorial about it okay and then we saved the Instagram to a file okay in MATLAB it's quite simple to show what exactly we need okay so let me just clean this I did wrong oh okay so let's just read read this image here okay and if you want to calculate the histogram of the image we just come in do this okay and basically we're going to verify if the Instagram that we calculated match more or less with this okay so now back to you we just asked to simulate the C version of this and I come back in some moments okay so it calculated okay let me just open here the the histogram file that we calculated now okay probably in this directory here actually the file is here in the solution system view okay and let's open up okay here we can see that in the beam 43 for instance we have nine elements so let's go to MATLAB another way to get the histogram you just click on the variable okay by the way 300 320 by 240 you want 8 and we can see here if we do a zoom at this point yes we have 9 okay so our beam 43 9 yeah more or less what maybe you guys are not able to see the file okay it's more or less what we have okay guys so let me close up this so in theory if the C version is working and you are able to synthesize in vivo HLS you can do code simulation to verify again if the if the if the output matches ok so let's do it he just said that the solution change it ok so let's try it again our synthesize again or HDL code for this and after this is done I run the code simulation so we synthesize now we could simulate let's ask modelsim and in this case I'm not interested to see the the waveform at least not now so let's ask to Qasim ladies and I bring back with the results so guys the simulation is done the code simulation is done and now let's just check the files so I'm just here in the directory of my project ok and I'll search for all the text files and I have here two instagrams okay one that is the value that we had before in the simulation seen here the system okay and now we are interested on the on the histogram that got calculate in the code simulation okay so let's check so they are basically the same file they have the same quantity I would say okay so ah another point before we finish this part let's go back to my club again and let's open the Instagram and I will explain okay guys now with the Instagram here we have simply an array of q5 five elements okay and if you want to answer questions like what are the maximum value of our image okay we can see that is something around 110 and the minimum value is something around for you okay so instead of doing Traverse in the image with a for loop just to get the mean value or in this case then the max value on the image okay we simply run in the histogram we just iterate in a histogram for instance if we iterate from from 255 up to the first element nonzero is going to be our max value and if we go from 0 to our first element nonzero in the direction to 255 it's going to the first non-zero element would be our mean okay I hope you guys enjoyed now the last step is just to export the this block and in the next video I'm going to show how to do the the image stretch okay sorry the histogram stretching using this IP core as well so I whisk probably I will cut these in three videos or two videos like this second one a little bit bigger but this one I choose to be smaller then you guys can can understand better okay so basically we do the histogram quite simple we just for every element on the stream we just increment the value of the pixel in an array of 256 elements by the way to 5/6 just because our grayscale if I have more colors should be like three three arrays one for each color or stuff IDs okay so I hope you guys enjoy it and let's move to the next video and see you guys soon

You Might Also Like

11 Comments

  • Reply rene souza' July 12, 2019 at 8:42 pm

    Amigo …vc é brasileiro ?

  • Reply Julien Claisse July 12, 2019 at 8:42 pm

    Hi, thanks for your really good tutorials.
    I have a question regarding the #pragma HLS PIPELINE. As you had commented them out in your google driver, I saw that without them the 2nd loop is 1 cycle faster. Do you know why? That is the default synthesis loop mode in case of no directives #pragma?

  • Reply Minh Hoàng Vũ July 12, 2019 at 8:42 pm

    wow, extremely good tutorial, thank you so much

  • Reply Ali Mirzaeyan July 12, 2019 at 8:42 pm

    HI,
    thank you for the video, I have a question related to test_core.cpp, I noticed you have added the "doHist(inputStream, histro)" after the for loop, shouldn't it be inside the for loop after the "inputStream << valIn" ? please correct me if I'm wrong

  • Reply swati raut July 12, 2019 at 8:42 pm

    can you plz give the detail of that book ,that which book and author and edition is that book having ?

  • Reply Dubacharla Gyaneshwar July 12, 2019 at 8:42 pm

    Can you do a video on how to read image, convert to matrix form and to display it for arithmetic manipulations?

  • Reply George Tzimpragos July 12, 2019 at 8:42 pm

    awesome videos! very helpful 🙂

  • Reply Chibi chan July 12, 2019 at 8:42 pm

    Where can we get the example code you used so we can try it ourselves? Thanks for the tutorials, they are extremely helpful.

  • Reply Chibi chan July 12, 2019 at 8:42 pm

    Where can we get the example code you used so we can try it ourselves? Thanks for the tutorials, they are extremely helpful.

  • Reply Chibi chan July 12, 2019 at 8:42 pm

    Where can we get the example code you used so we can try it ourselves? Thanks for the tutorials, they are extremely helpful.

  • Reply Chibi chan July 12, 2019 at 8:42 pm

    Where can we get the example code you used so we can try it ourselves? Thanks for the tutorials, they are extremely helpful.

  • Leave a Reply