-1

I have been trying to make a median filter using vivado hls. A person advised me to use the OpenCV imread function, but when I do use it the program doesn't recognize it even though I have included the library of OpenCV. If I don't use imread function and use Mat and cvLoadImage I get an error saying the following:

/../HLSAXIStreamMedian_tb.cpp:109:15: error: missing template arguments before '.' token

If anyone knows how to solve this problem or give me an alternative I would appreciate it. This is the code from my test bench:

#include "HLSAXIStreamMedian.h"

int main(void) {

    spix imageIn[MAX_HEIGHT][MAX_WIDTH]; // May have to malloc these if they're large
    spix imageOut[MAX_HEIGHT][MAX_WIDTH];
    spix imageGold[MAX_HEIGHT][MAX_WIDTH];

    static uint8_t frameIn[MAX_HEIGHT * MAX_WIDTH];

    FILE * imgFile = fopen("C:/img.bin","rb"); // "rb" is important - forces the image to be opened in binary mode.

    fread(frameIn,1,MAX_HEIGHT*MAX_WIDTH,imgFile);

    fclose(imgFile);

    // Organise that data into the required image type.

    spix dataIn[MAX_HEIGHT][MAX_WIDTH];

    for (int y = 0; y < MAX_HEIGHT; y++) {
        for (int x = 0; x < MAX_WIDTH; x++) {
            spix tmp;
            tmp.data = frameIn[y*MAX_WIDTH + x];
            tmp.last = (x == (MAX_WIDTH-1));
            tmp.user = (x == 0) && (y == 0);
            dataIn[y][x] = tmp;
        }
    }

    // spix array is now initialised.
//    Mat gold(1212, 1216, CV_8UC3, Scalar(0,0, 100)); //create an image ( 3 channels, 8 bit image depth, 1212 high, 1216 wide, (0, 0, 100) assigned for Blue, Green and Red plane respectively. )
//    imageGold = gold;
    IplImage* test = cvLoadImage("C:/MedianTrial/test_image.PNG");
    test = Mat.dataIn;
    IplImage* gold = cvLoadImage("C:/MedianTrial/gold_output.PNG");
    gold = Mat.imageGold;
    //Mat test;
    //test = imread("C:/MedianTrial/test_image.PNG",in_pix); // Read a sample image. "imread" does not actually exist; read or generate an image using a method of your choice.
    //Mat gold;
    //gold = imread("C:/MedianTrial/gold_output.PNG",imageGold); // Read a known-good image (eg. generated using Matlab's median filter).

    top_median(imageIn,imageOut,1080,1920); // Call the test function.

    for (int i = 0; i < 1080; i++) {
        for (int j = 0; j < 1920; j++) {
            if (imageOut != imageGold) {
                printf("Data mismatch");// at position (%d,%d): %d != %d\n",i,j,imageOut[i][j],imageGold[i][j]);
                return -1;
            }
        }
    }
}

This is the header file:

