Exemplo n.º 1
0
static unsigned char *read_image_uint8_rgb(char *fname, int *w, int *h)
{
	int pd;
	unsigned char *x = iio_read_image_uint8_vec(fname, w, h, &pd);
	if (pd == 3) return x;
	unsigned char *y = malloc(3**w**h);
	for (int i = 0; i < *w**h; i++) {
		switch(pd) {
		case 1:
			y[3*i+0] = y[3*i+1] = y[3*i+2] = x[i];
			break;
		case 2:
			y[3*i+0] = x[2*i+0];
			y[3*i+1] = y[3*i+2] = x[2*i+1];
			break;
		default:
			assert(pd > 3);
			for (int l = 0; l < 3; l++)
				y[3*i+l] = x[pd*i+l];
			break;
		}
	}
	free(x);
	return y;
}
Exemplo n.º 2
0
int main(int c, char *v[])
{
	int w, h, pd;
	uint8_t *x = iio_read_image_uint8_vec("-", &w, &h, &pd);
	if (pd != 3) return 1;
	idump(x, w, h);
	return 0;
}
Exemplo n.º 3
0
// read an image in any format from STDIN and write a ppm to STDOUT
int main(int c, char *v[])
{
	int w, h, pixeldim;
	uint8_t *x = iio_read_image_uint8_vec("-", &w, &h, &pixeldim);
	fprintf(stderr, "got a %dx%d image with %d channels\n", w, h, pixeldim);
	iio_save_image_uint8_vec("-", x, w, h, pixeldim);
	free(x);
	return 0;
}
Exemplo n.º 4
0
int main(int c, char *v[])
{
    if (c < 6 || c > 48) {
        help(v[0]);
        return 1;
    }

    // utm zone and hemisphere: true for 'N' and false for 'S'
    int zone;
    bool hem;
    const char *utm_string = pick_option(&c, &v, "-utm-zone", "no_utm_zone");
    parse_utm_string(&zone, &hem, utm_string);

    // ascii flag
    bool ascii = pick_option(&c, &v, "-ascii", NULL);

    // longitude-latitude bounding box
    double lon_m = atof(pick_option(&c, &v, "-lon-m", "-inf"));
    double lon_M = atof(pick_option(&c, &v, "-lon-M", "inf"));
    double lat_m = atof(pick_option(&c, &v, "-lat-m", "-inf"));
    double lat_M = atof(pick_option(&c, &v, "-lat-M", "inf"));

    // x-y bounding box
    double col_m = atof(pick_option(&c, &v, "-col-m", "-inf"));
    double col_M = atof(pick_option(&c, &v, "-col-M", "inf"));
    double row_m = atof(pick_option(&c, &v, "-row-m", "-inf"));
    double row_M = atof(pick_option(&c, &v, "-row-M", "inf"));

    // mask on the unrectified image grid
    const char *msk_orig_fname = pick_option(&c, &v, "-mask-orig", "");
    int msk_orig_w, msk_orig_h;
    float *msk_orig = iio_read_image_float(msk_orig_fname, &msk_orig_w,
                                           &msk_orig_h);

    // rectifying homography
    double href_inv[9], hsec_inv[9];
    int n_hom;
    const char *hom_string_ref = pick_option(&c, &v, "href", "");
    if (*hom_string_ref) {
        double *hom = alloc_parse_doubles(9, hom_string_ref, &n_hom);
        if (n_hom != 9)
            fail("can not read 3x3 matrix from \"%s\"", hom_string_ref);
        invert_homography(href_inv, hom);
    }
    const char *hom_string_sec = pick_option(&c, &v, "hsec", "");
    if (*hom_string_sec) {
        double *hom = alloc_parse_doubles(9, hom_string_sec, &n_hom);
        if (n_hom != 9)
            fail("can not read 3x3 matrix from \"%s\"", hom_string_sec);
        invert_homography(hsec_inv, hom);
    }

    // open disp and mask input images
    int w, h, nch, ww, hh, pd;
    float *dispy;
    float *dispx = iio_read_image_float_split(v[2], &w, &h, &nch);
    if (nch > 1) dispy = dispx + w*h;
    else dispy = calloc(w*h, sizeof(*dispy));

    float *mask = iio_read_image_float(v[3], &ww, &hh);
    if (w != ww || h != hh) fail("disp and mask image size mismatch\n");

    // open color images if provided
    uint8_t *clr = NULL;
    if (c > 6) {
        clr = iio_read_image_uint8_vec(v[6], &ww, &hh, &pd);
        if (w != ww || h != hh) fail("disp and color image size mismatch\n");
    }

    // read input rpc models
    struct rpc rpc_ref[1], rpc_sec[1];
    read_rpc_file_xml(rpc_ref, v[4]);
    read_rpc_file_xml(rpc_sec, v[5]);

    // outputs
    double p[2], q[2], X[3];

    // count number of valid pixels, and determine utm zone
    int npoints = 0;
    for (int row=0; row<h; row++)
    for (int col=0; col<w; col++) {
        int pix = row*w + col;
        if (!mask[pix]) continue;

        // compute coordinates of pix in the full reference image
        double a[2] = {col, row};
        apply_homography(p, href_inv, a);

        // check that it lies in the image domain bounding box
        if (round(p[0]) < col_m || round(p[0]) > col_M ||
            round(p[1]) < row_m || round(p[1]) > row_M)
            continue;

        // check that it passes the image domain mask
        int x = (int) round(p[0]) - col_m;
        int y = (int) round(p[1]) - row_m;
        if ((x < msk_orig_w) && (y < msk_orig_h))
            if (!msk_orig[y * msk_orig_w + x])
                continue;

        // compute (lon, lat, alt) of the 3D point
        float dx = dispx[pix];
        float dy = dispy[pix];
        double b[2] = {col + dx, row + dy};
        apply_homography(q, hsec_inv, b);
        intersect_rays(X, p, q, rpc_ref, rpc_sec);

        // check with lon/lat bounding box
        if (X[0] < lon_m || X[0] > lon_M || X[1] < lat_m || X[1] > lat_M)
            continue;

        // if it passed all these tests then it's a valid point
        npoints++;

        // if not defined, utm zone is that of the first point
        if (zone < 0)
            utm_zone(&zone, &hem, X[1], X[0]);
    }

    // print header for ply file
    FILE *ply_file = fopen(v[1], "w");
    write_ply_header(ply_file, ascii, npoints, zone, hem, (bool) clr, false);

    // loop over all the pixels of the input disp map
    // a 3D point is produced for each non-masked disparity
    for (int row=0; row<h; row++)
    for (int col=0; col<w; col++) {
        int pix = row*w + col;
        if (!mask[pix]) continue;

        // compute coordinates of pix in the full reference image
        double a[2] = {col, row};
        apply_homography(p, href_inv, a);

        // check that it lies in the image domain bounding box
        if (round(p[0]) < col_m || round(p[0]) > col_M ||
            round(p[1]) < row_m || round(p[1]) > row_M)
            continue;

        // check that it passes the image domain mask
        int x = (int) round(p[0]) - col_m;
        int y = (int) round(p[1]) - row_m;
        if ((x < msk_orig_w) && (y < msk_orig_h))
            if (!msk_orig[y * msk_orig_w + x])
                continue;

        // compute (lon, lat, alt) of the 3D point
        float dx = dispx[pix];
        float dy = dispy[pix];
        double b[2] = {col + dx, row + dy};
        apply_homography(q, hsec_inv, b);
        intersect_rays(X, p, q, rpc_ref, rpc_sec);

        // check with lon/lat bounding box
        if (X[0] < lon_m || X[0] > lon_M || X[1] < lat_m || X[1] > lat_M)
            continue;

        // convert (lon, lat, alt) to utm
        utm_alt_zone(X, X[1], X[0], zone);

        // colorization: if greyscale, copy the grey level on each channel
        uint8_t rgb[3];
        if (clr) {
            for (int k = 0; k < pd; k++) rgb[k] = clr[k + pd*pix];
            for (int k = pd; k < 3; k++) rgb[k] = rgb[k-1];
        }

        // write to ply
        if (ascii) {
            fprintf(ply_file, "%0.17g %0.17g %0.17g ", X[0], X[1], X[2]);
            if (clr)
                fprintf(ply_file, "%d %d %d", rgb[0], rgb[1], rgb[2]);
            fprintf(ply_file, "\n");
        } else {
            double XX[3] = {X[0], X[1], X[2]};
            fwrite(XX, sizeof(double), 3, ply_file);
            if (clr) {
                unsigned char C[3] = {rgb[0], rgb[1], rgb[2]};
                fwrite(rgb, sizeof(unsigned char), 3, ply_file);
            }
        }
    }

    fclose(ply_file);
    return 0;
}