a.at<uchar>(x,y) wont works in Vivado SDSoC - vivado-hls

I want to get 2d array from a Mat in Vivado SDSoC but Im not able to do that because as described Xilinx(XAPP1167),
cv::Mat<>.at() method and cvGet2D() function have no corresponding equivalent
function in the synthesizable library
I appreciate any help. Thank you.
The project is about face recognition system. The system first will go viola-jone face detection and then feed the output to the CNN classification.
The output of viola-jones face detection is in unsigned char*.
I plant to convert it back to Mat and obtain 2d array for the input of CNN Classifier.
#include <cstdio>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include <ctime>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <sys/mman.h>
#include <malloc.h>
#include <unistd.h>
#include <sds_lib.h>
#include <opencv2/core/core.hpp>
#include <opencv2/contrib/contrib.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/core/core_c.h"
using namespace cv;
using namespace std;
void resize_2_gray(unsigned char *imageIn, unsigned char *imageOut)
{
int k=0;
int coord;
for (int i=0; i<240; i++) {
for(int j=0; j<320; j++){
//coord=(i*2*640+j*2)*3;
#pragma HLS PIPELINE II=1
coord=6*(i*640+j);
imageOut[k] = 0.2126*imageIn[coord] + 0.7152*imageIn[coord+1] + 0.0722*imageIn[coord+2] ;
k++;
}
}
}
int main(){
Mat ROI;
unsigned int cam_num =5;//camera USB port
unsigned char *image_ROI;
image_ROI = (unsigned char*)sds_alloc(sizeof(unsigned char)*56*46);
VideoCapture stream(cam_num);
stream.read(ROI);
cvtColor(ROI,ROI, CV_BGR2RGB);
resize_2_gray(ROI.data, image_ROI);
double face_2darray [56][46]={0};
int h1=3, w1=3,w2=46,h2=56;
int x_ratio = (int)((w1<<16)/w2)+1;
int y_ratio = (int)((h1<<16)/h2)+1;
int x2,y2;
//resize image before fed into classification
for (int a=0;a<h2;a++)
{
for (int b=0;b<w2;b++)
{
x2=((b*x_ratio)>>16);
y2=((a*y_ratio)>>16);
image_ROI[(a*w2)+b]=ROI.data[(y2*w1)+x2];
face_2darray[a][b]=(double)image_ROI[a+b];
face_2darray[a][b]= 2*(face_2darray[a][b]/255)-1;
}
}
Mat Image = Mat(56,46,0,image_ROI);
imshow("Output",Image);
sds_free(image_ROI);
}

I believe the synthesized version is using a streaming interface, so you can't use random access APIs, and instead must read pixels/elements one by one.

Related

Why it isn't working? I have seen examples using this, but when I use it, it crashes. Why?(C)

Why can't I do g->n=n ? Can someone explain?
#include <assert.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define MAX 100000
int N[]={100,200,300,400,500,600,700,800,900,1000};
double B[]={0.125, 0.250, 0.375, 0.5, 0.625, 0.750, 0.875};
typedef struct Graph
{
int n;
int M[MAX][MAX];
int val;
int adjacent[MAX-1];
}G;
struct Graph * RandomDirectedGraph(int n, double b)
{
struct Graph * g = (struct Graph*)malloc(sizeof(struct Graph));
g->n=n;
int u,i,v;
for(u=0;u<n;u++)
{
for(i=0;i<n;i++)
{
g->M[u][i]=0;
}
}
int m=b*n*n;
for(i=0;i<m;i++)
{
do
{
u=rand()%n;
v=rand()%n;
}
while(u==v || g->M[u][v]==1);
g->M[u][v]=1;
}
};
int main()
{
int i,j,n,b;
for(i=0;i<sizeof(N)/sizeof(*N);i++)
{
for(j=0;j<sizeof(B)/sizeof(*B);j++)
{
RandomDirectedGraph(N[i],B[j]);
}
}
return 0;
}
When I compile and run it, ERROR pops up and says that some memory can't be written. I don't know what I'm doing wrong here.
The issue is that everytime you are executing the method RandomDirectedGraph, your application tries to reserve about 40GB of RAM memory. I guess your computer doesn't have that much.
Furthermore you should release all memory by calling free on the pointers.
For such big matrices, you don't use classic C matrices. You need external librarys for sparse matrix calculations like https://www.alglib.net/matrixops/sparse.php

in c programming , adding function with #pragma omp parallel for, cause other variable to be changed