#define KMED 3 // KMED can be 3, 5, 7
#define KKMED 1 // KKMED == 1 for 3x3 window
#define MIN(x,y) ( (x)>(y) ? (y) : (x) )
#define MAX(x,y) ( (x)>(y) ? (x) : (y) )
#include "opencv/cxcore.h"
#include <opencv2/core/core.hpp>
#include "opencv2/highgui/highgui.hpp"
//opencv_core
//opencv_imgcodecs
//opencv_highgui
#include <iostream>
#include "hls_video.h"
#include "hls_opencv.h"
#include <ap_int.h>
#include "ap_axi_sdata.h"
#include <stdio.h>
#include <string.h>
#include <hls_stream.h>
#include <stdlib.h>
using namespace hls;
//using namespace cv;
//using namespace std;
#include <ap_axi_sdata.h>
typedef ap_axis<8,2,5,6> spix;
#ifndef GRAY11
typedef unsigned char pix_t; // 8-bit per pixel
#else
#include <ap_int.h>
typedef ap_int<11> pix_t; // 11-bit per pixel
#endif
#define MAX_HEIGHT 1080
#define MAX_WIDTH 1920
void top_median(spix in_pix[MAX_HEIGHT][MAX_WIDTH],
spix out_pix[MAX_HEIGHT][MAX_WIDTH],
short int height, short int width);
//spix in_pix[MAX_HEIGHT][MAX_WIDTH];
//spix out_pix[MAX_HEIGHT][MAX_WIDTH];
//spix out_pixtb[MAX_HEIGHT][MAX_WIDTH];
//short int height;
//short int width;
pix_t Median(pix_t window[KMED*KMED]);
    enter code here
    enter code here
  • I don't know the library you are referring to, but to use `imread` in OpenCV you will need the `highgui` module, so include `highgui.h` or `highgui.hpp`. – Adi Shavit Jul 04 '16 at 12:39
  • Tell us which line is 109 in your preprocessed file. You haven't marked it, and we have no idea how long the `#include`d header is in order to do the math ourselves. – underscore_d Jul 04 '16 at 12:40
  • In the header file I included those two libraries (highgui.h and highgui.hpp) – Michael Nakhleh Jul 04 '16 at 13:41
  • The line 109 is the one where it says test = Mat.dataIn – Michael Nakhleh Jul 04 '16 at 13:42
  • This error occurs of course for the line 111 as well (gold = Mat.imageGold) – Michael Nakhleh Jul 04 '16 at 13:45
  • Please describe what exactly you want to do in this line `test = Mat.dataIn; ` as it is incorrect from syntax to logic. – Logman Jul 04 '16 at 14:03
  • I am trying to assign the data written to test inside the array dataIn to be able to use it or output comparison. I have been changing a lot based on recommendations I find. I cannot know myself if what I write is correct or no because I am a beginner. – Michael Nakhleh Jul 04 '16 at 14:10
  • Look at [this](http://stackoverflow.com/a/30849778/2483065) how to convert IplImage to Mat. But from your code it's clear that you don't understand basic concepts of c++. Before you try to use complex solutions like openCV please read something about c++ itself. – Logman Jul 04 '16 at 17:51
  • Thank you for your help. Your guessing is right that I barely know the basics of c++. I have tried what you have gave me but then the simulation doesn't work. It crashes giving me this error: @E Simulation failed: SIGSEGV. @E [SIM-1] CSim failed with errors. – Michael Nakhleh Jul 05 '16 at 07:56
  • This is how I modified it according to what you gave: – Michael Nakhleh Jul 05 '16 at 07:58
  • IplImage* test = cvLoadImage("C:/MedianTrial/test_image.PNG"); cv::Mat t = cv::cvarrToMat(test); – Michael Nakhleh Jul 05 '16 at 07:58
  • @MichaelNakhleh Probably you do not modify rest of your code and I only show you how to resolve image loading problem. I also see that you use wrong slash /, in windows you should use \ (inside string you need to escape it like "\\") (and your path suggested that you using windows). And one more advise when you referring in comment to someone else comment add @ and then name as I do. If you do that another person will see that someone response to his comment. – Logman Jul 05 '16 at 19:31
  • @Logman Thank you for your advice. I have changed the / to \ yet I still get this error, the exact same one I had at the beginning. When I used what you told me for the conversion the program just crashes with no errors as I mentioned already. I hope you can help me because I am stuck at this point and until now no one from any forum gave me a solution that worked. – Michael Nakhleh Jul 06 '16 at 08:45

1 Answers1

0

I can't give you complete solution as I don't have hls library but can point you to right direction.

  1. Don't use using namespace ....

  2. Load image using cvLoadImage() or cv::imread().

  3. a. (if cvLoadImage) Convert IplImage* to hls::Mat using IplImage2hlsMat().
    b. (if imread) Convert cv::Mat to hls::Mat using cvMat2hlsMat().
    It is important to convert to hls::Mat not to cv::Mat.

  4. Create hls::Window and initialize it with your filter for ex.
    [ 1, 1, 1 ]
    [ 1, 1, 1 ]
    [ 1, 1, 1 ]

  5. Use hls::Filter2D with your hls::Mat and hls::Window to apply median filter.

Refs:

Article how to create filter in OpenCV
HLS doc

Logman
  • 4,031
  • 1
  • 23
  • 35
  • Thank you very much for the information. I am trying to use the cvMat2hlsMat() but I get just as for the imread(), it gets underlined with red saying "the function could not be resolved" as if I am not including the cv library which is strange because I have it included. Is there a specific library maybe I haven't included or is it a different problem? Any ideas? – Michael Nakhleh Jul 07 '16 at 08:03
  • And what about `cvLoadImage`? Can you copy whole error message when you use `imread`? Did you remove `using namespace` and use `imread` with `cv::`? – Logman Jul 07 '16 at 14:27
  • I have wrote it like this: – Michael Nakhleh Jul 11 '16 at 08:29
  • cv::cvarrToMat(imageGold); imageGold = cv::imread("C:\\MedianTrial\\test_image.PNG"); – Michael Nakhleh Jul 11 '16 at 08:29
  • I get this error now ../HLSAXIStreamMedian_tb.cpp:113:66: error: incompatible types in assignment of 'cv::Mat' to 'spix [108][192] {aka ap_axis<8, 2, 5, 6> [108][192]}' I did remove the using namespace as you told me – Michael Nakhleh Jul 11 '16 at 08:30
  • If I do it like this cv::Mat g = cv::imread("C:\\MedianTrial\\test_image.PNG"); imageGold = spix.g; Then I get this error: expected primary-expression before '.' token for his line imageGold = spix.g; – Michael Nakhleh Jul 11 '16 at 08:51
  • I used different options to fix this specific problem. Every time I figure out how to write it correctly and get rid of the errors it simply crashes. I believe there might be a problem in this line of code: FILE * imgFile = fopen("C:\\img.bin","rb"); Or maybe when trying to open the image, something doesn't go right and the crash happens. I don't know what this line of code is used for because I had this piece of code written by someone from another forum. If you know it might help figure out the real problem. – Michael Nakhleh Jul 11 '16 at 12:31
  • @MichaelNakhleh Please write exactly what you want to do because your code is not self-explanatory and most parts of it is wrong. In first part of it you declare data that you do not initialize and next you try to open some img.bin as binary stream and try to read it, than you open your image files and do nothing with it and in the last part you perform mediana on random data and you checking if in and out data are the same. Also you can't do something like `type.variable` it's just not how c++ works this syntax is reserved to `variable.property`. – Logman Jul 11 '16 at 17:15
  • I know probably most of what I write is wrong because as I said I am a beginner. What I want to do with my code is simply read two images, an input image and also a golden image. Then use the input image in the median function to do the filtering and after that use the output image to compare it with the golden image. – Michael Nakhleh Jul 12 '16 at 07:41
  • @MichaelNakhleh First please remove your code from main function. Second try to implement my solution step by step. Try to compile after each step and check if there is no runtime errors. As you do not provide any info about your median filter, for prof of concept use `hls::GaussianBlur` filter this way you can omit window step. Also for the same reason I'm positive that your output image will be completely different from your golden image. At the end put your validation procedure but only if no runtime errors occurred. If you encounter any problem just post it here. – Logman Jul 12 '16 at 10:51