/// Replace the OpenCV imread function with this one using Vision Workbench.
/// - This is done to avoid having to build the OpenCV highgui library on a Mac.
cv::Mat vw_imread(const std::string fileName,
         			    vw::ImageView<vw::uint8> &gray_buffer) {

  vw::read_image(gray_buffer, fileName);

  // Figure out the image buffer parameters
  void*   raw_data_ptr = reinterpret_cast<void*>(gray_buffer.data());
  size_t  pixel_size   = sizeof(vw::uint8);
  size_t  step_size    = gray_buffer.cols() * pixel_size;

  // Create an OpenCV wrapper for the buffer image
  cv::Mat cv_image(gray_buffer.rows(), gray_buffer.cols(), CV_8UC1, raw_data_ptr, step_size);
  return cv_image;
}
Exemple #2
0
void conrec(vw::ImageView<float>& dem, PointContourSet& cset,
                    int cint, float nodataval,
                    std::list<ContourSegment>& seglist) {
    int m1,m2,m3,case_value;
    double zmin,zmax;
    register int c,i,j,m;
    double h[5];
    int sh[5];
    double xh[5], yh[5];
    int im[4] = {0,1,1,0}, jm[4] = {0,0,1,1};
    int castab[3][3][3] =
        { { {0,0,8},{0,2,5},{7,6,9} },
          { {0,3,4},{1,3,1},{4,3.0} },
          { {9,6,7},{5,2,0},{8,0,0} } };
    ContourSegment seg;

    vw::vw_out(vw::InfoMessage, "console") << "Running CONREC\n";
    vw::vw_out(vw::DebugMessage, "console") << "\tFinding contours\n";
    for (i=0; i < dem.cols()-1; i++) {
        for (j=0; j < dem.rows()-1; j++) {
            zmin = min_nodata( min_nodata(dem(i,j),   dem(i,j+1),   nodataval),
                               min_nodata(dem(i+1,j), dem(i+1,j+1), nodataval),
                               nodataval);
            if (zmin == nodataval) continue;

            zmax = max_nodata( max_nodata(dem(i,j),   dem(i,j+1),   nodataval),
                               max_nodata(dem(i+1,j), dem(i+1,j+1), nodataval),
                               nodataval);

            int cmin = ceil(zmin / cint) * cint;
            int cmax = floor(zmax / cint) * cint;
            for (c = cmin; c <= cmax; c += cint) {
                //printf("(%d,%d) c: %d\n",i,j,c);
                int goodvals = 0;
                h[0] = 0;
                for (m = 4; m >= 0; m--) {
                    if (m > 0) {
                        if (dem(i+im[m-1], j+jm[m-1]) == nodataval)
                            h[m] = nodataval;
                        else {
                            h[m] = dem(i+im[m-1], j+jm[m-1]) - c;
                            h[0] += h[m];
                            goodvals++;
                        }
                        xh[m] = i + im[m-1];
                        yh[m] = j + jm[m-1];
                        //printf("h[%d]: %0.2f (%0.2f)\n",m,dem(i+im[m-1],j+jm[m-1]),h[m]);
                    } else {
                        h[0] /= goodvals;
                        xh[0] = i + 0.5;
                        yh[0] = j + 0.5;
                        //printf("h[%d]: ... (%0.2f)\n",m,h[m]);
                    }


                    if (h[m] > 0.0)
                        sh[m] = 1;
                    else if (h[m] < 0.0)
                        sh[m] = -1;
                    else
                        sh[m] = 0;
                }
                //=================================================================
                //
                // Note: at this stage the relative heights of the corners and the
                // centre are in the h array, and the corresponding coordinates are
                // in the xh and yh arrays. The centre of the box is indexed by 0
                // and the 4 corners by 1 to 4 as shown below.
                // Each triangle is then indexed by the parameter m, and the 3
                // vertices of each triangle are indexed by parameters m1,m2,and
                // m3.
                // It is assumed that the centre of the box is always vertex 2
                // though this isimportant only when all 3 vertices lie exactly on
                // the same contour level, in which case only the side of the box
                // is drawn.
                //
                //
                //      vertex 4 +-------------------+ vertex 3
                //               | \               / |
                //               |   \    m-3    /   |
                //               |     \       /     |
                //               |       \   /       |
                //               |  m=4    X   m=2   |       the centre is vertex 0
                //               |       /   \       |
                //               |     /       \     |
                //               |   /    m=1    \   |
                //               | /               \ |
                //      vertex 1 +-------------------+ vertex 2
                //
                //
                //
                //               Scan each triangle in the box
                //
                //=================================================================
                for (m=1;m<=4;m++) {
                    m1 = m;
                    m2 = 0;
                    m3 = (m==4) ? 1 : m+1;

                    if (h[m1] == nodataval || h[m3] == nodataval)
                        continue;

                    case_value = castab[sh[m1]+1][sh[m2]+1][sh[m3]+1];
                    if (case_value!=0) {
                        ContourSegment seg;
                        seg.level = c;
                        switch (case_value) {
                            //===========================================================
                            //     Case 1 - Line between vertices 1 and 2
                            //===========================================================
                            case 1:
                                seg.a = ContourPoint(xh[m1], yh[m2]);
                                seg.b = ContourPoint(xh[m2], yh[m2]);
                                break;
                            //===========================================================
                            //     Case 2 - Line between vertices 2 and 3
                            //===========================================================
                            case 2:
                                seg.a = ContourPoint(xh[m2], yh[m2]);
                                seg.b = ContourPoint(xh[m3], yh[m3]);
                                break;
                            //===========================================================
                            //     Case 3 - Line between vertices 3 and 1
                            //===========================================================
                            case 3:
                                seg.a = ContourPoint(xh[m3], yh[m3]);
                                seg.b = ContourPoint(xh[m1], yh[m1]);
                                break;
                            //===========================================================
                            //     Case 4 - Line between vertex 1 and side 2-3
                            //===========================================================
                            case 4:
                                seg.a = ContourPoint(xh[m1], yh[m1]);
                                seg.b = ContourPoint(xsect(m2,m3), ysect(m2,m3));
                                break;
                            //===========================================================
                            //     Case 5 - Line between vertex 2 and side 3-1
                            //===========================================================
                            case 5:
                                seg.a = ContourPoint(xh[m2], yh[m2]);
                                seg.b = ContourPoint(xsect(m3,m1), ysect(m3,m1));
                                break;
                            //===========================================================
                            //     Case 6 - Line between vertex 3 and side 1-2
                            //===========================================================
                            case 6:
                                seg.a = ContourPoint(xh[m3], yh[m3]);
                                seg.b = ContourPoint(xsect(m1,m2), ysect(m1,m2));
                                break;
                            //===========================================================
                            //     Case 7 - Line between sides 1-2 and 2-3
                            //===========================================================
                            case 7:
                                seg.a = ContourPoint(xsect(m1,m2), ysect(m1,m2));
                                seg.b = ContourPoint(xsect(m2,m3), ysect(m2,m3));
                                break;
                            //===========================================================
                            //     Case 8 - Line between sides 2-3 and 3-1
                            //===========================================================
                            case 8:
                                seg.a = ContourPoint(xsect(m2,m3), ysect(m2,m3));
                                seg.b = ContourPoint(xsect(m3,m1), ysect(m3,m1));
                                break;
                            //===========================================================
                            //     Case 9 - Line between sides 3-1 and 1-2
                            //===========================================================
                            case 9:
                                seg.a = ContourPoint(xsect(m3,m1), ysect(m3,m1));
                                seg.b = ContourPoint(xsect(m1,m2), ysect(m1,m2));
                                break;
                            default:
                                break;
                        }
                        seglist.push_back(seg);
                    }
                }
            }
        }
    }
    vw::vw_out(vw::DebugMessage, "console")
        << "\tCONREC found " << seglist.size() << " segments" << std::endl;
}