为什么我无法访问函数中的对象?
Why can't I access the Object in my function?
我有一个函数,检测两帧之间的运动,并在变量cv::Mat result_庄稼中存储仅移动对象的裁剪图像。现在我想添加一个函数来检查result_庄稼是否有黑色像素。我写的代码很容易,但我完全卡住了,试图实现它在我的类。
由于某些原因,我的blackDetection(Mat &裁剪)无法访问裁剪后的图像,这会导致程序崩溃。下面是我的简化代码:
void ActualRec::run(){
while (isActive){
//...code to check for motion
//if there was motion a cropped image will be stored in result_cropped
number_of_changes = detectMotion(motion, result, result_cropped, region, max_deviation, color);
if(number_of_changes>=there_is_motion) {
if(number_of_sequence>0){
// there was motion detected, store cropped image - this works
saveImg(pathnameThresh, result_cropped);
if (blackDetection(result_cropped)==true){
//the cropped image has black pixels
}
else {
//the cropped image has no black pixels
}
number_of_sequence++;
}
else
{
// no motion was detected
}
}
}
bool ActualRec::blackDetection(Mat & result_cropped){
//...check for black pixels, program crashes since result_cropped is empty
//if i add imshow("test",result_cropped) I keep getting an empty window
if (blackPixelCounter>0){
return true;
}
else return false;
}
再次,问题是我无法在blackDetection(Mat &result_cropped)。
\edit:该类的完整代码http://pastebin.com/3i0WdLG0。谁来帮帮我吧。这个问题对我来说没有任何意义。
在blackDetection()
中没有cv::waitKey()
,因此在到达run()
中的cvWaitKey()
之前您将崩溃。你急于得出result_cropped
是"空"的结论。
您没有在任何地方分配croppedBlack
,因此您将在croppedBlack.at<Vec3b>(y,x)[c] =
上崩溃。
在blackDetection()
(例如)开头添加:
croppedBlack.create(result_cropped.size(), result_cropped.type());
要使它更快,请参阅如何扫描图像…使用OpenCV:有效的方法
bool ActualRec::blackDetection(Mat& result_cropped)
{
croppedBlack.create(result_cropped.size(), result_cropped.type());
int blackCounter = 0;
for(int y = 0; y < result_cropped.rows; ++y)
{
Vec3b* croppedBlack_row = croppedBlack.ptr<Vec3b>(y);
Vec3b* result_cropped_row = result_cropped.ptr<Vec3b>(y);
for(int x = 0; x < result_cropped.cols; ++x)
{
for(int c = 0; c < 3; ++c)
{
croppedBlack_row[x][c] =
saturate_cast<uchar>(alpha * result_cropped_row[x][c] + beta);
}
}
}
}
相关文章:
- 如何创建对象函数指针C++映射?
- 为什么我可以改变常量对象中的成员变量,这是返回常量对象函数的结果?
- 如何将对象函数的实例传递给另一个函数
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- makefile对我的名称空间对象/函数/构造函数的不确定引用
- 将对象函数转换为函数指针
- 非对象函数/类函数C++
- 线程对象函数 C++
- C 将成员对象函数分配给类成员功能
- 使用基本指针调用派生对象函数
- 可以(通过编译器)使用多少个线程来初始化全局对象(函数main之前)
- C++类对象函数
- 对对象::函数的未定义引用
- 无法弄清楚将多个对象函数作为单独的线程调用的语法
- 在提升作用域出口中调用对象函数
- 使用基指针来使用派生对象函数
- 在for_each lambda 中调用对象函数
- C++:: 模板函数 - 从对象函数获取对象的地址
- Qt5 未解析的外部静态元对象函数
- 通过变量使用对象和对象函数