问题描述
代码中,创建了一个opencv的Mat图像(并打印了所有的像素值),然后通过vxCreateImageFromHandle函数将Mat图像转为了vx_image图像(通过映射的方式打印了所有的像素值,通过日志可以看出与之前打印相同)。然后创建graph,将其作为vxNotNode的输入,运行图之后,将vxNotNode节点的输出图像进行映射打印。按理说,打印的像素值应该是输入图像的取反图像(即输出像素值=255-输入像素值),然而事实并非如此,该问题应该如何解决?
代码
void show_image_all_pixel(vx_image in_img)
{
vx_uint32 width;
vx_uint32 height;
vxQueryImage(in_img, (vx_enum)VX_IMAGE_WIDTH, &width, sizeof(vx_uint32));
vxQueryImage(in_img, (vx_enum)VX_IMAGE_HEIGHT, &height, sizeof(vx_uint32));
vx_map_id map_id_in;
vx_rectangle_t rect_in = {0, 0, width, height};
vx_imagepatch_addressing_t map_addr_in;
vx_uint8* map_ptr_in;
vxMapImagePatch(in_img, &rect_in, 0, &map_id_in, &map_addr_in, (void **)&map_ptr_in, VX_READ_ONLY, VX_MEMORY_TYPE_HOST, VX_NOGAP_X);
printf("/************************************打印图像像素值************************************/\n");
volatile vx_uint32 i,j,k;
i=j=k=0;
for(volatile vx_uint32 y = 0; y < map_addr_in.dim_y; y+=map_addr_in.step_y)
{
j = (map_addr_in.stride_y*y*map_addr_in.scale_y)/VX_SCALE_UNITY;
for (volatile vx_uint32 x = 0; x < map_addr_in.dim_x; x+=map_addr_in.step_x)
{
i = j + (map_addr_in.stride_x*x*map_addr_in.scale_x)/VX_SCALE_UNITY;
k++;
if (0 == k%width)
{
printf("%u\n", *(map_ptr_in+i));
}
else
{
printf("%u, ", *(map_ptr_in+i));
}
}
}
printf("/************************************打印图像像素值结束************************************/\n");
vxUnmapImagePatch(in_img, map_id_in);
}
void main(void)
{
vx_uint32 width = 16;
vx_uint32 height = 5;
// 创建opencv图像
Mat img(height,width,CV_8UC1,Scalar(0));
img(Range(1,5), Range(1,5)) = Scalar(255);
printf("opencv创建图像(高*宽*通道数):%d*%d*%d\n", img.rows, img.cols, img.channels());
cout << "img.elemSize(): " << img.elemSize() << endl;
cout << "img.elemSize1(): " << img.elemSize1() << endl;
cout << "img.step: " << img.step << endl;
cout << "img.step1(): " << img.step1() << endl;
imwrite("pics/inImg_cvSave.bmp", img);
// 打印opencv创建的图像的所有像素
cout << "---------------------------------打印opencv创建的图像的所有像素---------------------------------" << endl;
int tt=0;
for (int i=0; i<(int)height; i++)
{
for(int j=0; j<(int)width; j++)
{
tt++;
if (0 == tt%width)
{
cout << img.at(i,j) << endl;
}
else
{
cout << (int)img.at(i,j) << ", ";
}
}
}
// 利用opencv图像信息配置openvx图像地址结构
vx_imagepatch_addressing_t addr;
addr.dim_x = width;
addr.dim_y = height;
addr.stride_x = (vx_uint32)img.elemSize();
addr.stride_y = (vx_uint32)img.step;
addr.step_x = 1;
addr.step_y = 1;
addr.scale_x = VX_SCALE_UNITY;
addr.scale_y = VX_SCALE_UNITY;
void* ptr = img.data;
// 创建上下文,输入图像
vx_context context = vxCreateContext();
ERROR_CHECK_OBJECT(context);
vx_image in_img;
vx_status status = VX_SUCCESS;
// 从句柄中导入输入图像,打印图像属性,openvx图像地址结构
in_img = vxCreateImageFromHandle(context, VX_DF_IMAGE_U8, &addr, &ptr, VX_MEMORY_TYPE_HOST);
cout << "---------------------------------打印vxCreateImageFromHandle创建的图像的所有像素---------------------------------" << endl;
show_image_all_pixel(in_img);
// 创建图,创建输出图像
vx_graph graph = vxCreateGraph(context);
vx_image out_img = vxCreateImage(context, width, height, VX_DF_IMAGE_U8);
// 创建节点
vx_node n0 = vxNotNode(graph, in_img, out_img);
// 验证图,保存图结构
status = vxVerifyGraph(graph);
// 调度图
status = vxScheduleGraph(graph);
status = vxWaitGraph(graph);
if (VX_SUCCESS == status)
{
cout << "---------------------------------打印vxNotNode输出图像的所有像素---------------------------------" << endl;
show_image_all_pixel(out_img);
}
// 释放资源
vxReleaseImage(&in_img);
vxReleaseImage(&out_img);
vxReleaseNode(&n0);
vxReleaseGraph(&graph);
vxReleaseContext(&context);
}
日志
APP: Init ... !!!
MEM: Init ... !!!
MEM: Initialized DMA HEAP (fd=4) !!!
MEM: Init ... Done !!!
IPC: Init ... !!!
IPC: Init ... Done !!!
REMOTE_SERVICE: Init ... !!!
REMOTE_SERVICE: Init ... Done !!!
30433.712534 s: GTC Frequency = 200 MHz
APP: Init ... Done !!!
30433.712892 s: VX_ZONE_INIT:Enabled
30433.712904 s: VX_ZONE_ERROR:Enabled
30433.712948 s: VX_ZONE_WARNING:Enabled
30433.713889 s: VX_ZONE_INIT:[tivxInitLocal:130] Initialization Done !!!
30433.715078 s: VX_ZONE_INIT:[tivxHostInitLocal:93] Initialization Done for HOST !!!
/************************************打印Target使能情况************************************/
TIVX_TARGET_A72_0 Enable: YES
TIVX_TARGET_A72_1 Enable: YES
TIVX_TARGET_A72_2 Enable: YES
TIVX_TARGET_A72_3 Enable: YES
TIVX_TARGET_DSP1 Enable: YES
TIVX_TARGET_DSP2 Enable: YES
TIVX_TARGET_DSP_C7_1 Enable: YES
/************************************打印Target使能情况结束************************************/
opencv创建图像(高*宽*通道数):5*16*1
img.elemSize(): 1
img.elemSize1(): 1
img.step: 16
img.step1(): 16
---------------------------------打印opencv创建的图像的所有像素---------------------------------
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
---------------------------------打印vxCreateImageFromHandle创建的图像的所有像素---------------------------------
/************************************打印图像像素值************************************/
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 255, 255, 255, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
/************************************打印图像像素值结束************************************/
---------------------------------打印vxNotNode输出图像的所有像素---------------------------------
/************************************打印图像像素值************************************/
90, 30, 1, 1, 22, 0, 0, 0, 33, 0, 0, 0, 44, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
/************************************打印图像像素值结束************************************/
30433.719841 s: VX_ZONE_INIT:[tivxHostDeInitLocal:107] De-Initialization Done for HOST !!!
30433.724260 s: VX_ZONE_INIT:[tivxDeInitLocal:193] De-Initialization Done !!!
APP: Deinit ... !!!
REMOTE_SERVICE: Deinit ... !!!
REMOTE_SERVICE: Deinit ... Done !!!
IPC: Deinit ... !!!
IPC: DeInit ... Done !!!
MEM: Deinit ... !!!
DDR_SHARED_MEM: Alloc's: 1 alloc's of 80 bytes
DDR_SHARED_MEM: Free's : 1 free's of 80 bytes
DDR_SHARED_MEM: Open's : 0 allocs of 0 bytes
DDR_SHARED_MEM: Total size: 536870912 bytes
MEM: Deinit ... Done !!!
APP: Deinit ... Done !!!