Example #1
0
void userCalculatePerspectiveTransformFromLK(vx_matrix matrix_forward, vx_matrix matrix_backward, vx_array old_features,
        vx_array new_features)
{
    vx_float32 mat1[3][3];
    vx_float32 mat2[3][3];
    vx_size olen = 0;
    vx_size nlen = 0;
    vx_keypoint_t *old_features_ptr = NULL;
    vx_keypoint_t *new_features_ptr = NULL;
    vx_size old_features_stride = 0;
    vx_size new_features_stride = 0;
    vx_uint32 ind;
    //vx_float32 *A = malloc(olen+nlen);
    //vx_float32 *b = malloc(olen+nlen);

    vxQueryArray(old_features, VX_ARRAY_ATTRIBUTE_NUMITEMS, &olen, sizeof(olen));
    vxQueryArray(new_features, VX_ARRAY_ATTRIBUTE_NUMITEMS, &nlen, sizeof(nlen));

    if (olen != nlen)
        return;

    vxAccessArrayRange(old_features, 0, olen, &old_features_stride, (void **) &old_features_ptr, VX_READ_ONLY);
    vxAccessArrayRange(new_features, 0, nlen, &new_features_stride, (void **) &new_features_ptr, VX_READ_ONLY);

    /*! \internal do least square algorithm that find perspective transform */
    /*! \see "Computer Vision Algorithm and Application by Richard Szeliski section 6.1.3 */
    for (ind = 0; ind < nlen; ind++)
    {
        //vx_int32 matrix_index = 2 * ind;

        //vx_keypoint_t old_kp = vxArrayItem(vx_keypoint_t, old_features_ptr, ind, old_features_stride);
        //vx_keypoint_t new_kp = vxArrayItem(vx_keypoint_t, new_features_ptr, ind, new_features_stride);

        /*
         A[matrix_index] = {old_kp.x, old_kp.y,    1,
         0,       0,    0,
         -old_kp.x*new_kp.x,
         -old_kp.x*new_kp.y};

         A[matrix_index+1] = {0, 0, 0,
         old_kp.x,
         old_kp.y,
         1,
         -old_kp.y*new_kp.x,
         -old_kp.y*new_kp.y};

         b[matrix_index]   = new_kp.x - old_kp.x;
         b[matrix_index+1] = new_kp.y - old_kp.y;
         */
    }

    //least_square_divide(A,b, mat1);
    //inverse_matrix(mat1, mat2);

    vxCommitArrayRange(old_features, 0, 0, old_features_ptr);
    vxCommitArrayRange(new_features, 0, 0, new_features_ptr);

    vxWriteMatrix(matrix_forward, mat1);
    vxWriteMatrix(matrix_backward, mat2);
}
bool CVxParamArray::CompareFrameBitwiseExact(size_t numItems, size_t numItemsRef, vx_uint8 * bufItems, int frameNumber, const char * fileName)
{
	// bitwise exact compare
	size_t numItemsMin = min(numItems, numItemsRef);
	size_t numMismatches = 0;
	if (numItemsMin > 0) {
		void * ptr = nullptr;
		vx_size stride = 0;
		ERROR_CHECK(vxAccessArrayRange(m_array, 0, numItems, &stride, &ptr, VX_READ_ONLY));
		for (size_t i = 0; i < numItems; i++) {
			vx_uint8 * item = vxFormatArrayPointer(ptr, i, stride);
			if (memcmp(item, bufItems + i * m_itemSize, m_itemSize) != 0) {
				numMismatches++;
			}
		}
		ERROR_CHECK(vxCommitArrayRange(m_array, 0, numItems, ptr));
	}
	numMismatches += max(numItems, numItemsRef) - numItemsMin;
	bool mismatchDetected = false;
	if (numMismatches > 0) {
		printf("ERROR: array COMPARE MISMATCHED %d/%d for %s with frame#%d of %s\n", (int)numMismatches, (int)numItems, GetVxObjectName(), frameNumber, fileName);
		mismatchDetected = true;
	}
	else {
		if (m_verbose) printf("OK: array COMPARE MATCHED for %s with frame#%d of %s\n", GetVxObjectName(), frameNumber, fileName);
	}
	return mismatchDetected;
}
void FeatureTracker::changeType(vx_array vx_pt, vector<cv::Point2f> &cv_pt )
{

    vx_size vCount = 0;
    vxQueryArray(vx_pt, VX_ARRAY_ATTRIBUTE_NUMITEMS, &vCount, sizeof(vCount));
    vx_enum item_type = 0;
    NVXIO_SAFE_CALL( vxQueryArray(vx_pt, VX_ARRAY_ATTRIBUTE_ITEMTYPE, &item_type, sizeof(item_type)) );
    NVXIO_ASSERT( item_type == NVX_TYPE_POINT2F) ;
    
    vx_size stride;
    void * featureData = NULL;
    NVXIO_SAFE_CALL( vxAccessArrayRange(vx_pt, 0, vCount, &stride,
                                        (void**)&featureData, VX_READ_ONLY) );
    int id = 0;

    cv_pt.resize(vCount);
    for (vx_size i = 0; i < vCount; i++)
    {
        nvx_point2f_t feature = vxArrayItem(nvx_point2f_t, featureData, i, stride);
        cv_pt[id].x = feature.x;
        cv_pt[id].y = feature.y;
        id++;
     
    }
    NVXIO_SAFE_CALL( vxCommitArrayRange(vx_pt, 0, vCount, featureData) );
    cv_pt.resize(id);
    //cout<<"point size   "<<cv_pt.size()<<"  id  "<<id<<endl;
}
static vx_status VX_CALLBACK vxFindWarpKernel(vx_node node, vx_reference *parameters, vx_uint32 num)
{
    if(num != 3)
        return VX_ERROR_INVALID_PARAMETERS;

    vx_status status = VX_SUCCESS;
    vx_array def_pnts   = (vx_array) parameters[0];
    vx_array moved_pnts = (vx_array) parameters[1];
    vx_matrix matrix    = (vx_matrix)parameters[2];

    vx_size points_num;
    status |= vxQueryArray(def_pnts, VX_ARRAY_ATTRIBUTE_NUMITEMS, &points_num, sizeof(points_num));
    if(status != VX_SUCCESS)
    {
        VX_PRINT(VX_ZONE_ERROR, "Can't query array attribute(%d)!\n", status);
        return VX_FAILURE;
    }
    
    if(points_num < 4)
    {
        vx_float32 matr_buff[9];
        vxAccessMatrix(matrix, (void*)matr_buff);
        memset(matr_buff, 0, sizeof(vx_float32) * 9);
        matr_buff[0] = matr_buff[4] = matr_buff[8] = 1.;
        vxCommitMatrix(matrix, (void*)matr_buff);
        VX_PRINT(VX_ZONE_WARNING, "Number of points less then 4(%d)!\n", points_num);
        return VX_SUCCESS;//VX_FAILURE;
    }

    /*** CV array initialize ***/
    std::vector<cv::Point2f> cv_points_from, cv_points_to;
    cv_points_from.reserve(points_num);
    cv_points_to.reserve(points_num);
    /***************************/

    vx_size i, stride1 = 0ul, stride2 = 0ul;
    void *def_buff = NULL, *moved_buff = NULL;
    status |= vxAccessArrayRange(def_pnts, 0, points_num, &stride1, &def_buff, VX_READ_AND_WRITE);
    status |= vxAccessArrayRange(moved_pnts, 0, points_num, &stride2, &moved_buff, VX_READ_AND_WRITE);

    if(def_buff && moved_buff)
    {
        for (i = 0; i < points_num; i++)
        {
            if(vxArrayItem(vx_keypoint_t, moved_buff, i, stride2).tracking_status)
            {
                cv::Point2f pnt_from;
                pnt_from.x = vxArrayItem(vx_keypoint_t, def_buff, i, stride1).x;
                pnt_from.y = vxArrayItem(vx_keypoint_t, def_buff, i, stride1).y;
                cv_points_from.push_back(pnt_from);

                cv::Point2f pnt_to;
                pnt_to.x = vxArrayItem(vx_keypoint_t, moved_buff, i, stride2).x;
                pnt_to.y = vxArrayItem(vx_keypoint_t, moved_buff, i, stride2).y;
                cv_points_to.push_back(pnt_to);
            }
        }
    }
    else
    {
        status = VX_FAILURE;
    }
    status |= vxCommitArrayRange(def_pnts, 0, points_num, def_buff);
    status |= vxCommitArrayRange(moved_pnts, 0, points_num, moved_buff);
    VX_PRINT(VX_ZONE_LOG, "Number of points = (%d)!\n", cv_points_from.size());

    /*** CV find homography ***/
    cv::Mat_<float> cv_matr = cv::Mat::eye(3, 3, CV_32FC1);
    if(cv_points_from.size() > 0 && cv_points_to.size() > 0)
        cv_matr = cv::findHomography(cv_points_from, cv_points_to, CV_RANSAC);
    else
        VX_PRINT(VX_ZONE_WARNING, "Number of points is equal to zero!\n");
    vx_float32 matr_buff[9];
    status |= vxAccessMatrix(matrix, (void*)matr_buff);
    memcpy(matr_buff, cv_matr.data, sizeof(vx_float32) * 9);

    status |= vxCommitMatrix(matrix, (void*)matr_buff);
    return status;
}
bool CVxParamArray::CompareFrameCoord2d(size_t numItems, size_t numItemsRef, vx_uint8 * bufItems, int frameNumber, const char * fileName)
{
	FILE * fpLog = NULL;
	if (m_fileNameCompareLog.length() > 0) {
		char fileName[MAX_FILE_NAME_LENGTH]; sprintf(fileName, m_fileNameCompareLog.c_str(), frameNumber);
		fpLog = fopen(fileName, "w");
		if (!fpLog) ReportError("ERROR: Unable to create: %s\n", fileName);
		printf("OK: creating array compare output log for %s in %s\n", GetVxObjectName(), fileName);
	}

	enum { // color indices of each list for viewing
		colorIndex_match_XYexact = 0,
		colorIndex_match_XY = 1,
		colorIndex_missing_in_ref = 2,
		colorIndex_missing_in_cur = 3,
	};
	// number of keypoint counts
	size_t count_match_XYexact = 0;
	size_t count_match_XY = 0;
	size_t count_missing_in_ref = 0;
	size_t count_missing_in_cur = 0;

	// reset array list for viewing
	ResetArrayListForView();

	// get reference and actual keypoint buffers
	vx_coordinates2d_t * kpRefBase = (vx_coordinates2d_t *)m_bufForRead, *kpActualBase = nullptr;
	vx_size stride;
	if (numItems > 0) {
		ERROR_CHECK(vxAccessArrayRange(m_array, 0, numItems, &stride, (void **)&kpActualBase, VX_READ_ONLY));
	}

	// try matching reference keypoints with actual
	for (size_t j = 0; j < numItemsRef; j++) {
		vx_coordinates2d_t * kpRef = &kpRefBase[j];
		bool matched = false;
		for (size_t i = 0; i < numItems; i++) {
			vx_coordinates2d_t * kpCur = &vxArrayItem(vx_coordinates2d_t, kpActualBase, i, stride);
			if ((kpCur->x == kpRef->x) && (kpCur->y == kpRef->y)) {
				AddToArrayListForView(colorIndex_match_XYexact, kpCur->x, kpCur->y, 0.0f);
				if (fpLog) fprintf(fpLog, "MATCH-XY-EXACT       -- %5d %5d (ref:%06d) %5d %5d (cur:%06d)\n", kpRef->x, kpRef->y, (int)j, kpCur->x, kpCur->y, (int)i);
				count_match_XYexact++;
				matched = true;
			}
			else if ((abs((vx_int32)kpCur->x - (vx_int32)kpRef->x) <= m_errX) && (abs((vx_int32)kpCur->y - (vx_int32)kpRef->y) <= m_errY)) {
				AddToArrayListForView(colorIndex_match_XY, kpCur->x, kpCur->y, 0.0f);
				if (fpLog) fprintf(fpLog, "MATCH-XY             -- %5d %5d (ref:%06d) %5d %5d (cur:%06d)\n", kpRef->x, kpRef->y, (int)j, kpCur->x, kpCur->y, (int)i);
				count_match_XY++;
				matched = true;
			}
			if (matched)
				break;
		}
		if (!matched) {
			AddToArrayListForView(colorIndex_missing_in_cur, kpRef->x, kpRef->y, 0.0f);
			if (fpLog) fprintf(fpLog, "MISMATCH-WITH-CUR    -- %5d %5d (ref:%06d)\n", kpRef->x, kpRef->y, (int)j);
			count_missing_in_cur++;
		}
	}

	// try matching actual keypoints with reference
	for (size_t i = 0; i < numItems; i++) {
		vx_coordinates2d_t * kpCur = &vxArrayItem(vx_coordinates2d_t, kpActualBase, i, stride);
		bool matched = false;
		for (size_t j = 0; j < numItemsRef; j++) {
			vx_coordinates2d_t * kpRef = &kpRefBase[j];
			if ((abs((vx_int32)kpCur->x - (vx_int32)kpRef->x) <= m_errX) && (abs((vx_int32)kpCur->y - (vx_int32)kpRef->y) <= m_errY)) {
				matched = true;
				break;
			}
		}
		if (!matched) {
			AddToArrayListForView(colorIndex_missing_in_ref, kpCur->x, kpCur->y, 0.0f);
			if (fpLog) fprintf(fpLog, "MISMATCH-WITH-REF    --                          %5d %5d (cur:%06d)\n", kpCur->x, kpCur->y, (int)i);
			count_missing_in_ref++;
		}
	}

	if (numItems > 0) {
		ERROR_CHECK(vxCommitArrayRange(m_array, 0, numItems, kpActualBase));
	}

	// check for overall mismatch criteria
	size_t totalMatched = count_match_XYexact + count_match_XY;
	size_t totalMismatchesOrMissing = max(count_missing_in_ref, count_missing_in_cur);
	size_t total = totalMatched + totalMismatchesOrMissing;
	float percentMismatches = (total > 0) ? (100.0f * (float)totalMismatchesOrMissing / (float)total) : 0.0f;
	bool mismatched = false;
	if (percentMismatches > m_errMismatchPercent) {
		mismatched = true;
		printf("ERROR: array COMPARE MISMATCHED [matched %d; mismatched/missing %d (%.3f%%)] for %s with frame#%d of %s\n", (int)totalMatched, (int)totalMismatchesOrMissing, percentMismatches, GetVxObjectName(), frameNumber, fileName);
		if (fpLog) fprintf(fpLog, "ERROR: array COMPARE MISMATCHED [matched %d; mismatched/missing %d (%.3f%%)] for %s with frame#%d of %s\n", (int)totalMatched, (int)totalMismatchesOrMissing, percentMismatches, GetVxObjectName(), frameNumber, fileName);
	}
	else {
		if (m_verbose) printf("OK: array COMPARE MATCHED %.3f%% for %s with frame#%d of %s\n", 100.0f - percentMismatches, GetVxObjectName(), frameNumber, fileName);
		if (fpLog) fprintf(fpLog, "OK: array COMPARE MATCHED %.3f%% for %s with frame#%d of %s\n", 100.0f - percentMismatches, GetVxObjectName(), frameNumber, fileName);
	}

	if (fpLog) fclose(fpLog);
	return mismatched;
}
int CVxParamArray::WriteFrame(int frameNumber)
{
	// check if user specified file to write
	if (m_fileNameWrite.length() < 1) return 0;

	// create the output file
	char fileName[MAX_FILE_NAME_LENGTH]; sprintf(fileName, m_fileNameWrite.c_str(), frameNumber);
	FILE * fp = fopen(fileName, m_writeFileIsBinary ? "wb" : "w");
	if(!fp) ReportError("ERROR: Unable to create: %s\n", fileName);

	// get numItems and write if any items exist
	vx_size numItems;
	ERROR_CHECK(vxQueryArray(m_array, VX_ARRAY_ATTRIBUTE_NUMITEMS, &numItems, sizeof(numItems)));
	if (numItems > 0) {
		vx_uint8 * base = nullptr;
		vx_size stride;
		ERROR_CHECK(vxAccessArrayRange(m_array, 0, numItems, &stride, (void **)&base, VX_READ_ONLY));
		if (m_writeFileIsBinary)
		{ // write in binary
			for (size_t i = 0; i < numItems; i++) {
				vx_uint8 * item = vxFormatArrayPointer(base, i, stride);
				fwrite(item, 1, m_itemSize, fp);
			}
		}
		else
		{ // write in ASCII mode
			if (m_format == VX_TYPE_KEYPOINT) {
				for (size_t i = 0; i < numItems; i++) {
					vx_keypoint_t * item = (vx_keypoint_t *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%4d %4d %20.12e %20.12e %20.12e %d %20.12e\n", item->x, item->y, item->strength, item->scale, item->orientation, item->tracking_status, item->error);
				}
			}
			else if (m_format == VX_TYPE_COORDINATES2D) {
				for (size_t i = 0; i < numItems; i++) {
					vx_coordinates2d_t * item = (vx_coordinates2d_t *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%4d %4d\n", item->x, item->y);
				}
			}
			else if (m_format == VX_TYPE_COORDINATES3D) {
				for (size_t i = 0; i < numItems; i++) {
					vx_coordinates3d_t * item = (vx_coordinates3d_t *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%4d %4d %4d\n", item->x, item->y, item->z);
				}
			}
			else if (m_format == VX_TYPE_RECTANGLE) {
				for (size_t i = 0; i < numItems; i++) {
					vx_rectangle_t * item = (vx_rectangle_t *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%4d %4d %4d %4d\n", item->start_x, item->start_y, item->end_x, item->end_y);
				}
			}
			else if (m_format == VX_TYPE_INT32 || m_format == VX_TYPE_BOOL) {
				for (size_t i = 0; i < numItems; i++) {
					vx_int32 * item = (vx_int32 *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%d\n", *item);
				}
			}
			else if (m_format == VX_TYPE_UINT32) {
				for (size_t i = 0; i < numItems; i++) {
					vx_uint32 * item = (vx_uint32 *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%u\n", *item);
				}
			}
			else if (m_format == VX_TYPE_FLOAT32) {
				for (size_t i = 0; i < numItems; i++) {
					vx_float32 * item = (vx_float32 *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%.12g\n", *item);
				}
			}
			else if (m_format == VX_TYPE_FLOAT64) {
				for (size_t i = 0; i < numItems; i++) {
					vx_float64 * item = (vx_float64 *)vxFormatArrayPointer(base, i, stride);
					fprintf(fp, "%.12lg\n", *item);
				}
			}
			else {
				// write output as hex values
				for (size_t i = 0; i < numItems; i++) {
					vx_uint8 * item = vxFormatArrayPointer(base, i, stride);
					for (size_t j = 0; j < m_itemSize; j++)
						fprintf(fp, " %02X", item[j]);
					fprintf(fp, "\n");
				}
			}
		}
		ERROR_CHECK(vxCommitArrayRange(m_array, 0, numItems, base));
	}
	fclose(fp);

	return 0;
}
Example #7
0
vx_status vxFWriteImage(vx_image input, vx_array file)
{
    vx_char *filename = NULL;
    vx_size filename_stride = 0;
    vx_uint8 *src[4] = {NULL, NULL, NULL, NULL};
    vx_uint32 p, y, sx, ex, sy, ey, width, height;
    vx_size planes;
    vx_imagepatch_addressing_t addr[4];
    vx_df_image format;
    FILE *fp = NULL;
    vx_char *ext = NULL;
    size_t wrote = 0ul;
    vx_rectangle_t rect;

    vx_status status = vxAccessArrayRange(file, 0, VX_MAX_FILE_NAME, &filename_stride, (void **)&filename, VX_READ_ONLY);
    if (status != VX_SUCCESS || filename_stride != sizeof(vx_char))
    {
        vxCommitArrayRange(file, 0, 0, filename);
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Incorrect array "VX_FMT_REF"\n", file);
        return VX_FAILURE;
    }
    //VX_PRINT(VX_ZONE_INFO, "filename=%s\n",filename);
    fp = fopen(filename, "wb+");
    if (fp == NULL) {
        vxCommitArrayRange(file, 0, 0, filename);
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Failed to open file %s\n",filename);
        return VX_FAILURE;
    }

    status |= vxQueryImage(input, VX_IMAGE_WIDTH,  &width,  sizeof(width));
    status |= vxQueryImage(input, VX_IMAGE_HEIGHT, &height, sizeof(height));
    status |= vxQueryImage(input, VX_IMAGE_PLANES, &planes, sizeof(planes));
    status |= vxQueryImage(input, VX_IMAGE_FORMAT, &format, sizeof(format));

    status |= vxGetValidRegionImage(input, &rect);

    sx = rect.start_x;
    sy = rect.start_y;
    ex = rect.end_x;
    ey = rect.end_y;

    ext = strrchr(filename, '.');
    if (ext && (strcmp(ext, ".pgm") == 0 || strcmp(ext, ".PGM") == 0))
    {
        fprintf(fp, "P5\n# %s\n",filename);
        fprintf(fp, "%u %u\n", width, height);
        if (format == VX_DF_IMAGE_U8)
            fprintf(fp, "255\n");
        else if (format == VX_DF_IMAGE_S16)
            fprintf(fp, "65535\n");
        else if (format == VX_DF_IMAGE_U16)
            fprintf(fp, "65535\n");
    }
    for (p = 0u; p < planes; p++)
    {
        status |= vxAccessImagePatch(input, &rect, p, &addr[p], (void **)&src[p], VX_READ_ONLY);
    }
    for (p = 0u; (p < planes) && (status == VX_SUCCESS); p++)
    {
        size_t len = addr[p].stride_x * (addr[p].dim_x * addr[p].scale_x)/VX_SCALE_UNITY;
        for (y = 0u; y < height; y+=addr[p].step_y)
        {
            vx_uint32 i = 0;
            vx_uint8 *ptr = NULL;
            uint8_t value = 0u;

            if (y < sy || y >= ey)
            {
                for (i = 0; i < width; ++i) {
                    wrote += fwrite(&value, sizeof(value), 1, fp);
                }
                continue;
            }

            for (i = 0; i < sx; ++i)
                wrote += fwrite(&value, sizeof(value), 1, fp);

            ptr = vxFormatImagePatchAddress2d(src[p], 0, y - sy, &addr[p]);
            wrote += fwrite(ptr, 1, len, fp);

            for (i = 0; i < width - ex; ++i)
                wrote += fwrite(&value, sizeof(value), 1, fp);
        }
        if (wrote == 0)
        {
            vxAddLogEntry((vx_reference)file, VX_FAILURE, "Failed to write to file!\n");
            status = VX_FAILURE;
            break;
        }
        if (status == VX_FAILURE)
        {
            vxAddLogEntry((vx_reference)file, VX_FAILURE, "Failed to write image to file correctly\n");
            break;
        }
    }
    for (p = 0u; p < planes; p++)
    {
        status |= vxCommitImagePatch(input, NULL, p, &addr[p], src[p]);
    }
    if (status != VX_SUCCESS)
    {
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Failed to write image to file correctly\n");
    }
    fflush(fp);
    fclose(fp);
    if (vxCommitArrayRange(file, 0, 0, filename) != VX_SUCCESS)
    {
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Failed to release handle to filename array!\n");
    }

    return status;
}
Example #8
0
vx_status vxFReadImage(vx_array file, vx_image output)
{
    vx_char *filename = NULL;
    vx_size filename_stride = 0;
    vx_uint8 *src = NULL;
    vx_uint32 p = 0u, y = 0u;
    vx_size planes = 0u;
    vx_imagepatch_addressing_t addr = {0};
    vx_df_image format = VX_DF_IMAGE_VIRT;
    FILE *fp = NULL;
    vx_char tmp[VX_MAX_FILE_NAME] = {0};
    vx_char *ext = NULL;
    vx_rectangle_t rect;
    vx_uint32 width = 0, height = 0;

    vx_status status = vxAccessArrayRange(file, 0, VX_MAX_FILE_NAME, &filename_stride, (void **)&filename, VX_READ_ONLY);
    if (status != VX_SUCCESS || filename_stride != sizeof(vx_char))
    {
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Incorrect array "VX_FMT_REF"\n", file);
        return VX_FAILURE;
    }
    fp = fopen(filename, "rb");
    if (fp == NULL) {
        vxAddLogEntry((vx_reference)file, VX_FAILURE, "Failed to open file %s\n",filename);
        return VX_FAILURE;
    }

    vxQueryImage(output, VX_IMAGE_PLANES, &planes, sizeof(planes));
    vxQueryImage(output, VX_IMAGE_FORMAT, &format, sizeof(format));

    ext = strrchr(filename, '.');
    if (ext && (strcmp(ext, ".pgm") == 0 || strcmp(ext, ".PGM") == 0))
    {
        FGETS(tmp, fp); // PX
        FGETS(tmp, fp); // comment
        FGETS(tmp, fp); // W H
        sscanf(tmp, "%u %u", &width, &height);
        FGETS(tmp, fp); // BPP
        // ! \todo double check image size?
    }
    else if (ext && (strcmp(ext, ".yuv") == 0 ||
                     strcmp(ext, ".rgb") == 0 ||
                     strcmp(ext, ".bw") == 0))
    {
        sscanf(filename, "%*[^_]_%ux%u_%*s", &width, &height);
    }

    rect.start_x = rect.start_y = 0;
    rect.end_x = width;
    rect.end_y = height;

    for (p = 0; p < planes; p++)
    {
        status = vxAccessImagePatch(output, &rect, p, &addr, (void **)&src, VX_WRITE_ONLY);
        if (status == VX_SUCCESS)
        {
            for (y = 0; y < addr.dim_y; y+=addr.step_y)
            {
                vx_uint8 *srcp = vxFormatImagePatchAddress2d(src, 0, y, &addr);
                vx_size len = ((addr.dim_x * addr.scale_x)/VX_SCALE_UNITY);
                vx_size rlen = fread(srcp, addr.stride_x, len, fp);
                if (rlen != len)
                {
                    status = VX_FAILURE;
                    break;
                }
            }
            if (status == VX_SUCCESS)
            {
                status = vxCommitImagePatch(output, &rect, p, &addr, src);
            }
            if (status != VX_SUCCESS)
            {
                break;
            }
        }
        /* src pointer should be made NULL , otherwise the first plane data gets over written. */
        src = NULL;
    }
    fclose(fp);
    vxCommitArrayRange(file, 0, 0, filename);

    return status;
}