//--------------------------------------------------------- bool CViGrA_Morphology::On_Execute(void) { bool bRescale; int Type, Radius; double Rank; CSG_Grid *pInput, *pOutput, Rescaled; pInput = Parameters("INPUT") ->asGrid(); pOutput = Parameters("OUTPUT") ->asGrid(); Type = Parameters("TYPE") ->asInt(); Radius = Parameters("RADIUS") ->asInt(); Rank = Parameters("RANK") ->asDouble(); bRescale = Parameters("RESCALE") ->asBool(); //----------------------------------------------------- if( bRescale ) { Rescaled.Create(*Get_System(), SG_DATATYPE_Byte); for(sLong i=0; i<Get_NCells() && Set_Progress_NCells(i); i++) { Rescaled.Set_Value(i, 0.5 + (pInput->asDouble(i) - pInput->Get_ZMin()) * 255.0 / pInput->Get_ZRange()); } pInput = &Rescaled; } //----------------------------------------------------- vigra::BImage Input, Output(Get_NX(), Get_NY()); Copy_Grid_SAGA_to_VIGRA(*pInput, Input, true); switch( Type ) { case 0: // Dilation discDilation (srcImageRange(Input), destImage(Output), Radius); break; case 1: // Erosion discErosion (srcImageRange(Input), destImage(Output), Radius); break; case 2: // Median discMedian (srcImageRange(Input), destImage(Output), Radius); break; case 3: // User defined rank discRankOrderFilter (srcImageRange(Input), destImage(Output), Radius, Rank); break; } //----------------------------------------------------- Copy_Grid_VIGRA_to_SAGA(*pOutput, Output, false); pOutput->Set_Name(CSG_String::Format(SG_T("%s [%s]"), pInput->Get_Name(), Get_Name().c_str())); return( true ); }
void NeuronsImageWriter::write() { // make sure we have a recent id map updateInputs(); unsigned int numSections = _idMap->size(); if (_annotation) { _directory += std::string("_") + boost::lexical_cast<std::string>(*_annotation); } // prepare the output directory boost::filesystem::path directory(_directory); if (!boost::filesystem::exists(directory)) { boost::filesystem::create_directory(directory); } else if (!boost::filesystem::is_directory(directory)) { BOOST_THROW_EXCEPTION(IOError() << error_message(std::string("\"") + _directory + "\" is not a directory") << STACK_TRACE); } // save output images for (unsigned int i = 0; i < numSections; i++) { std::stringstream filename; filename << _directory << "/" << _basename << std::setw(4) << std::setfill('0') << i << ".tiff"; vigra::exportImage(srcImageRange(*(*_idMap)[i]), vigra::ImageExportInfo(filename.str().c_str())); } }
void EdgeHistogram::InitializeImage(const vigra::FVector3Image& image) { // Consider grayscale image vigra::FImage image_gray(image.width(), image.height()); vigra::transformImage(srcImageRange(image), destImage(image_gray), VectorMeanTransformAccessor<vigra::FVector3Image::Accessor>()); // 1. Compute canny edge image vigra::BImage canny(image_gray.width(), image_gray.height()); canny = 0; vigra::cannyEdgeImage(srcImageRange(image_gray), destImage(canny), 1, 15, 255); // scale, threshold, edgevalue //vigra::exportImage(srcImageRange(canny), "canny.png"); // 2. Get gradient magnitude and orientation of original image vigra::FVector2Image gradient(canny.width(), canny.height()); vigra::gradientBasedTransform(srcImageRange(image_gray), destImage(gradient), MagnitudeOrientationGradientFunctor<float>(undirected_edges)); // 3. Produce matrices: histogram bin and gradient magnitude for each pixel bin_image.resize(gradient.height(), gradient.width()); gmm::fill(bin_image, 0); bin_value.resize(gradient.height(), gradient.width()); gmm::fill(bin_value, 0); for (int y = 0; y < gradient.height(); ++y) { for (int x = 0; x < gradient.width(); ++x) { // No edge -> skip if (canny(x, y) == 0) continue; // Edge: calculate bin index and gradient magnitude double magnitude = gradient(x, y)[0]; double orientation = gradient(x, y)[1]; // range 0 to 1. assert(orientation >= 0.0 && orientation <= 1.0); if (orientation >= 1.0) orientation = 0.0; orientation *= static_cast<double>(bin_count); int orientation_index = static_cast<int>(orientation) % bin_count; // Bin value of zero denotes no edge. bin_image(y, x) = orientation_index + 1; bin_value(y, x) = magnitude; } } }
void PreviewColorPickerTool::CalcCorrectionForImage(unsigned int i,vigra::Point2D pos) { const HuginBase::SrcPanoImage & img = helper->GetPanoramaPtr()->getImage(i); HuginBase::ImageCache::ImageCacheRGB8Ptr cacheImage8 = HuginBase::ImageCache::getInstance().getImage(img.getFilename())->get8BitImage(); //copy only region to be inspected vigra::BRGBImage tempImage(2*ColorPickerSize,2*ColorPickerSize); vigra::copyImage(vigra::make_triple((*cacheImage8).upperLeft() + pos + vigra::Point2D(-ColorPickerSize,-ColorPickerSize), (*cacheImage8).upperLeft() + pos + vigra::Point2D( ColorPickerSize, ColorPickerSize), vigra::BRGBImage::Accessor()), destImage(tempImage) ); //now apply photometric corrections HuginBase::Photometric::InvResponseTransform<vigra::UInt8, double> invResponse(img); if (helper->GetPanoramaPtr()->getOptions().outputMode == HuginBase::PanoramaOptions::OUTPUT_LDR) { // select exposure and response curve for LDR output std::vector<double> outLut; vigra_ext::EMoR::createEMoRLUT(helper->GetPanoramaPtr()->getImage(0).getEMoRParams(), outLut); vigra_ext::enforceMonotonicity(outLut); invResponse.setOutput(1.0/pow(2.0,helper->GetPanoramaPtr()->getOptions().outputExposureValue), outLut, 255.0); } else { invResponse.setHDROutput(true,1.0/pow(2.0,helper->GetPanoramaPtr()->getOptions().outputExposureValue)); } vigra::DRGBImage floatTemp(tempImage.size()); vigra_ext::transformImageSpatial(srcImageRange(tempImage), destImage(floatTemp), invResponse, vigra::Diff2D(pos.x-ColorPickerSize,pos.y-ColorPickerSize)); //calculate average vigra::FindAverage<vigra::DRGBImage::PixelType> average; vigra::inspectImage(srcImageRange(floatTemp), average); //range check vigra::RGBValue<double> RGBaverage=average.average(); if(RGBaverage[0]>2 && RGBaverage[0]<253 && RGBaverage[1]>2 && RGBaverage[1]<253 && RGBaverage[2]>2 && RGBaverage[2]<253) { m_red+=RGBaverage[0]/RGBaverage[1]; m_blue+=RGBaverage[2]/RGBaverage[1]; m_count++; }; };
void CViGrA_Watershed::Segmentation(TImage_In &Input, TImage_Out &Output, double Scale, bool bEdges) { typedef typename vigra::NumericTraits<typename TImage_In::value_type>::RealPromote TmpType; vigra::BasicImage<TmpType> gradientx (Get_NX(), Get_NY()); vigra::BasicImage<TmpType> gradienty (Get_NX(), Get_NY()); vigra::FImage gradientmag (Get_NX(), Get_NY()); vigra::IImage labels (Get_NX(), Get_NY()); //----------------------------------------------------- // calculate the x- and y-components of the image gradient at given scale Process_Set_Text(_TL("calculate gradients")); recursiveFirstDerivativeX (srcImageRange(Input) , destImage(gradientx), Scale); recursiveSmoothY (srcImageRange(gradientx) , destImage(gradientx), Scale); recursiveFirstDerivativeY (srcImageRange(Input) , destImage(gradienty), Scale); recursiveSmoothX (srcImageRange(gradienty) , destImage(gradienty), Scale); //----------------------------------------------------- // transform components into gradient magnitude Process_Set_Text(_TL("calculate gradient magnitude")); combineTwoImages( srcImageRange(gradientx), srcImage(gradienty), destImage(gradientmag), GradientSquaredMagnitudeFunctor() ); //----------------------------------------------------- // find the local minima of the gradient magnitude (might be larger than one pixel) Process_Set_Text(_TL("find local minima")); labels = 0; extendedLocalMinima(srcImageRange(gradientmag), destImage(labels), 1); //----------------------------------------------------- // label the minima just found Process_Set_Text(_TL("label minima")); int max_region_label = labelImageWithBackground(srcImageRange(labels), destImage(labels), false, 0); //----------------------------------------------------- // create a statistics functor for region growing vigra::ArrayOfRegionStatistics<vigra::SeedRgDirectValueFunctor<float> >gradstat(max_region_label); //----------------------------------------------------- // perform region growing, starting from the minima of the gradient magnitude; // as the feature (first input) image contains the gradient magnitude, // this calculates the catchment basin of each minimum Process_Set_Text(_TL("perform region growing")); seededRegionGrowing(srcImageRange(gradientmag), srcImage(labels), destImage(labels), gradstat); //----------------------------------------------------- // initialize a functor to determine the average gray-value or color for each region (catchment basin) just found vigra::ArrayOfRegionStatistics<vigra::FindAverage<TmpType> >averages(max_region_label); //----------------------------------------------------- // calculate the averages Process_Set_Text(_TL("calculate averages")); inspectTwoImages(srcImageRange(Input), srcImage(labels), averages); //----------------------------------------------------- // write the averages into the destination image (the functor 'averages' acts as a look-up table) transformImage(srcImageRange(labels), destImage(Output), averages); //----------------------------------------------------- // mark the watersheds (region boundaries) black if( bEdges ) { regionImageToEdgeImage(srcImageRange(labels), destImage(Output), vigra::NumericTraits<typename TImage_Out::value_type>::zero()); } }
// MAIN int main(int argc, char* argv[]) { try { DataParams params(argc, argv); params.doSanityChecks(); int stacksize = params.shape(2); Size2D size2 (params.shape(0), params.shape(1)); // isnt' there a slicing operator? if(params.getVerbose()) { std::cout << "Images with Shape: " << params.shape() << std::endl; std::cout << "Processing a stack of " << stacksize << " images..." << std::endl; } // found spots. One Vector over all images in stack // the inner set contains all spots in the image std::vector<std::set<Coord<float> > > res_coords(stacksize); DImage res((size2-Diff2D(1,1))*params.getFactor()+Diff2D(1,1)); // check if outfile is writable, otherwise throw error -> exit exportImage(srcImageRange(res), ImageExportInfo(params.getOutFile().c_str())); std::ofstream cf; if(!params.getCoordsFile().empty()) { cf.open(params.getCoordsFile()); vigra_precondition(cf.is_open(), "Could not open coordinate-file for writing."); } USE_NESTED_TICTOC; //USETICTOC; TICPUSH; // measure the time // STORM Algorithmut CliProgressFunctor func; wienerStorm(params, res_coords, func); // resulting image drawCoordsToImage<Coord<float> >(res_coords, res); int numSpots = 0; if(cf.is_open()) { numSpots = saveCoordsFile(params, cf, res_coords); cf.close(); } // end: done. std::cout << std::endl << TOCS << std::endl; std::cout << "detected " << numSpots << " spots." << std::endl; // some maxima are very strong so we scale the image as appropriate : double maxlim = 0., minlim = 0; findMinMaxPercentile(res, 0., minlim, 0.996, maxlim); std::cout << "cropping output values to range [" << minlim << ", " << maxlim << "]" << std::endl; if(maxlim > minlim) { transformImage(srcImageRange(res), destImage(res), ifThenElse(Arg1()>Param(maxlim), Param(maxlim), Arg1())); } exportImage(srcImageRange(res), ImageExportInfo(params.getOutFile().c_str())); if (!params.getSettingsFile().empty()) params.save(); } catch (vigra::StdException & e) { std::cout<<"There was an error:"<<std::endl; std::cout << e.what() << std::endl; return 1; } return 0; }