读写函数
函数的定义:
-
fopen(const wchar_t * restrict _Filename,const wchar_t *restrict _Mode) __MINGW_ATTRIB_DEPRECATED_SEC_WARN;
例:fpb = fopen(“B.txt”, “r”); -
fwrite(const void * restrict _Str,size_t _Size,size_t _Count,FILE * restrict _File);
#include <stdio.h>
int main(){
FILE *fp;
uchar C[]="111111111112222222222222223333333333333344";
fp = fopen( "B.txt" , "w" );
fwrite(C,sizeof(C),1,fp);
fclose(fp);
return 0;
}
创建的文件在build文件夹下:
读取图片像素值
printf函数:“%u” 整数的参数会被转成无符号的十进制数字,打印输出时,输出正常:
读取出来的像素值存储在 uint8_t 定义的数组中,需要注意的是如果将数组中的像素值再次写入到txt文本中,内容是乱码的。
源码
imge.cpp
#include <stdio.h>
#include "imge.h"
void hls_pixel(SRC_IMAGE& src){
FILE *fp;
uint8_t B[32*32];
uint8_t G[32*32];
uint8_t R[32*32];
int k = 0;
RGB_PIXEL src_data;
for(int i = 0; i < 32 ; i++)
{
for(int j = 0; j < 32; j++)
{
src >> src_data;
//获取RGB通道数据
B[k] = src_data.val[0];
G[k] = src_data.val[1];
R[k] = src_data.val[2];
printf("B is %d \n", B[k]);
printf("G is %d \n", G[k]);
printf("R is %d \n", R[k]);
//printf("I is %u \n", i);
k = k + 1;
}
}
}
void pixel(AXI_STREAM& src_axi, int src_rows, int src_cols){
#pragma HLS INTERFACE axis port=src_axi
#pragma HLS RESOURCE core=AXI_SLAVE variable=src_rows metadata="-bus_bundle CONTROL_BUS"
#pragma HLS RESOURCE core=AXI_SLAVE variable=src_cols metadata="-bus_bundle CONTROL_BUS"
#pragma HLS RESOURCE core=AXI_SLAVE variable=return metadata="-bus_bundle CONTROL_BUS"
#pragma HLS INTERFACE ap_stable port=src_rows
#pragma HLS INTERFACE ap_stable port=src_cols
SRC_IMAGE imag_0(src_rows,src_cols);
hls::AXIvideo2Mat(src_axi, imag_0);
hls_pixel(imag_0);
}
imge.h
#ifndef _IMGE_H_
#define _IMGE_H_
#include "hls_video.h"
#include "ap_int.h"
#include "hls_stream.h"
#define SRC_WIDTH 32
#define SRC_HEIGHT 32
//#define INPUT_IMAGE "lena_gray.jpg"
typedef hls::stream<ap_axiu<24,1,1,1> > AXI_STREAM;
typedef hls::Mat<SRC_HEIGHT, SRC_WIDTH, HLS_8UC3> SRC_IMAGE;
typedef hls::Scalar<3, unsigned char> RGB_PIXEL;
typedef unsigned char uchar;
typedef unsigned int uint;
void pixel(AXI_STREAM& src_axi, int src_rows, int src_cols);
#endif
imge_tb.cpp
#include "imge.h"
#include "hls_opencv.h"
#include "iostream"
#include <time.h>
using namespace std;
using namespace cv;
#define INPUT_IMAGE "1.jpg"
int main ()
{
IplImage* src = cvLoadImage(INPUT_IMAGE);//,CV_LOAD_IMAGE_ANYCOLOR);
AXI_STREAM src_axi;
IplImage2AXIvideo(src, src_axi);
pixel(src_axi,SRC_HEIGHT, SRC_WIDTH);
//cvShowImage("src",src);
//cvWaitKey(0);
}