hi i have struct which contains a string in c. i malloc memory for string and when i printf its value it is empty as i expect.
after adding new function which uses open mp tag (#pragma omp parallel for) above of a dummy loop, value of my string changes.
how can i stop changing of my string value?
whats the reason of this behavior?
here is my code and output before adding function
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define RESULT_LENGTH 100000
struct SimpleSearch {
char* ids;
};
int main () {
struct SimpleSearch search;
search.ids = malloc((RESULT_LENGTH) * sizeof(char));
printf("search id now is: %s\n",search.ids);
return 0;
}
output is:
search id now is:
then i add new function and complete code will be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <omp.h>
#define RESULT_LENGTH 100000
struct SimpleSearch {
char* ids;
};
void x(){
#pragma omp parallel for
for(int i=0; i <(10+1); i++) {
printf("%d \n",i);
}
}
int main () {
struct SimpleSearch search;
search.ids = malloc((RESULT_LENGTH) * sizeof(char));
printf("search id now is: %s\n",search.ids);
return 0;
}
now ouput will be:
search id now is: ���

C error in linking library

I am begginer in C and I have a problem with linking .c and .h files into main.c
I tried to link it and it looks like this in main.c :
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "hangman.h"
#include "hangman.c"
#include <string.h>
#include <time.h>
#include <ctype.h>
int main(void)
{
char secret[20];
srand(time(NULL));
getWord(secret);
hangman(secret);
return 0;
}
and like this in .c file
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include "hangman.h"
int isWordGuessed(const char secret[], const char lettersGuessed[]){
int pom = 0;
int i,j;
for(i = 0; i < strlen(secret); i++){
for (j= 0; j < strlen(lettersGuessed); j++)
{
if(secret[i] == lettersGuessed[j])
pom = 1;
}
if(pom == 1){
pom = 0;
}
else{
return 0;
}
}
return 1;
}
and like this in .h file
#define WORDLIST_FILENAME "words.txt"
int isWordGuessed(const char secret[], const char lettersGuessed[]);
Program just throws an exception. It tells me:
hangman.o: In function isWordGuessed':
hangman.c:(.text+0x0): multiple definition ofisWordGuessed'
main.o:main.c:(.text+0x0): first defined here
#include "hangman.h"
#include "hangman.c"
Do not include .c source files in your program. The include directive will simply copy hangman.c in your main.c.

fatal error: stdio.matrixVector: File or directory not found

I'm trying to compile a C method
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
void myMethod(double vector[], double matrixVector[])
{
int n = sizeof(matrixVector)/sizeof(double);
gsl_matrix *X = gsl_matrix_calloc(n, 3);
gsl_vector *Y = gsl_vector_alloc(n);
gsl_vector *beta = gsl_vector_alloc(3);
for (int i = 0; i < n; i++) {
....
}
....
}
but I get this error
fatal error: stdio.matrixVector: File or directory not found
How could I fix this?
1- all the include are wrong
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
should be
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_multifit.h>
note that 3 include need the GNU Scientific Library, make sure to have installed it before compile the code
2- in the loop
for (int i = 0; i < n; i++) {
you have to declare i outside of the loop (if you aren't in C99 mode).
You need to change the #include lines, you've replaced all of the .h suffixes with .matrixVector in what I assume was a bulk replace for the rest of the file.
Change:
#include <stdio.matrixVector>
#include <gsl/gsl_matrix.matrixVector>
#include <gsl/gsl_math.matrixVector>
#include <gsl/gsl_multifit.matrixVector>
To:
#include <stdio.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_multifit.h>

character array to floating point conversion

I am trying to convert the output buffer(character array)
of the code below to floating point format for further calculations.
Can anybody tell me how to do it.
#include "usbtmc.h"
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <getopt.h>
#include <inttypes.h>
#include <sys/types.h>
#include <pthread.h>
int main()
{
int myfile;
char buffer[4000];
int actual;
myfile=open("/dev/usbtmc1",O_RDWR);
if(myfile>0)
{
system("echo MEAS:VOLT:AC?>/dev/usbtmc1");
actual=read(myfile,buffer,4000);
buffer[actual] = 0;
printf("Response = \n %s\n",buffer);
close(myfile);
}
return 0;
}
The sample output for this code is
Response =
+1.29273072E-04
You may have two ways:
using double atof(const char* str)
float f;
f = (float)atof(buffer);
printf("%f",f); // here you can use f
using int sscanf( const char * s, const char * format, ...)
float f;
sscanf(buffer,"%f",&f);
printf("%f",f); // here you can use f

Resources