nonMaximumSuppress.h

Go to the documentation of this file.
00001 
00015 #ifndef _DLRCOMPUTERVISION_NONMAXIMUMSUPPRESS_H_
00016 #define _DLRCOMPUTERVISION_NONMAXIMUMSUPPRESS_H_
00017 
00018 #include <dlrComputerVision/image.h>
00019 
00020 namespace dlr {
00021 
00022   namespace computerVision {
00023     
00024 
00029     template <ImageFormat FORMAT>
00030     Image<FORMAT>
00031     nonMaximumSuppress(const Image<FORMAT>& inputImage,
00032                        const Array2D<Float64>& gradX,
00033                        const Array2D<Float64>& gradY);
00034 
00035 
00036   } // namespace computerVision
00037 
00038 } // namespace dlr
00039 
00040 
00041 /* =============== Implementation follows =============== */
00042 
00043 
00044 namespace dlr {
00045 
00046   namespace computerVision {
00047 
00048     
00049     // This function zeros any pixels of the input image which are not
00050     // plausible edges.
00051     template <ImageFormat FORMAT>
00052     Image<FORMAT>
00053     nonMaximumSuppress(const Image<FORMAT>& inputImage,
00054                        const Array2D<Float64>& gradX,
00055                        const Array2D<Float64>& gradY)
00056     {
00057       // Argument checking.
00058       if(inputImage.rows() == 0 || inputImage.columns() == 0) {
00059         DLR_THROW(ValueException, "nonMaximumSuppress()",
00060                   "Argument inputImage must have non-zero size.");
00061       }
00062       if(inputImage.rows() != gradX.rows()
00063          || inputImage.columns() < gradX.columns()) {
00064         DLR_THROW(ValueException, "nonMaximumSuppress()",
00065                   "Arguments inputImage and gradX must have the same shape.");
00066       }
00067       if(inputImage.rows() != gradY.rows()
00068          || inputImage.columns() < gradY.columns()) {
00069         DLR_THROW(ValueException, "nonMaximumSuppress()",
00070                   "Arguments inputImage and gradY must have the same shape.");
00071       }
00072 
00073       // Create an output image.
00074       Image<FORMAT> suppressedImage(inputImage.rows(), inputImage.columns());
00075       suppressedImage = static_cast<typename Image<FORMAT>::PixelType>(0);
00076 
00077       // Test each pixel individually.
00078       size_t columns = inputImage.columns();
00079       size_t rowsMinusOne = inputImage.rows() - 1;
00080       size_t columnsMinusOne = inputImage.columns() - 1;
00081       for(size_t row = 1; row < rowsMinusOne; ++row) {
00082         size_t index0 = row * inputImage.columns() + 1;
00083         for(size_t column = 1; column < columnsMinusOne; ++column) {
00084           if(inputImage[index0]) {
00085             double gradXComponent = gradX[index0];
00086             double gradYComponent = gradY[index0];
00087             size_t neighbor0Index;
00088             size_t neighbor1Index;
00089             if(gradXComponent == 0.0) {
00090               neighbor0Index = index0 + columns;
00091               neighbor1Index = index0 - columns;
00092             } else if(std::fabs(gradXComponent) >= std::fabs(gradYComponent)) {
00093               double indicator = gradYComponent / gradXComponent;
00094               if(indicator >= 0.5) {
00095                 neighbor0Index = index0 + columns + 1;
00096                 neighbor1Index = index0 - columns - 1;
00097               } else if(indicator < -0.5) {
00098                 neighbor0Index = index0 + columns - 1;
00099                 neighbor1Index = index0 - columns + 1;
00100               } else {
00101                 neighbor0Index = index0 + 1;
00102                 neighbor1Index = index0 - 1;
00103               }
00104             } else { // fabs(gradYComponent) > fabs(gradXComponent)
00105               double indicator = gradXComponent / gradYComponent;
00106               if(indicator >= 0.5) {
00107                 neighbor0Index = index0 + columns + 1;
00108                 neighbor1Index = index0 - columns - 1;
00109               } else if(indicator < -0.5) {
00110                 neighbor0Index = index0 + columns - 1;
00111                 neighbor1Index = index0 - columns + 1;
00112               } else {
00113                 neighbor0Index = index0 + columns;
00114                 neighbor1Index = index0 - columns;
00115               }
00116             }
00117             if(inputImage[index0] > inputImage[neighbor0Index]
00118                && inputImage[index0] > inputImage[neighbor1Index]) {
00119               suppressedImage[index0] = inputImage[index0];
00120             }
00121           } // if(inputImage[index0] != 0.0)
00122           ++index0;
00123         } // for(size_t column...)
00124       } // for(size_t row...)
00125 
00126       return suppressedImage;
00127     } // nonMaximumSuppress()
00128 
00129     
00130   } // namespace computerVision
00131   
00132 } // namespace dlr
00133 
00134 #endif /* #ifndef _DLRCOMPUTERVISION_KERNEL_H_ */

Generated on Mon Jul 9 20:34:03 2007 for dlrLibs Utility Libraries by  doxygen 1.5.2