Ejemplo n.º 1
0
    void setLayerSizes( InputArray _layer_sizes )
    {
        clear();

        _layer_sizes.copyTo(layer_sizes);
        int l_count = layer_count();

        weights.resize(l_count + 2);
        max_lsize = 0;

        if( l_count > 0 )
        {
            for( int i = 0; i < l_count; i++ )
            {
                int n = layer_sizes[i];
                if( n < 1 + (0 < i && i < l_count-1))
                    CV_Error( CV_StsOutOfRange,
                             "there should be at least one input and one output "
                             "and every hidden layer must have more than 1 neuron" );
                max_lsize = std::max( max_lsize, n );
                if( i > 0 )
                    weights[i].create(layer_sizes[i-1]+1, n, CV_64F);
            }

            int ninputs = layer_sizes.front();
            int noutputs = layer_sizes.back();
            weights[0].create(1, ninputs*2, CV_64F);
            weights[l_count].create(1, noutputs*2, CV_64F);
            weights[l_count+1].create(1, noutputs*2, CV_64F);
        }
    }
Ejemplo n.º 2
0
void drawKeypoints( InputArray image, const std::vector<KeyPoint>& keypoints, InputOutputArray outImage,
                    const Scalar& _color, DrawMatchesFlags flags )
{
    CV_INSTRUMENT_REGION();

    if( !(flags & DrawMatchesFlags::DRAW_OVER_OUTIMG) )
    {
        if (image.type() == CV_8UC3 || image.type() == CV_8UC4)
        {
            image.copyTo(outImage);
        }
        else if( image.type() == CV_8UC1 )
        {
            cvtColor( image, outImage, COLOR_GRAY2BGR );
        }
        else
        {
            CV_Error( Error::StsBadArg, "Incorrect type of input image: " + typeToString(image.type()) );
        }
    }

    RNG& rng=theRNG();
    bool isRandColor = _color == Scalar::all(-1);

    CV_Assert( !outImage.empty() );
    std::vector<KeyPoint>::const_iterator it = keypoints.begin(),
                                     end = keypoints.end();
    for( ; it != end; ++it )
    {
        Scalar color = isRandColor ? Scalar( rng(256), rng(256), rng(256), 255 ) : _color;
        _drawKeypoint( outImage, *it, color, flags );
    }
}
Ejemplo n.º 3
0
void cv::superres::arrCopy(InputArray src, OutputArray dst)
{
    if (dst.isUMat() || src.isUMat())
    {
        src.copyTo(dst);
        return;
    }

    typedef void (*func_t)(InputArray src, OutputArray dst);
    static const func_t funcs[10][10] =
    {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
        { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
        { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
        { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
        { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
        { 0, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, mat2mat, arr2buf, 0, mat2gpu },
        { 0, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, buf2arr, 0, buf2arr },
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
        { 0, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, gpu2mat, arr2buf, 0 , gpu2gpu },
    };

    const int src_kind = src.kind() >> _InputArray::KIND_SHIFT;
    const int dst_kind = dst.kind() >> _InputArray::KIND_SHIFT;

    CV_Assert( src_kind >= 0 && src_kind < 10 );
    CV_Assert( dst_kind >= 0 && dst_kind < 10 );

    const func_t func = funcs[src_kind][dst_kind];
    CV_Assert( func != 0 );

    func(src, dst);
}
Ejemplo n.º 4
0
Archivo: ippe.cpp Proyecto: lz89/IPPE
void IPPE::PoseSolver::solveSquare(float squareLength, InputArray _imagePoints, InputArray _cameraMatrix, InputArray _distCoeffs,
                                   OutputArray _rvec1, OutputArray _tvec1, float& err1, OutputArray _rvec2, OutputArray _tvec2, float& err2)
{

    //allocate outputs:
    _rvec1.create(3, 1, CV_64FC1);
    _tvec1.create(3, 1, CV_64FC1);
    _rvec2.create(3, 1, CV_64FC1);
    _tvec2.create(3, 1, CV_64FC1);

    cv::Mat normalizedInputPoints; //undistored version of imagePoints
    cv::Mat objectPoints2D;

    //generate the object points:
    generateSquareObjectCorners2D(squareLength, objectPoints2D);


    cv::Mat H; //homography from canonical object points to normalized pixels


    if (_cameraMatrix.empty()) {
        //this means imagePoints are defined in normalized pixel coordinates, so just copy it:
        _imagePoints.copyTo(normalizedInputPoints);
    }
    else {
        //undistort the image points (i.e. put them in normalized pixel coordinates).
        cv::undistortPoints(_imagePoints, normalizedInputPoints, _cameraMatrix, _distCoeffs);
    }

    //compute H
    homographyFromSquarePoints(normalizedInputPoints, squareLength / 2.0f, H);

    //now solve
    cv::Mat Ma, Mb;
    solveCanonicalForm(objectPoints2D, normalizedInputPoints, H, Ma, Mb);

    //sort poses according to reprojection error:
    cv::Mat M1, M2;
    cv::Mat objectPoints3D;
    generateSquareObjectCorners3D(squareLength, objectPoints3D);
    sortPosesByReprojError(objectPoints3D, _imagePoints, _cameraMatrix, _distCoeffs, Ma, Mb, M1, M2, err1, err2);

    //fill outputs
    rot2vec(M1.colRange(0, 3).rowRange(0, 3), _rvec1);
    rot2vec(M2.colRange(0, 3).rowRange(0, 3), _rvec2);

    M1.colRange(3, 4).rowRange(0, 3).copyTo(_tvec1);
    M2.colRange(3, 4).rowRange(0, 3).copyTo(_tvec2);
}
Ejemplo n.º 5
0
static void _prepareImage(InputArray src, const Mat& dst)
{
    CV_CheckType(src.type(), src.type() == CV_8UC1 || src.type() == CV_8UC3 || src.type() == CV_8UC4, "Unsupported source image");
    CV_CheckType(dst.type(), dst.type() == CV_8UC3 || dst.type() == CV_8UC4, "Unsupported destination image");
    const int src_cn = src.channels();
    const int dst_cn = dst.channels();

    if (src_cn == dst_cn)
        src.copyTo(dst);
    else if (src_cn == 1)
        cvtColor(src, dst, dst_cn == 3 ? COLOR_GRAY2BGR : COLOR_GRAY2BGRA);
    else if (src_cn == 3 && dst_cn == 4)
        cvtColor(src, dst, COLOR_BGR2BGRA);
    else if (src_cn == 4 && dst_cn == 3)
        cvtColor(src, dst, COLOR_BGRA2BGR);
    else
        CV_Error(Error::StsInternal, "");
}
Ejemplo n.º 6
0
static bool ocl_flip(InputArray _src, OutputArray _dst, int flipCode )
{
    int type = _src.type(), cn = CV_MAT_CN(type);

    if (cn > 4 || cn == 3)
        return false;

    const char * kernelName;
    int flipType;

    if (flipCode == 0)
        kernelName = "arithm_flip_rows", flipType = FLIP_ROWS;
    else if (flipCode > 0)
        kernelName = "arithm_flip_cols", flipType = FLIP_COLS;
    else
        kernelName = "arithm_flip_rows_cols", flipType = FLIP_BOTH;

    Size size = _src.size();
    int cols = size.width, rows = size.height;
    if ((cols == 1 && flipType == FLIP_COLS) ||
            (rows == 1 && flipType == FLIP_ROWS) ||
            (rows == 1 && cols == 1 && flipType == FLIP_BOTH))
    {
        _src.copyTo(_dst);
        return true;
    }

    ocl::Kernel k(kernelName, ocl::core::flip_oclsrc,
        format( "-D type=%s", ocl::memopTypeToStr(type)));
    if (k.empty())
        return false;

    _dst.create(size, type);
    UMat src = _src.getUMat(), dst = _dst.getUMat();

    cols = flipType == FLIP_COLS ? ((cols+1)/2) : cols;
    rows = flipType & FLIP_ROWS ? ((rows+1)/2) : rows;

    size_t globalsize[2] = { cols, rows };
    return k.args(ocl::KernelArg::ReadOnlyNoSize(src), ocl::KernelArg::WriteOnly(dst), rows, cols).run(2, globalsize, NULL, false);
}
Textons::Textons(int DictionarySize, InputArray input_image):
    k(DictionarySize) {
    input_image.copyTo(test_image);
}
void BagOfWordsSlic::GenerateSuperpixels(Mat _edges, InputArray _input_image, OutputArray _superpixels,
        int _number_of_superpixels, InputArray _visual_word_map,
        InputArray _mask, OutputArray _superpixel_centroids) {

    cout << "generating super pixels" << endl;
    /*--Convert from input arguments to class data-structures--*/
    input_image_ = _input_image.getMat();
    Mat visual_word_map = _visual_word_map.getMat();
    Mat mask = _mask.getMat();

    cvtColor(mask, mask, CV_BGR2GRAY);
    im_height_ = input_image_.rows;
    im_width_ = input_image_.cols;
    num_superpixels_ = _number_of_superpixels;

    // Compute step size
    S_ = sqrt(im_height_*im_width_/num_superpixels_);

    /*--Get Lab Image--*/
    Mat lab_image(input_image_.size(), CV_8UC3);
    cvtColor(input_image_, lab_image, COLOR_BGR2Lab);

    /*--Initialize centroid locations to regular grid with computed step size--*/
    int grid_width = ceil((float)im_width_/S_);
    int grid_height = ceil((float)im_height_/S_);
    num_superpixels_  = grid_width*grid_height;
    image_oversegmentation_ = new Oversegmentation(input_image_);
    cluster_centroids_.resize(num_superpixels_);

    int index_cluster = 0;

    for(int i = 0; i < cluster_centroids_.size(); ++i) {
        int offset = 0;
        if (((i-1)/grid_width)%2 == 0)
            offset = S_/4;
        else
            offset = 3*S_/4;
        int x = offset + ((i-1)%grid_width)*S_;
        int y = offset + ((i-1)/grid_width)*S_;
        int s_ = ceil((float)S_/2);

        if ((int)(mask.at<uchar>(y,x)) == 1
                && mask.at<uchar>(min(y+s_, im_height_),min(x+s_, im_width_)) == 1
                && mask.at<uchar>(max(y-s_, 0), max(x-s_,0)) ==1
                && mask.at<uchar>(max(y-s_, 0), min(x+s_, im_width_))==1
                && mask.at<uchar>(min(y+s_, im_height_), max(x-s_, 0))==1) {
//            cluster_centroids_[i].pt_.x = offset + ((i-1)%grid_width)*S_;
//            cluster_centroids_[i].pt_.y = offset + ((i-1)/grid_width)*S_;
            cluster_centroids_[i].pt_.x = x;
            cluster_centroids_[i].pt_.y = y;
        }
        index_cluster++;
    }

    // Find local minimum of gradient magnitude and adjust centroid locations
    MoveCentroidsToLocalGradientMinima();

    for(int i = 0; i < cluster_centroids_.size(); ++i)
        image_oversegmentation_->AddNewSegmentAt(cluster_centroids_[i].pt_);

    if (visual_word_map.empty()) {
        visual_word_histogram_matrix_.create(input_image_.size(),CV_64FC(KCENTERS));
    } else {
        //Compute visual_word histograms
        ComputeVisualWordHistograms(5,5,visual_word_map);
    }

    /*---Generate descriptors at centroid locations---*/

    // Get L,A,B vector for each centroid
    for(int i = 0; i < cluster_centroids_.size(); ++i) {
        cluster_centroids_[i].lab_color_  = lab_image.at<Vec3b>(image_oversegmentation_->SegmentCentroid(i));
        cluster_centroids_[i].visual_word_histogram_ = visual_word_histogram_matrix_.at<Vec50d>(image_oversegmentation_->SegmentCentroid(i));
    }

    /*--Initialize distance to nearest centroid for each pixel--*/
    distance_matrix_.create(input_image_.size(), CV_64F);
    double dist;
    int* superpixel_label_matrix_row;
    Vec50d* visual_word_histogram_row;
    double* distance_matrix_row;
    Vec3b* lab_image_row;

    /*---First loop starts: for iter = 1 to kMaxIter---*/
    for(int iter = 0; iter < kMaxIter; ++iter) {

        /* Reset distances */
        distance_matrix_.setTo(DBL_MAX);

        cout << "EM iteration: " << iter << "\n";
        cout << "Number of segments now:   " << image_oversegmentation_->NumberOfSegments() << "\n";

        int x_lower_limit, x_upper_limit, y_lower_limit, y_upper_limit;
        /*---Second loop starts: for cInd = 1 to num_superpixels_---*/
        for(int i = 0; i < num_superpixels_; ++i) {
            int centroid_x = cluster_centroids_[i].pt_.x, centroid_y = cluster_centroids_[i].pt_.y;
            /*---Third loop starts: Iterate through each pixel in 2S+1 x 2S+1 window size around centroid[i]---*/
            y_lower_limit = max(centroid_y - S_,0);
            y_upper_limit = min(centroid_y + S_,im_height_);
            x_lower_limit = max(centroid_x - S_,0);
            x_upper_limit = min(centroid_x + S_,im_width_);

            for(int pixel_y = y_lower_limit; pixel_y < y_upper_limit; pixel_y++) {

                lab_image_row = lab_image.ptr<Vec3b>(pixel_y);
                distance_matrix_row = distance_matrix_.ptr<double>(pixel_y);
                visual_word_histogram_row = visual_word_histogram_matrix_.ptr<Vec50d>(pixel_y);
                superpixel_label_matrix_row = image_oversegmentation_->pixel_labels_.ptr<int>(pixel_y);

                int temp_x = x_lower_limit;
                for(int pixel_x = x_lower_limit; pixel_x < x_upper_limit; ++pixel_x) {

                    if (mask.at<uchar>(pixel_y, pixel_x) != 1)
                        continue;
                    //Compute the pixel's distance to centroid[i]
                    ClusterPoint pixel(Point2f(pixel_x,pixel_y), lab_image_row[pixel_x], visual_word_histogram_row[pixel_x]);
                    if (visual_word_map.empty()) {
                        dist = cluster_centroids_[i].distance_to(pixel, m_, S_, 0);
                    } else {
                        dist = cluster_centroids_[i].distance_to(pixel, m_, S_, kHistogramDistanceWeight);
                    }
                    /*---Update the superpixel[pixel] and distance[pixel] if required---*/
                    if(dist < distance_matrix_row[pixel_x]) {
                        distance_matrix_row[pixel_x] = dist;
                        superpixel_label_matrix_row[pixel_x] = i;
                    }
                }
            }
            /*---Third loop ends---*/
        }/*---Second loop ends---*/
        image_oversegmentation_->ComputeSegmentAreas();

        //Create vector of flags to indicate discardedsuperpixel_labels
        vector<bool> discard_list(num_superpixels_,false);

        /*---Fourth loop: iterate through each centroid(superpixel) and count number of pixels within.
        If count is too small, mark superpixel for discarding---*/
        for(int i = 0; i < num_superpixels_; ++i) {
            if (discard_list[i] != 1) {
                discard_list[i] = image_oversegmentation_->SegmentArea(i) < kMinSuperpixelAreaThreshold;

            }
        }

        int num_discarded = 0;
        for(int i = 0; i < discard_list.size(); ++i)
            if(discard_list[i])
                ++num_discarded;

        image_oversegmentation_->DeleteSegments(discard_list);
        num_superpixels_ = image_oversegmentation_->NumberOfSegments();

        vector<Point> old_centroids = image_oversegmentation_->GetCentroids();
        UpdateClusterCentroids(lab_image);
        vector<Point> new_centroids = image_oversegmentation_->GetCentroids();

        /*---Check for convergence - if converged, then break from loop---*/
        int max_centroid_displacement = -1;
        for(int i = 0; i < num_superpixels_ ; ++i) {
            int x_difference = abs(old_centroids[i].x-new_centroids[i].x);
            int y_difference = abs(old_centroids[i].y-new_centroids[i].y);
            max_centroid_displacement = std::max(max_centroid_displacement,x_difference);
            max_centroid_displacement = std::max(max_centroid_displacement,y_difference);
        }

        cout << "max distance:  " << max_centroid_displacement << "\n";
        if (max_centroid_displacement <= kCentroidErrorThreshold) {
            RenumberEachConnectedComponent();
            RelabelSmallSegmentsToNearestNeighbor(kMinSuperpixelAreaThreshold);
            cout << "Number of segments now:   " << image_oversegmentation_->NumberOfSegments() << "\n";

            break;
        }

        /*---First loop ends---*/
    }
    image_oversegmentation_->pixel_labels_.copyTo(_superpixels);

    vector<Point> centroids = image_oversegmentation_->GetCentroids();
    _superpixel_centroids.create(centroids.size(), 2, CV_32S);
    Mat superpixel_centroids = _superpixel_centroids.getMat();

    for (int i = 0; i < centroids.size(); ++i) {
        superpixel_centroids.at<int>(i,0) = centroids[i].x;
        superpixel_centroids.at<int>(i,1) = centroids[i].y;
    }
    _input_image.copyTo(image_oversegmentation_->_original_image);
    visual_word_map.copyTo(image_oversegmentation_->Texton_image);
    _edges.copyTo(image_oversegmentation_->_edges);
    image_oversegmentation_->ListPixelsForEachSegment();
//    Mat src = _input_image.getMat();
    //cout << "where2" << endl;
//    image_oversegmentation_->ComputeSegmentFeatures(src, visual_word_map, _edges);
    image_oversegmentation_->ShowClassifiedLabelImage(mask);

    cout << "where3" << endl;
//    cout << "total num superpixels: " << centroids.size() << endl;
//    _number_of_superpixels_total = centroids.size();
    return;
    /*---Clean up image_oversegmentation_->pixel_labels_---*/

}