C++ 使用OpenCV时出现内存错误
照片如下: 当我点击鼠标左键和[CTRL]键时,我想看到这个穿绿色衣服的白人 代码看起来是单击点的8个相邻点,并进行递归。 代码在小矩阵中正常工作。但在这张照片中,我得到了一个记忆错误。我想见那个穿绿色衣服的人。如何正确地修复代码C++ 使用OpenCV时出现内存错误,c++,visual-studio,opencv,vector,out-of-memory,C++,Visual Studio,Opencv,Vector,Out Of Memory,照片如下: 当我点击鼠标左键和[CTRL]键时,我想看到这个穿绿色衣服的白人 代码看起来是单击点的8个相邻点,并进行递归。 代码在小矩阵中正常工作。但在这张照片中,我得到了一个记忆错误。我想见那个穿绿色衣服的人。如何正确地修复代码 #include <iostream> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> using namespace c
#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;
}
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
int n=0;
无效yap(内部i、内部j、材料*res){
ostringstream t;
Mat*rgb=(Mat*)res;
matres1;
res1=*rgb;
这个代码在很多方面都是错误的
如果没有递归,就不要使用它
你为什么要创建这么多指向那个图像的指针
格式化真的很有帮助
内存重新分配在这里简直是疯了
以下是更好的代码版本:
#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;
}
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
无效yap(内部i、内部j、材料*res){
队列q;
q、 推(对(i,j));
而(!q.empty())
{
对p=q.前();
q、 pop();
如果((p.first<0)||
(p.second<0)||
(p.first>=res->rows)||
(秒>=res->cols)
继续;
如果(res->at(p.first,p.second)[0]>200)
{
res->at(p.first,p.second)={0,255,0};
q、 推(一对(p.first-1,p.second-1));
q、 推(成对(第一对+1,第二对+1);
q、 推(成对(第一对-1,第二对+1);
q、 推(成对(第一对+1,第二对-1);
q、 推(成对(第一对+1,第二对));
q、 推(一对(第一对-1对,第二对));
q、 推(成对(第一对,第二对+1);
q、 推(一对(p.first,p.second-1));
}
}
}
void mouseTikla(int-evt、int-x、int-y、int-flags、void*param)
{
Mat*rgb=(Mat*)参数;
如果(evt==CV_事件_LBUTTONDOWN)
{
yap(y,x,rgb);
imshow(“Secim”,rgb);
}
}
int main(){
材料;
res=imread(“把你的路径放在这里”);
namedWindow(“Secim”,1);
setMouseCallback(“Secim”、“mouseTikla和res”);
imshow(“Secim”,res);
等待键(0);
返回0;
}
你说你“得到一个内存错误”,这是什么意思?什么错误?你从哪里得到错误?你试过调试你的代码来查找或定位错误,或者捕捉错误吗?顺便说一句,在yap
函数中,我看到你修改了很多res1
。但它是函数中的局部变量。(早期)递归调用将看不到这些(稍后)更改,当函数返回且res1
超出范围时,更改将丢失。您是否检查过索引算法(i
和j
加/减一)是否超出范围?呵呵,对不起,遗漏了您使用的Ctrl键。如果您使用我的示例,请修复它。我想在我使人变为绿色时对单击的像素进行坐标。我编写了代码获取坐标,但我无法实现您的代码。我的代码如下。@Leontyev Georgiy