예제 #1
0
unsigned char calc_normalized_displacement(unsigned char *depth_map, int min_p, int image_w, int image_h, int p, int maximum_displacement) {

    int x = min_p % image_w; //x-coordinate of min_pos
    int y = min_p / image_w; //y-coordinate of min_pos
    int dx = x - (p % image_w); //dx of left and right
    int dy = y - (p / image_w); //dy of left and right
    return normalized_displacement(dx, dy, maximum_displacement);
}
예제 #2
0
void calc_depth(unsigned char *depth_map, unsigned char *left,
        unsigned char *right, int image_width, int image_height,
        int feature_width, int feature_height, int maximum_displacement) {

    int mapsize = image_width * image_height;
    int ftrboxw = (feature_width * 2) + 1;
    int ftrboxh = (feature_height * 2) + 1;
    int ftrboxsize = ftrboxw * ftrboxh;
    int srchboxwidth = feature_width + maximum_displacement;
    int srchboxheight = feature_height + maximum_displacement;
    int j = 0;

    if (maximum_displacement == 0){
        for (int z = 0; z < mapsize; z++){
            depth_map[z] = 0;
        }
    }else{   
        while (j < mapsize) {
        int x = j % image_width;
        int y = (j - (j % image_width))/image_width;

            if (feature_width > x || feature_width >= image_width - x ||
                feature_height > y || feature_height >= image_height - y) {
                depth_map[j] = 0;
            }else{
                int startx, starty, endx, endy;
                /* Establish search box boundaries */
                startx = x - srchboxwidth;
                starty = y - srchboxheight;
                endx = x + srchboxwidth;
                endy = y + srchboxheight; 
                if (startx < 0){
                    startx = 0;
                }
                if (starty < 0){
                    starty = 0;
                }
                if (endx >= image_width){
                    endx = image_width - 1;
                }
                if (endy >= image_height){
                    endy = image_height - 1;
                }

                /* Creates an array for left-image feature box centered around j */
                unsigned char leftftrbox[ftrboxsize];
                int lstart = ((y-feature_height)*image_width) + (x-feature_width);
                int count = 0;

                for (int s = lstart; s <= lstart + ((ftrboxh-1)*image_width); s=s+image_width){
                    for (int t = 0; t < ftrboxw; t++){
                        leftftrbox[t+count] = left[s+t];
                    }
                    count = count+ftrboxw;
                }
            
                int start = (starty * image_width) + startx;
                int iterendx = endx - (startx + (ftrboxw-1));
                int iterendy = endy - (starty + (ftrboxh-1));            
                int abs(int x);

                unsigned char normdis;
                unsigned char normdistemp;
                unsigned int tempedistnc;
                unsigned int edistance;
                int temp = 0;
                /* Iterate through all right-image feature box in search box */
                for (int k = start; k <= start + iterendx; k++){
                    for (int m = k; m <= k+(iterendy*image_width); m=m+image_width){
                        /* Creates an array for each right-image feature box centered around m */
                        unsigned char currftrbox[ftrboxsize];
                        int counter = 0;
                        for (int p = m; p <= m + ((ftrboxh-1)*image_width); p=p+image_width){
                            for (int n = 0; n < ftrboxw; n++) {
                                currftrbox[n+counter] = right[n+p];
                            }
                            counter = counter+ftrboxw;
                        }
                        int centerftrx = (m+feature_width)%image_width;
                        int centerftry = ((m+ (feature_height*image_width)) -((m+ (feature_height*image_width))%image_width)) / image_width;
                        int xloc = abs(centerftrx - x);
                        int yloc = abs(centerftry - y);
                        normdistemp = normalized_displacement(xloc, yloc, maximum_displacement);                    
                        if (temp == 0) {
                            normdis = normalized_displacement(xloc, yloc, maximum_displacement);
                            edistance = euclidean(leftftrbox, currftrbox, ftrboxsize);
                        }else{
                        
                            tempedistnc = euclidean(leftftrbox, currftrbox, ftrboxsize);
                            if (tempedistnc < edistance) {
                                edistance = tempedistnc;
                                normdis = normalized_displacement(xloc, yloc, maximum_displacement);                                        }
                            if (tempedistnc == edistance){
                                if (normdistemp < normdis){
                                    normdis = normdistemp;
                                }
                            }
                        }   
                        temp++;
                    }
                }   
                depth_map[j] = normdis;
            }
            j++;
        }
    }
}
예제 #3
0
void calc_depth(unsigned char *depth_map, unsigned char *left,
        unsigned char *right, int image_width, int image_height,
        int feature_width, int feature_height, int maximum_displacement) {

    /* YOUR CODE HERE */
    int i; // the index in the left image.
    for (i = 0; i < image_width * image_height; i++) {
      depth_map[i] = 0;
    }
    if (!maxd) {
      return;
    }

    int row, col; // the row and column number of the px in the left image.
    int j, k; // j, k are row/col position of the center of current search.
    int p, q; // relative location of pixel being compared.
    unsigned int sum = 0; // the sum of each iteration
    unsigned int currmin;
    int minj = 0, mink = 0;
    int tmpidx;
    int iidx;
    
    // start iteration for pixel in the left image.
    for (i = 0; i < image_width * image_height; i++) {
      row = get_row(i, image_width);
      col = get_col(i, image_width);
      if (row - feature_height < 0 ||
          row + feature_height >= image_height ||
          col - feature_width < 0 || col + feature_width >= image_width) {
        // if Pixels on the edge of the image, whose left-image features don't 
        // fit inside the image, should have a distance of 0 (infinity).
        depth_map[i] = 0;
      } else {
        // now we really start our search!
        currmin = 0xFFFFFFFF;
        minj = row;
        mink = col;
        for (j = row - maxd; j <= row + maxd; j++) {
          for (k = col - maxd; k <= col + maxd; k++) {
            // we ignore the case if a right feature is out.
            if (j - feature_height >= 0 && j + feature_height < image_height &&
                k - feature_width >= 0 && k + feature_width < image_width) {
              sum = 0;
              for (p = j - feature_height; p <= j + feature_height; p++) {
                for (q = k - feature_width; q <= k + feature_width; q++) {
                  tmpidx = idx(p, q, image_width);
                  iidx = idx(p + row - j, q + col - k, image_width);
                  sum += (left[iidx] - right[tmpidx]) *
                    (left[iidx] - right[tmpidx]);
                }
              }
              if (sum < currmin ||
                  (sum == currmin && pow((j-row), 2) + pow((k-col), 2) <
                   pow((minj-row), 2) + pow((mink-col), 2))) {
                currmin = sum;
                minj = j;
                mink = k;
              }
            }
          }
        }
      depth_map[i] = normalized_displacement(
          abs(mink-col), abs(minj-row), maxd);
      }
    }
}
예제 #4
0
파일: calc_depth.c 프로젝트: empbetty/cs61c
void calc_depth(unsigned char *depth_map, unsigned char *left,
                unsigned char *right, int image_width, int image_height,
                int feature_width, int feature_height, int maximum_displacement) {

    int x;
    int y;

    for (y = 0; y < image_height; y++)
    {
        for (x = 0; x < image_width; x++)
        {
            // test if on the edge or maximum_displacement = 0
            if (inside_the_graph(x, y, image_width, image_height, feature_width, feature_height) == 0
                    || maximum_displacement == 0)
            {
                depth_map[convert(x, y, image_width)] = 0;

            }

            else
            {

                int h;
                int w;
                Data good_point;
                good_point.x = 0;
                good_point.y = 0;
                good_point.sed = 0;
                int is_first = 1;
                Data this_point;

                for (h = -maximum_displacement; h <= maximum_displacement; h++)
                {
                    for (w = -maximum_displacement; w <= maximum_displacement; w++)
                    {
                        // right_X, right_y, center of the patch
                        int right_x = x + w;
                        int right_y = y + h;

                        // the patch must be valid
                        if (inside_the_graph(right_x, right_y, image_width, image_height, feature_width, feature_height) == 1)
                        {
                            this_point.x = right_x;
                            this_point.y = right_y;
                            this_point.sed = 0;

                            // calculate sed of the patch at right_x, right_y
                            int height;
                            int width;
                            for (height = -feature_height; height <= feature_height; height++)
                            {
                                for (width = -feature_width; width <= feature_width; width++)
                                {
                                    int left_temp_pos = convert(x + width, y + height, image_width);
                                    int right_temp_pos = convert(right_x + width, right_y + height, image_width);
                                    this_point.sed = this_point.sed + (left[left_temp_pos] - right[right_temp_pos]) * (left[left_temp_pos] - right[right_temp_pos]);
                                }
                            }

                            // update good_point.sed for the smallest sed
                            if (is_first == 1 || this_point.sed < good_point.sed)
                            {
                                good_point.x = this_point.x;
                                good_point.y = this_point.y;
                                good_point.sed = this_point.sed;
                                is_first = 0;
                            }

                            else if (this_point.sed == good_point.sed)
                            {
                                if ((normalized_displacement(this_point.x - x, this_point.y - y, maximum_displacement)
                                        - normalized_displacement(good_point.x - x, good_point.y - y, maximum_displacement)) < 0)
                                {
                                    good_point.x = this_point.x;
                                    good_point.y = this_point.y;
                                    good_point.sed = this_point.sed;

                                }
                            }

                        }

                    }
                }

                depth_map[convert(x, y, image_width)]=normalized_displacement(good_point.x - x, good_point.y - y, maximum_displacement);

            }
        }

    }

}