克隆cv::Mat
. 调用了两个函数,并在m_mask
我的类的成员变量(不是指针)上工作:
设置掩码:
void SetMask(QImage mask)
{
if(!mask.isNull() && mask.depth() == 1)
{
std::cout << "Mask width: " << mask.width() << " and mask height: " << mask.height() << std::endl << std::flush;
if(mask.width() != m_mask.cols || mask.height() != m_mask.rows)
m_mask.create(mask.height(), mask.width(), CV_8UC1);
if(m_mask.data == 0)
std::cout << "MALLOC FAILED" << std::endl << std::flush;
//Copy data here
cv::imshow("OpenCV Image", m_mask);
}
else
m_mask = cv::Scalar(0);
}
然后使用掩码:
QString MaskToXML()
{
QString xml_out;
if(!m_mask.empty())
{
cv::Mat workspace = m_mask.clone(); //Clone our mask - SEGFAULT HERE
//Run the contour code
std::vector< std::vector<cv::Point> > contours;
cv::findContours(workspace, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
//do stuff
}
return xml_out;
}