我在我的代码中使用OpenMp并获得错误结果

时间:2017-12-05 05:54:54

标签: c++ opencv computer-vision openmp

#pragma omp parallel for num_threads(6)
            for (int m = 0; m < k_num; m++) {       
                Point(0, 0) = double(data3[j * 3]) - mean1[m].at<double>(0, 0);
                Point(0, 1) = double(data3[j * 3 + 1]) - mean1[m].at<double>(0, 1);
                Point(0, 2) = double(data3[j * 3 + 2]) - mean1[m].at<double>(0, 2);

                end = Point * inv[m] * Point.t();
                distance = sqrt(end(0, 0));
                if (distance < min && distance>0) {      
                    color = m;
                    class_success++;
                    //  color_map[i + left_y][j + left_x] = color; 
                    data2[(j + left_x) * 3] = lab_k[m][0];
                    data2[(j + left_x) * 3 + 1] = lab_k[m][1];
                    data2[(j + left_x) * 3 + 2] = lab_k[m][2];
                    data4[j * 3] = lab_k[m][0];
                    data4[j * 3 + 1] = lab_k[m][1];
                    data4[j * 3 + 2] = lab_k[m][2];
                }
            }

我尝试遍历图像中的每个像素,计算它们与颜色模型之间的马哈拉诺比斯距离。(i,j)是点坐标。我检查了代码,所有变量不应该相互关联,但是当我使用openmp结果是不同的。任何帮助?非常感谢。

1 个答案:

答案 0 :(得分:0)

问题是变量是私有的,我在循环体内声明变量并得到正确的结果。

#pragma omp parallel for num_threads(6)
            for (int m = 0; m < k_num; m++) {       
                Mat_<double>end(1, 1);
                double distance = 1000;
                Mat_<double>Point(1, 3);
                ....
                }
            }