bool ImageEdgeDetectionBase<T, U>::followEdges(T* edgemapptr, T *edgemagptr, short lowval,
        int cols)
{
    if (!edgemapptr || !edgemagptr)
        return false;

    T *tempmagptr;
    T *tempmapptr;
    int i;
    //float thethresh;
    int x[8] = {1,1,0,-1,-1,-1,0,1},
               y[8] = {0,1,1,1,0,-1,-1,-1};

    for(i=0; i<8; i++) {
        tempmapptr = edgemapptr + y[i]*cols + x[i];
        tempmagptr = edgemagptr + y[i]*cols + x[i];

        if((*tempmapptr == kPgPOSSIBLE_EDGE) && (*tempmagptr > lowval)) {
            *tempmapptr = (T) kPgEDGE;
            followEdges(tempmapptr,tempmagptr, lowval, cols);
        }
    }

    return true;
}
Ejemplo n.º 2
0
SsaOpnd* OSR::followEdges(SsaOpnd* iv) {
    if (!LFTRHashMap.has(iv)) {
        return iv;
    } else {
        LFTREntry & tmp2 = LFTRHashMap[iv];
        SsaOpnd* reduced = tmp2.second;
        return followEdges(reduced);
    }
}
Ejemplo n.º 3
0
void OSR::performLFTR(Inst* inst) {
    Opcode opcode = inst->getOpcode();
    if (opcode == Op_Cmp || opcode == Op_Branch) {
        U_32 num = inst->getNumSrcOperands();
        if (2 == num) {
            iv = 0;
            rc = 0;
            recordIVRC(inst);
            if (iv && rc) {
                SsaOpnd* reduced = 0;
                SsaOpnd* newbound = 0;
                SsaOpnd* src = getLeadingOperand(iv);
                if (src == 0)
                    return;
                reduced = followEdges(iv);
                newbound = applyEdges(iv, rc);
                if ((reduced != src) && (newbound != rc)) {
                    reduced = makeTmp(reduced, iv->getInst());
                    newbound = makeTmp(newbound, rc->getInst());
                    inst->setType(reduced->getType()->tag);

                    if ((inst->getOpcode() == Op_Cmp
                            || inst->getOpcode() == Op_Branch)
                            && inst->getComparisonModifier() == Cmp_GT) {
                        inst->setSrc(1, reduced);
                        inst->setSrc(0, newbound);
                    } else if ((inst->getOpcode() == Op_Cmp
                                || inst->getOpcode() == Op_Branch)
                               && inst->getComparisonModifier() == Cmp_GTE) {
                        inst->setSrc(0, reduced);
                        inst->setSrc(1, newbound);

                    } else {
                        inst->setSrc(1, reduced);
                        inst->setSrc(0, newbound);
                    }
                }
            }
            iv = 0;
            rc = 0;
        }
    }
}
bool ImageEdgeDetectionBase<T, U>::applyHysteresis(T *mag, T *nms, int rows, int cols,
        float tlow, float thigh, U*edge)
{
    int r, c, pos, numedges, /*lowcount,*/ highcount, lowthreshold, highthreshold,
        //i,
        hist[32768];//, rr, cc;
    T maximum_mag;//, sumpix;

    /****************************************************************************
    * Initialize the edge map to possible edges everywhere the non-maximal
    * suppression suggested there could be an edge except for the border. At
    * the border we say there can not be an edge because it makes the
    * followEdges algorithm more efficient to not worry about tracking an
    * edge off the side of the image.
    ****************************************************************************/
    for(r=0,pos=0; r<rows; r++) {
        for(c=0; c<cols; c++,pos++) {
            if(nms[pos] == kPgPOSSIBLE_EDGE) edge[pos] = kPgPOSSIBLE_EDGE;
            else edge[pos] = kPgNOEDGE;
        }
    }

    for(r=0,pos=0; r<rows; r++,pos+=cols) {
        edge[pos] = kPgNOEDGE;
        edge[pos+cols-1] = kPgNOEDGE;
    }
    pos = (rows-1) * cols;
    for(c=0; c<cols; c++,pos++) {
        edge[c] = kPgNOEDGE;
        edge[pos] = kPgNOEDGE;
    }

    /****************************************************************************
    * Compute the histogram of the magnitude image. Then use the histogram to
    * compute hysteresis thresholds.
    ****************************************************************************/
    for(r=0; r<32768; r++) hist[r] = 0;
    for(r=0,pos=0; r<rows; r++) {
        for(c=0; c<cols; c++,pos++) {
            int magi = (int)(0.5f + mag[pos]);
            magi = min(max(magi, 0), 32767);
            if(edge[pos] == kPgPOSSIBLE_EDGE) hist[magi]++;
        }
    }

    /****************************************************************************
    * Compute the number of pixels that passed the nonmaximal suppression.
    ****************************************************************************/
    for(r=1,numedges=0; r<32768; r++) {
        if(hist[r] != 0) maximum_mag = r;
        numedges += hist[r];
    }

    highcount = (int)(numedges * thigh + 0.5);

    /****************************************************************************
    * Compute the high threshold value as the (100 * thigh) percentage point
    * in the magnitude of the gradient histogram of all the pixels that passes
    * non-maximal suppression. Then calculate the low threshold as a fraction
    * of the computed high threshold value. John Canny said in his paper
    * "A Computational Approach to Edge Detection" that "The ratio of the
    * high to low threshold in the implementation is in the range two or three
    * to one." That means that in terms of this implementation, we should
    * choose tlow ~= 0.5 or 0.33333.
    ****************************************************************************/
    r = 1;
    numedges = hist[1];
    while((r<(maximum_mag-1)) && (numedges < highcount)) {
        r++;
        numedges += hist[r];
    }
    highthreshold = r;
    lowthreshold = (int)(highthreshold * tlow + 0.5);

    if(0) {
        printf("The input low and high fractions of %f and %f computed to\n",
               tlow, thigh);
        printf("magnitude of the gradient threshold values of: %d %d\n",
               lowthreshold, highthreshold);
    }

    /****************************************************************************
    * This loop looks for pixels above the highthreshold to locate edges and
    * then calls followEdges to continue the edge.
    ****************************************************************************/
    for(r=0,pos=0; r<rows; r++) {
        for(c=0; c<cols; c++,pos++) {
            if((edge[pos] == kPgPOSSIBLE_EDGE) && (mag[pos] >= highthreshold)) {
                edge[pos] = kPgEDGE;
                followEdges((edge+pos), (mag+pos), lowthreshold, cols);
            }
        }
    }

    /****************************************************************************
    * Set all the remaining possible edges to non-edges.
    ****************************************************************************/
    for(r=0,pos=0; r<rows; r++) {
        for(c=0; c<cols; c++,pos++) if(edge[pos] != kPgEDGE) edge[pos] = kPgNOEDGE;
    }

    return true;
}