如何解决有关Opencv Mat的冲突错误?

时间:2019-05-14 14:51:33

标签: c++ multithreading opencv

我正在使用多个线程编写图像段程序。我创建一个vector<Mat> a来存储图像。`有时候,当我使用.push_back()函数时,发生了一个称为“访问冲突”的错误(我的VS2015是中文,所以我不知道此错误的英文翻译)。但是有时该程序有效,有时却无效。

可能是一个nullptr问题吗?还是allocate问题?

void ShuipingProjL(Mat srcImg, Mat srcImg2,vector<Mat>& roiList = vector<Mat>(),vector<Mat>& roiList2 = vector<Mat>())//水平投影
{
    Mat binImg;
    //threshold(srcImg, binImg, 0, 255, CV_THRESH_OTSU);
    int perPixelValue = 0;//每个像素的值
    int width = srcImg.cols;
    int height = srcImg.rows;
    int* projectValArry = new int[height];//创建一个储存每行黑色像素个数的数组
    memset(projectValArry, 0, height * 4);//初始化数组
    for (int col = 0; col < height; col++)//遍历每个像素点
    {
        for (int row = 0; row < width; row++)
        {
            perPixelValue = srcImg.at<uchar>(col, row);
            if (perPixelValue == 0)//如果是黑点
            {
                projectValArry[col]++;
            }
        }
    }
    Mat binImg2;
    //threshold(srcImg, binImg, 0, 255, CV_THRESH_OTSU);
    int perPixelValue2 = 0;//每个像素的值
    int width2 = srcImg2.cols;
    int height2 = srcImg2.rows;
    int* projectValArry2 = new int[height2];//创建一个储存每行黑色像素个数的数组
    memset(projectValArry2, 0, height2 * 4);//初始化数组
    for (int col = 0; col < height2; col++)//遍历每个像素点
    {
        for (int row = 0; row < width2; row++)
        {
            perPixelValue2 = srcImg2.at<uchar>(col, row);
            if (perPixelValue2 == 0)//如果是黑点
            {
                projectValArry2[col]++;
            }
        }
    }
    //vector<Mat> roiList;//用于储存分割出来的每个字符
    int startIndex = 0;//记录进入字符区的索引
    int endIndex = 0;//记录进入空白区域的索引
    bool inBlock = false;//是否遍历到了字符区内
    for (int i = 0; i <srcImg.rows; i++)
    {
        if (!inBlock && projectValArry[i] != 0)//进入字符区
        {
            inBlock = true;
            startIndex = i;
        }
        else if (inBlock && projectValArry[i] == 0)//进入空白区
        {
            endIndex = i;
            inBlock = false;
            Mat roiImg = srcImg(Range(startIndex, endIndex + 1), Range(0, srcImg.cols));//从原图中截取有图像的区域
            roiList.push_back(roiImg);
        }
    }
    int startIndex2 = 0;//记录进入字符区的索引
    int endIndex2 = 0;//记录进入空白区域的索引
    bool inBlock2 = false;//是否遍历到了字符区内
    for (int i = 0; i <srcImg2.rows; i++)
    {
        if (!inBlock2 && projectValArry2[i] != 0)//进入字符区
        {
            inBlock2 = true;
            startIndex2 = i;
        }
        else if (inBlock2 && projectValArry2[i] == 0)//进入空白区
        {
            endIndex2 = i;
            inBlock2 = false;
            Mat roiImg2 = srcImg2(Range(startIndex2, endIndex2 + 1), Range(0, srcImg2.cols));//从原图中截取有图像的区域
            roiList2.push_back(roiImg2);
        }
    }
    delete[] projectValArry;
    delete[] projectValArry2;
}
void ShuipingProj(Mat srcImg,vector<Mat>& roiList=vector<Mat>())//水平投影
{
    Mat binImg;
    //threshold(srcImg, binImg, 0, 255, CV_THRESH_OTSU);
    int perPixelValue = 0;//每个像素的值
    int width = srcImg.cols;
    int height = srcImg.rows;
    int* projectValArry = new int[height];//创建一个储存每行黑色像素个数的数组
    memset(projectValArry, 0, height * 4);//初始化数组
    for (int col = 0; col < height; col++)//遍历每个像素点
    {
        for (int row = 0; row < width; row++)
        {
            perPixelValue = srcImg.at<uchar>(col, row);
            if (perPixelValue == 0)//如果是黑点
            {
                projectValArry[col]++;
            }
        }
    }
    //vector<Mat> roiList;//用于储存分割出来的每个字符
    int startIndex = 0;//记录进入字符区的索引
    int endIndex = 0;//记录进入空白区域的索引
    bool inBlock = false;//是否遍历到了字符区内
    for (int i = 0; i <srcImg.rows; i++)
    {
        if (!inBlock && projectValArry[i] != 0)//进入字符区
        {
            inBlock = true;
            startIndex = i;
        }
        else if (inBlock && projectValArry[i] == 0)//进入空白区
        {
            endIndex = i;
            inBlock = false;
            Mat roiImg = srcImg(Range(startIndex, endIndex+1), Range(0, srcImg.cols));//从原图中截取有图像的区域
            roiList.push_back(roiImg);
        }
    }
    delete[] projectValArry;

}
int main(){
    vector<Mat> a2 = vector<Mat>();
    vector<Mat> b3 = vector<Mat>();
    threads4[0] = thread(ShuipingProjL, b[0], b[1], ref(a2), ref(b3));
    threads4[1] = thread(ShuipingProjL, b[2], b[3], ref(a2), ref(b3));
    threads4[2] = thread(ShuipingProj, b[4],ref(a2));
    threads4[3] = thread(ShuipingProj, b[5], ref(a2));
    threads4[0].join();
    threads4[1].join();
    threads4[2].join();
    threads4[3].join();
    waitKey(333333330);

    return 0;
}

ShuipingProj

ShuipingProj

ShuipingProjL的第一部分

first part of ShuipingProjL

ShuipingProjL的第二部分

second part of ShuipingProjL

ShuipingProjL的第三部分

third part of ShuipingProjL

0 个答案:

没有答案