c++ - Opencv Mat - 访问冲突错误 C++

标签 c++ opencv access-violation

我正在实现一个算法,我为自己的极端for循环辩解,还没有找到更好的方法。

问题是,在第 81 行的第二次迭代中,它在 Test.exe 中的 0x000000007707320E (ntdll.dll) 处给出了第一次机会异常:0xC0000005:访问冲突读取位置 0xFFFFFFFFFFFFFFFF。

void co_hog(Mat image, int offset, int blockSize, int nrBins, int cat) {
Mat img_x;
Mat img_y;
IplImage img = image;

Mat kern_x = (Mat_<char>(1, 3) << -1, 0, 1);
Mat kern_y = (Mat_<char>(3, 1) << -1, 0, 1);

filter2D(image, img_x, image.depth(), kern_x);
filter2D(image, img_y, image.depth(), kern_y);

Size imageSize = image.size();

int nrBlocksY = imageSize.height / blockSize;
int nrBlocksX = imageSize.width / blockSize;

int degreePerBin = 180 / nrBins;

Mat gradients = Mat(image.size(), CV_32FC1);
Mat magnitudes = Mat(image.size(), CV_32FC1);
for(int y = 0; y < image.rows; y++) {
    for(int x = 0; x < image.cols; x++) {
        float grad_x = (float)img_x.at<uchar>(y, x);
        float grad_y = (float)img_y.at<uchar>(y, x);
        gradients.at<float>(y, x) = abs(atan2(grad_y, grad_x) * 180 / PI);
        magnitudes.at<float>(y, x) = sqrt(pow(grad_x, 2) + pow(grad_y, 2));
    }
}

int bin_1, bin_2, bin_3, bin_4;
double theta_1, theta_2, theta_3, theta_4;

Mat H;  
stringstream line(stringstream::in | stringstream::out);
line << cat << " ";
int index = 1;

for(int i = 0; i < nrBlocksY; i++) {
    for(int j = 0; j < nrBlocksX; j++) {
        Mat coOccMat = Mat::zeros(nrBins, nrBins, CV_32FC1);
        for(int q = i * blockSize; q < (i * blockSize) + blockSize; q++) {
            for(int p = j * blockSize; p < (j * blockSize) + blockSize; p++) {
                for(int offy = -offset; offy < offset; offy++) {
                    for(int offx = -offset; offx < offset; offx++) {
                        if((q + offy) >= imageSize.height || (p + offx) >= imageSize.width || (q + offy) < 0 || (p + offx) < 0) {
                            continue;
                        }
                        float m_1 = magnitudes.at<float>(q, p);
                        float m_2 = magnitudes.at<float>(q + offy, p + offx);
                        float alpha = gradients.at<float>(q, p);
                        float beta = gradients.at<float>(q + offy, p + offx);
                        if(fmod(alpha / degreePerBin, 1) > 0.5) {
                            bin_1 = floor(alpha / degreePerBin);
                            bin_2 = bin_1 + 1;
                        } else {
                            bin_2 = floor(alpha / degreePerBin);
                            bin_1 = bin_2 - 1;
                        }

                        if(fmod(beta / degreePerBin, 1) > 0.5) {
                            bin_3 = floor(beta / degreePerBin);
                            bin_4 = bin_3 + 1;
                        } else {
                            bin_4 = floor(beta / degreePerBin);
                            bin_3 = bin_4 - 1;
                        }

                        theta_1 = (bin_1 * degreePerBin) + (degreePerBin / 2);
                        theta_2 = (bin_2 * degreePerBin) + (degreePerBin / 2);
                        theta_3 = (bin_3 * degreePerBin) + (degreePerBin / 2);
                        theta_4 = (bin_4 * degreePerBin) + (degreePerBin / 2);

                        coOccMat.at<float>(bin_1, bin_3) += (m_1 * (1 - (alpha - theta_1) / (theta_2 - theta_1))) + (m_2 * (1 - (beta - theta_3) / (theta_4 - theta_1))); 
                        coOccMat.at<float>(bin_1, bin_4) += (m_1 * (1 - (alpha - theta_1) / (theta_2 - theta_1))) + (m_2 * ((beta - theta_3) / (theta_4 - theta_1)));
                        coOccMat.at<float>(bin_2, bin_3) += (m_1 * ((alpha - theta_1) / (theta_2 - theta_1))) + (m_2 * (1 - (beta - theta_3) / (theta_4 - theta_1)));
                        coOccMat.at<float>(bin_2, bin_4) += (m_1 * ((alpha - theta_1) / (theta_2 - theta_1))) + (m_2 * ((beta - theta_3) / (theta_4 - theta_1)));
                    }
                }
            }
        }
        cout << coOccMat << endl;
        -> Next statement to be called *passes the first time* H = coOccMat.reshape(0, 1);
        normalize(H, H);
        cout << H.size() << endl;
        for(int i = 0; i < H.cols; ++i) {
            for(int j = 0; j < H.rows; ++j) {
                if(H.at<float>(j, i) > 0) {
                    line << index << ":" << H.at<float>(j, i) << " ";
                }
                index++;
            }
        }
        cout << "Done" << index << endl;
    }   
}
 }

最佳答案

问题已修复,有时 bin 的值设置为 -1,因此无法访问它,Visual Studio 的调试工具无法指出问题所在。

关于c++ - Opencv Mat - 访问冲突错误 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18900824/

相关文章:

c++ - 将 cv::Mat 转换为 SDL_Texture

python - 在 Python 中获取彩色形状周围边界的坐标

函数前的 Python 星号

c++ - 字符串到 PCWSTR -> 奇怪的返回(C++)

c++ - 访问冲突 OpenCV C++ dll

c++ - OpenMP 在 for() 循环中面临的情况

c++ - 为什么这个变量需要是静态的?

c++ - Qt C++ 从线程发出,在插槽 GUI 中

html - 从 HTML 文档树打印 CSS 的算法

c++ - 将输入字符串转换为 char* <Bad Ptr>