使用 OpenCV 的内存错误

A Memory Error by Using OpenCV

照片在这里:

我想在单击鼠标左键和 [CTRL] 按钮时看到这个穿绿色衣服的白人。

代码查找点击点的8邻点并递归。 该代码在小矩阵中正常工作。但是在这张照片中,我遇到了内存错误。我想见见绿衣男子。如何正确修复代码?

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int n=0;
void yap(int i,int j,Mat* res){
ostringstream t;
Mat *rgb=(Mat*) res;
Mat res1;
res1=*rgb;
t<<"fark_binary"<<n<<".jpg";
    imwrite(t.str(),res1);
if(res1.at<Vec3b>(i,j)[0]>=200){
    cout<<j<<"\n"<<i;
    res1.at<Vec3b>(i,j)[0]=0;
    res1.at<Vec3b>(i,j)[1]=255;
    res1.at<Vec3b>(i,j)[2]=0;
    cout<<"yapıldı";

}
    if(res1.at<Vec3b>(i-1,j-1)[0]>=200){
    yap(i-1,j-1,&res1);
    res1.at<Vec3b>(i-1,j-1)[0]=0;
    res1.at<Vec3b>(i-3,j-3)[1]=255;
    res1.at<Vec3b>(i-3,j-3)[2]=0;

    }
    if(res1.at<Vec3b>(i-1,j)[0]>=200){
    yap(i-1,j,&res1);
    res1.at<Vec3b>(i-1,j)[0]=0;
    res1.at<Vec3b>(i-1,j)[1]=255;
    res1.at<Vec3b>(i-1,j)[2]=0;

    }

    if(res1.at<Vec3b>(i-1,j+1)[0]>=200){
    res1.at<Vec3b>(i-1,j+1)[0]=0;
    res1.at<Vec3b>(i-1,j+1)[1]=255;
    res1.at<Vec3b>(i-1,j+1)[2]=0;

    }
    if(res1.at<Vec3b>(i,j-1)[0]>=200){
    yap(i,j-1,&res1);
    res1.at<Vec3b>(i,j-1)[0]=0;
    res1.at<Vec3b>(i,j-1)[1]=255;
    res1.at<Vec3b>(i,j-1)[2]=0;

    }
    if(res1.at<Vec3b>(i,j+1)[0]>=200){
    yap(i,j+1,&res1);
    res1.at<Vec3b>(i,j+1)[0]=0;
    res1.at<Vec3b>(i,j+1)[1]=255;
    res1.at<Vec3b>(i,j+1)[2]=0;

    }
    if(res1.at<Vec3b>(i+1,j-1)[0]>=200){
    yap(i+1,j-1,&res1);
    res1.at<Vec3b>(i+1,j-1)[0]=0;
    res1.at<Vec3b>(i+1,j-1)[1]=255;
    res1.at<Vec3b>(i+1,j-1)[2]=0;

    }
    if(res1.at<Vec3b>(i+1,j)[0]>=200){
    yap(i+1,j,&res1);
    res1.at<Vec3b>(i+1,j)[0]=0;
    res1.at<Vec3b>(i+1,j)[1]=255;
    res1.at<Vec3b>(i+1,j)[2]=0;

    }
    if(res1.at<Vec3b>(i+1,j+1)[0]>=200){
    yap(i+1,j+1,&res1);
    res1.at<Vec3b>(i+1,j+1)[0]=0;
    res1.at<Vec3b>(i+1,j+1)[1]=255;
    res1.at<Vec3b>(i+1,j+1)[2]=0;
    }
 }

void mouseTikla(int evt, int x, int y, int flags, void* param) 
{   Vec3b color;  
Mat* rgb = (Mat*) param;

Mat p;

p=*rgb;
if (flags == (CV_EVENT_LBUTTONDOWN+CV_EVENT_FLAG_CTRLKEY)) 
{ 
    yap(y,x,&p);

    cout<<x<<y;
 }

 }

int main(){
Mat res;
res=imread("C:/Users/giray/Desktop/27.jpg");
int i;
int j;

//res1.data[res.channels()*(res.cols*(i)+(j))];

namedWindow("Secim", 1);

setMouseCallback("Secim", mouseTikla, &res);
imshow("Secim", res);

waitKey(0);

return 0;
}

这段代码在一千个方面都错了...

  1. 如果不用递归就不要使用递归
  2. 为什么你实际上创建了那么多指向单个图像的指针?
  3. 格式化真的很有帮助。
  4. 这里的内存重新分配简直是疯了。

这是您的代码的更好版本:

#include <iostream>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <queue>

using namespace cv;
using namespace std;

void yap(int i,int j, Mat* res){
    queue<pair<int, int>> q;


    q.push(pair<int, int>(i, j));
    while (!q.empty())
    {
        pair<int, int> p = q.front();
        q.pop();

        if ((p.first < 0) ||
            (p.second < 0) ||
            (p.first >= res->rows) ||
            (p.second >= res->cols))
            continue;

        if (res->at<Vec3b>(p.first, p.second)[0] > 200)
        {
            res->at<Vec3b>(p.first, p.second) = {0, 255, 0};
            q.push(pair<int,int>(p.first - 1, p.second - 1));
            q.push(pair<int,int>(p.first + 1, p.second + 1));
            q.push(pair<int,int>(p.first - 1, p.second + 1));
            q.push(pair<int,int>(p.first + 1, p.second - 1));
            q.push(pair<int,int>(p.first + 1, p.second));
            q.push(pair<int,int>(p.first - 1, p.second));
            q.push(pair<int,int>(p.first, p.second + 1));
            q.push(pair<int,int>(p.first, p.second - 1));
        }
    }

}

void mouseTikla(int evt, int x, int y, int flags, void* param)
{
    Mat* rgb = (Mat*) param;

    if (evt == CV_EVENT_LBUTTONDOWN)
    {
        yap(y,x,rgb);
        imshow("Secim", *rgb);
    }
}

int main(){
    Mat res;
    res=imread("put_your_path_here");
    namedWindow("Secim", 1);

    setMouseCallback("Secim", mouseTikla, &res);
    imshow("Secim", res);
    waitKey(0);

    return 0;
}