首页 > 解决方案 > 如何写入在全局和 pthread 函数之外定义的向量

问题描述

在这段代码中,当我使用coutthen 输出时,它没有显示任何错误,但是当我写xinqueuedensity()函数的值时,它会引发分段错误错误。有谁知道我应该怎么做才能在我的函数中推送x对应于iand的值?step_iqueuedensity()

#include <opencv2/opencv.hpp>
#include <iostream>
#include <chrono>
#include <pthread.h>
// #include<mutex>
using namespace cv;
using namespace std;
using namespace chrono;

pthread_mutex_t lock;
// mutex mm;
string video;
int threads;
int core = -1;
vector<Mat> frames;
Mat backgroundImage;

vector<vector<double>> density(718, vector<double>(threads, 0));

vector<Mat> Mdivide(Mat img)
{
    vector<Mat> blocks;
    int sizeX = img.size[0];
    int sizeY = img.size[1];
    for (int i = 0; i < threads; i++)
    {
        Mat temp = img(Rect(0.0, double(778 * i / threads), 328.0, 778 / threads * 1.0));
        blocks.push_back(temp);
    }
    return blocks;
}
        
Mat croppedframe(Mat frames)
{
    Mat ret;

    Mat im_src = frames;
    Mat gray_img;
    cvtColor(im_src, gray_img, CV_BGR2GRAY);
    Size size;
    size = im_src.size();
    Mat im_dst = Mat::zeros(size, CV_8UC3);

    // Create a vector of destination points.
    vector<Point2f> final_pts;

    final_pts.push_back(Point2f(472, 52));
    final_pts.push_back(Point2f(472, 830));
    final_pts.push_back(Point2f(800, 830));
    final_pts.push_back(Point2f(800, 52));

    vector<Point2f> initial_pts;

    initial_pts.push_back(Point2f(966, 210));
    initial_pts.push_back(Point2f(446, 714));
    initial_pts.push_back(Point2f(1448, 714));
    initial_pts.push_back(Point2f(1288, 207));

    Mat h1 = findHomography(initial_pts, final_pts);
    warpPerspective(gray_img, im_dst, h1, size);
    Mat cropedImage = im_dst(Rect(472, 52, 328, 778));
    ret = cropedImage;
    return ret;
}

void print(vector<Mat> a)
{
    for (int i = 0; i < a.size(); i++)
    {
        imshow("frame", a[i]);

        waitKey(0);
    }
}

void *queuedensity(void *p)
{
    Ptr<BackgroundSubtractor> bg_model;
    bg_model = createBackgroundSubtractorMOG2();
    ofstream fout;
    fout.open("queue_method3.txt");
    for (int i = 0; i < frames.size(); i++)
    {
        Mat img1 = croppedframe(frames[i]);
        pthread_mutex_lock(&lock);
        core = (core + 1);
        core = core % threads;
        int step_i = core;
        vector<Mat> blocks;
        blocks = Mdivide(img1);
        pthread_mutex_unlock(&lock);

        Mat img = blocks[step_i];
        Mat foregroundMask;
        foregroundMask.create(backgroundImage.size(), backgroundImage.type());

        // compute foreground mask 8 bit image
        bg_model->apply(img, foregroundMask, 0);
        // smooth the mask to reduce noise in image
        GaussianBlur(foregroundMask, foregroundMask, Size(11, 11), 3.5, 3.5);
        // threshold mask to saturate at black and white values
        threshold(foregroundMask, foregroundMask, 10, 255, THRESH_BINARY);

        int totalpixels = foregroundMask.rows * foregroundMask.cols;
        int white = countNonZero(foregroundMask);
        double x = double(white) / totalpixels;

        // std::lock_guard<std::mutex> guard(mm);
        pthread_mutex_lock(&lock);
        cout << i << " " << step_i << " " << x << endl;
        // density[i][step_i] = x;
        pthread_mutex_unlock(&lock);

        // }

        // fout << i << " " << x << endl;
    }
}

int main(int argc, char const *argv[])
{
    video = argv[1];
    threads = stoi(argv[2]);
    VideoCapture cap(video);

    if (cap.isOpened() == false)
    {
        cout << "Cannot open the video file" << endl;
        cin.get(); //wait for any key press
        return -1;
    }

    auto start = high_resolution_clock::now();
    while (true)
    {
        Mat frame;
        bool bSuccess = cap.read(frame); // read a new frame from video

        //Breaking the while loop at the end of the video
        if (bSuccess == false)
        {
            cout << "Found the end of the video" << endl;
            break;
        }
        else
        {
            int index = cap.get(CV_CAP_PROP_POS_FRAMES);

            if (index % 8 == 0)
            {
                frames.push_back(frame);
            }
        }
    }

    //uncomment if you want to print all the selected frames
    //  print(cropped_frames);

    // background subtraction and queue density calculation start

    backgroundImage = imread("back.jpg");
    resize(backgroundImage, backgroundImage, Size(328.0, double(778 / threads)), 0, 0, INTER_CUBIC);

    pthread_t thread[threads];
    for (int i = 0; i < threads; i++) //thread creation
    {
        int *p;
        pthread_create(&thread[i], NULL, queuedensity, (void *)(p));
    }

    for (int i = 0; i < threads; i++) //thread joining
    {
        pthread_join(thread[i], NULL);
    }
    //queue density finish

    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<microseconds>(stop - start) / pow(10, 6);
    cout << "Time taken : " << duration.count() << " seconds" << endl;

    return 0;
}

当我使用cout.

标签: c++multithreadingpthreadsmutex

解决方案


推荐阅读