void HistogramFeatureExtractor::getFeatures(const BranchSegment& branch, std::vector<double>& features) { std::vector<double> sourceHistogram = computeHistogram(*branch.getSourceSlice().get()); std::vector<double> targetHistogram1 = computeHistogram(*branch.getTargetSlice1().get()); std::vector<double> targetHistogram2 = computeHistogram(*branch.getTargetSlice2().get()); std::vector<double> targetHistogram = targetHistogram1; for (unsigned int i = 0; i < _numBins; i++) targetHistogram[i] += targetHistogram2[i]; for (unsigned int i = 0; i < _numBins; i++) features[2*_numBins + i] = std::abs(sourceHistogram[i] - targetHistogram[i]); double sourceSum = 0; double targetSum = 0; for (unsigned int i = 0; i < _numBins; i++) { sourceSum += sourceHistogram[i]; targetSum += targetHistogram[i]; } for (unsigned int i = 0; i < _numBins; i++) features[2*_numBins + _numBins + i] = std::abs(sourceHistogram[i]/sourceSum - targetHistogram[i]/targetSum); }
void pcl::visualization::PCLPlotter::addHistogramData (std::vector<double> const& data, int const nbins, char const * name, std::vector<char> const &color) { std::vector<std::pair<double, double> > histogram; computeHistogram (data, nbins, histogram); this->addPlotData (histogram, name, vtkChart::BAR, color); }
void HistogramFeatureExtractor::getFeatures(const ContinuationSegment& continuation, std::vector<double>& features) { std::vector<double> sourceHistogram = computeHistogram(*continuation.getSourceSlice().get()); std::vector<double> targetHistogram = computeHistogram(*continuation.getTargetSlice().get()); for (unsigned int i = 0; i < _numBins; i++) features[2*_numBins + i] = std::abs(sourceHistogram[i] - targetHistogram[i]); double sourceSum = 0; double targetSum = 0; for (unsigned int i = 0; i < _numBins; i++) { sourceSum += sourceHistogram[i]; targetSum += targetHistogram[i]; } for (unsigned int i = 0; i < _numBins; i++) features[2*_numBins + _numBins + i] = std::abs(sourceHistogram[i]/sourceSum - targetHistogram[i]/targetSum); }
void Background::computeBasicModel() { // std::cout << "Computing basic background model.." << std::endl; assert(video.isOpened()); cv::Mat frame, gray; // read frame by frame and process while(video.read(frame)) { // convert to grayscale and apply Gaussian blur cv::cvtColor(frame, gray, CV_RGB2GRAY); grayscaleGaussianBlur(gray, gray, 5); for (int row = 0; row < gray.rows; ++row) { #pragma omp parallel for for (int col = 0; col < gray.cols; ++col) { pixels[row * width + col].push_back((int)gray.at<uchar>(row, col)); } } frames.push_back(gray.clone()); } // compute medians and get background model for (int row = 0; row < backgroundModel.rows; ++row) { #pragma omp parallel for for (int col = 0; col < backgroundModel.cols; ++col) { backgroundModel.at<uchar>(row, col) = getMean(pixels[row * width + col]); } } cv::Mat hist; cv::Mat centralBgModel = cv::Mat(backgroundModel, cv::Range(0, backgroundModel.rows*(1.0 - MARGIN_BOTTOM)), cv::Range(backgroundModel.cols * MARGIN_SIDE, backgroundModel.cols*(1.0 - MARGIN_SIDE))); bgModelHist = computeHistogram(centralBgModel); drawHistogram(bgModelHist, hist); // cv::namedWindow( "Background", CV_WINDOW_AUTOSIZE ); // cv::imshow( "Background", backgroundModel ); // cv::namedWindow( "Histogram", CV_WINDOW_AUTOSIZE ); // cv::imshow( "Histogram", hist ); adjustBackgroundModel(); drawHistogram(bgModelHist, hist); // cv::namedWindow( "Adjusted background", CV_WINDOW_AUTOSIZE ); // cv::imshow( "Adjusted background", backgroundModel ); // cv::namedWindow( "Adjusted histogram", CV_WINDOW_AUTOSIZE ); // cv::imshow( "Adjusted histogram", hist ); // cv::waitKey(0); // exit(0); }
void ColorHistogramMatcher::feature( const sensor_msgs::PointCloud2::ConstPtr& input_cloud, const jsk_pcl_ros::ClusterPointIndices::ConstPtr& input_indices) { boost::mutex::scoped_lock(mutex_); if (!reference_set_) { NODELET_WARN("reference histogram is not available yet"); return; } pcl::PointCloud<pcl::PointXYZRGB>::Ptr pcl_cloud (new pcl::PointCloud<pcl::PointXYZRGB>); pcl::fromROSMsg(*input_cloud, *pcl_cloud); pcl::PointCloud<pcl::PointXYZHSV>::Ptr hsv_cloud (new pcl::PointCloud<pcl::PointXYZHSV>); pcl::PointCloudXYZRGBtoXYZHSV(*pcl_cloud, *hsv_cloud); // compute histograms first std::vector<std::vector<float> > histograms; histograms.resize(input_indices->cluster_indices.size()); pcl::ExtractIndices<pcl::PointXYZHSV> extract; extract.setInputCloud(hsv_cloud); // for debug jsk_pcl_ros::ColorHistogramArray histogram_array; histogram_array.header = input_cloud->header; for (size_t i = 0; i < input_indices->cluster_indices.size(); i++) { pcl::IndicesPtr indices (new std::vector<int>(input_indices->cluster_indices[i].indices)); extract.setIndices(indices); pcl::PointCloud<pcl::PointXYZHSV> segmented_cloud; extract.filter(segmented_cloud); std::vector<float> histogram; computeHistogram(segmented_cloud, histogram, policy_); histograms[i] = histogram; ColorHistogram ros_histogram; ros_histogram.header = input_cloud->header; ros_histogram.histogram = histogram; histogram_array.histograms.push_back(ros_histogram); } all_histogram_pub_.publish(histogram_array); // compare histograms jsk_pcl_ros::ClusterPointIndices result; result.header = input_indices->header; for (size_t i = 0; i < input_indices->cluster_indices.size(); i++) { const double coefficient = bhattacharyyaCoefficient(histograms[i], reference_histogram_); NODELET_DEBUG_STREAM("coefficient: " << i << "::" << coefficient); if (coefficient > coefficient_thr_) { result.cluster_indices.push_back(input_indices->cluster_indices[i]); } } result_pub_.publish(result); }
void HistogramFeatureExtractor::getFeatures(const EndSegment& end, std::vector<double>& features) { std::vector<double> histogram = computeHistogram(*end.getSlice().get()); for (unsigned int i = 0; i < _numBins; i++) features[i] = histogram[i]; double sum = 0; for (unsigned int i = 0; i < _numBins; i++) sum += histogram[i]; for (unsigned int i = 0; i < _numBins; i++) features[_numBins + i] = histogram[i]/sum; }
static void computeColorMapFromInput(FILE * const ifP, bool const allColors, int const reqColors, enum methodForLargest const methodForLargest, enum methodForRep const methodForRep, int * const formatP, struct pam * const freqPamP, tupletable2 * const colormapP) { /*---------------------------------------------------------------------------- Produce a colormap containing the best colors to represent the image stream in file 'ifP'. Figure it out using the median cut technique. The colormap will have 'reqcolors' or fewer colors in it, unless 'allcolors' is true, in which case it will have all the colors that are in the input. The colormap has the same maxval as the input. Put the colormap in newly allocated storage as a tupletable2 and return its address as *colormapP. Return the number of colors in it as *colorsP and its maxval as *colormapMaxvalP. Return the characteristics of the input file as *formatP and *freqPamP. (This information is not really relevant to our colormap mission; just a fringe benefit). -----------------------------------------------------------------------------*/ tupletable2 colorfreqtable; computeHistogram(ifP, formatP, freqPamP, &colorfreqtable); if (allColors) { *colormapP = colorfreqtable; } else { if (colorfreqtable.size <= reqColors) { pm_message("Image already has few enough colors (<=%d). " "Keeping same colors.", reqColors); *colormapP = colorfreqtable; } else { pm_message("choosing %d colors...", reqColors); mediancut(colorfreqtable, freqPamP->depth, reqColors, methodForLargest, methodForRep, colormapP); pnm_freetupletable2(freqPamP, colorfreqtable); } } }
void HistogramCPU::quitAnyComputation() { if ( isRunning() ) { QMutexLocker l(&_imp->mustQuitMutex); _imp->mustQuit = true; ///post a fake request to wakeup the thread l.unlock(); computeHistogram(0, boost::shared_ptr<Natron::Image>(), RectI(), 0,0,0,0); l.relock(); while (_imp->mustQuit) { _imp->mustQuitCond.wait(&_imp->mustQuitMutex); } } }
void ColorHistogramMatcher::reference( const sensor_msgs::PointCloud2::ConstPtr& input_cloud) { boost::mutex::scoped_lock(mutex_); std::vector<float> hist; pcl::PointCloud<pcl::PointXYZRGB> pcl_cloud; pcl::fromROSMsg(*input_cloud, pcl_cloud); pcl::PointCloud<pcl::PointXYZHSV> hsv_cloud; pcl::PointCloudXYZRGBtoXYZHSV(pcl_cloud, hsv_cloud); computeHistogram(hsv_cloud, hist, policy_); reference_histogram_ = hist; NODELET_INFO("update reference"); reference_set_ = true; ColorHistogram ros_histogram; ros_histogram.header = input_cloud->header; ros_histogram.histogram = reference_histogram_; reference_histogram_pub_.publish(ros_histogram); }
uint8_t flyEqualiser::process(void) { uint8_t *src = _yuvBuffer->data; uint8_t *dst = _scratchImage->data; uint8_t *disp = _yuvBufferOut->data; for (int y = 0; y < _h; y++) { for (int x = 0; x < _w; x++) { *dst = scaler[*src]; dst++; src++; } } uint32_t half = _w >> 1; src = _yuvBuffer->data; dst = _scratchImage->data; for (int y = 0; y < _h; y++) { if (y > _h) { memcpy(disp, dst, half); memcpy(disp + half, src + half, half); } else { memcpy(disp, src, half); memcpy(disp + half, dst + half, half); } src += _w; dst += _w; disp += _w; } memcpy(_yuvBufferOut->data + _w * _h, _yuvBuffer->data + _w * _h, (_w * _h) >> 1); computeHistogram(); copyYuvFinalToRgb(); }
void ColorHistogramMatcher::computeHistogram( const pcl::PointCloud<pcl::PointXYZHSV>& cloud, std::vector<float>& output, const ComparePolicy policy) { if (policy == USE_HUE_AND_SATURATION) { std::vector<float> hue, saturation; computeHistogram(cloud, hue, USE_HUE); computeHistogram(cloud, saturation, USE_SATURATION); output.resize(hue.size() + saturation.size()); for (size_t i = 0; i < hue.size(); i++) { output[i] = hue[i]; } for (size_t j = hue.size(); j < hue.size() + saturation.size(); j++) { output[j] = saturation[j - hue.size()]; } } else { double val_max, val_min; if (policy == USE_HUE) { val_max = 360.0; val_min = 0.0; } else { val_max = 1.0; val_min = 0.0; } output.resize(bin_size_, 0); for (size_t i = 0; i < cloud.points.size(); i++) { pcl::PointXYZHSV output_point = cloud.points[i]; // ratil double val; if (policy == USE_HUE) { val = output_point.h; } else if (policy == USE_SATURATION) { val = output_point.s; } else if (policy == USE_VALUE) { val = output_point.v; } int index = int((val - val_min) / (val_max - val_min) * bin_size_); if (index >= bin_size_) { index = bin_size_ - 1; } output[index] += 1.0; } } // normalize double sum = 0; for (size_t i = 0; i < output.size(); i++) { sum += output[i]; } for (size_t i = 0; i < output.size(); i++) { if (sum != 0.0) { output[i] /= sum; } else { output[i] = 0.0; } } }
main(int argc, char *argv[]) { int ticksPerUs = calibrate(); int n = (1 << 20)-1; if (argc == 2) sscanf(argv[1],"%d",&n); computeHistogram(n,ticksPerUs); }
/******************************************************************************* ** Calculate the radial distribution function for the given selections of ** visible atoms. ** ** Inputs are: ** - visibleAtoms: indices of atoms that are to be used for the calculation ** - specie: array containing the species index for each atom ** - pos: array containing the positions of the atoms ** - specieID1: the species of the first selection of atoms ** - specieID2: the species of the second selection of atoms ** - cellDims: the size of the simulation cell ** - pbc: periodic boundary conditions ** - start: minimum separation to use when constructing the histogram ** - finish: maximum separation to use when constructing the histogram ** - interval: the interval between histogram bins ** - numBins: the number of histogram bins ** - rdf: the result is returned in this array *******************************************************************************/ static PyObject* calculateRDF(PyObject *self, PyObject *args) { int numVisible, *visibleAtoms, *specie, specieID1, specieID2, *pbc, numBins; int numAtoms; double *pos, *cellDims, start, finish, *rdf; PyArrayObject *visibleAtomsIn=NULL; PyArrayObject *specieIn=NULL; PyArrayObject *pbcIn=NULL; PyArrayObject *posIn=NULL; PyArrayObject *cellDimsIn=NULL; PyArrayObject *rdfIn=NULL; int i, status, *sel1, *sel2, sel1cnt, sel2cnt, duplicates; double interval; /* parse and check arguments from Python */ if (!PyArg_ParseTuple(args, "O!O!O!iiO!O!dddiO!", &PyArray_Type, &visibleAtomsIn, &PyArray_Type, &specieIn, &PyArray_Type, &posIn, &specieID1, &specieID2, &PyArray_Type, &cellDimsIn, &PyArray_Type, &pbcIn, &start, &finish, &interval, &numBins, &PyArray_Type, &rdfIn)) return NULL; if (not_intVector(visibleAtomsIn)) return NULL; visibleAtoms = pyvector_to_Cptr_int(visibleAtomsIn); numVisible = (int) PyArray_DIM(visibleAtomsIn, 0); if (not_intVector(specieIn)) return NULL; specie = pyvector_to_Cptr_int(specieIn); numAtoms = (int) PyArray_DIM(specieIn, 0); if (not_doubleVector(posIn)) return NULL; pos = pyvector_to_Cptr_double(posIn); if (not_doubleVector(rdfIn)) return NULL; rdf = pyvector_to_Cptr_double(rdfIn); if (not_doubleVector(cellDimsIn)) return NULL; cellDims = pyvector_to_Cptr_double(cellDimsIn); if (not_intVector(pbcIn)) return NULL; pbc = pyvector_to_Cptr_int(pbcIn); /* initialise result array to zero */ for (i = 0; i < numBins; i++) rdf[i] = 0.0; /* create the selections of atoms and check for number of duplicates */ sel1 = malloc(numVisible * sizeof(int)); if (sel1 == NULL) { PyErr_SetString(PyExc_MemoryError, "Could not allocate sel1"); return NULL; } sel2 = malloc(numVisible * sizeof(int)); if (sel2 == NULL) { PyErr_SetString(PyExc_MemoryError, "Could not allocate sel2"); free(sel1); return NULL; } sel1cnt = 0; sel2cnt = 0; duplicates = 0; for (i = 0; i < numVisible; i++) { int index = visibleAtoms[i]; /* check if this atom is in the first selection (negative means all species) */ if (specieID1 < 0 || specie[index] == specieID1) { sel1[i] = 1; sel1cnt++; } else sel1[i] = 0; /* check if this atom is in the second selection (negative means all species) */ if (specieID2 < 0 || specie[index] == specieID2) { sel2[i] = 1; sel2cnt++; } else sel2[i] = 0; /* count the number of atoms that are in both selections */ if (sel1[i] && sel2[i]) duplicates++; } /* compute the histogram for the RDF */ status = computeHistogram(numAtoms, numVisible, visibleAtoms, pos, pbc, cellDims, sel1, sel2, start, finish, interval, rdf); /* free memory used for selections */ free(sel1); free(sel2); /* return if there was an error */ if (status) return NULL; /* normalise the rdf */ normaliseRDF(numBins, sel1cnt, sel2cnt, duplicates, start, interval, cellDims, rdf); /* return None */ Py_INCREF(Py_None); return Py_None; }