문제

I have a problem with final picture, it is distorted and I don't know why. It is only when I turn on openmp option in Visual Studio. If its working for one thread there is no problem and the edges are clear. The code is right below.

#include "stdafx.h"
#include<iostream>
#include<omp.h>
#include<cmath>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;

int xGradient(Mat image, int x, int y)
{
return image.at<uchar>(y-1, x-1) +
            2*image.at<uchar>(y, x-1) +
             image.at<uchar>(y+1, x-1) -
              image.at<uchar>(y-1, x+1) -
               2*image.at<uchar>(y, x+1) -
                image.at<uchar>(y+1, x+1);
}
int yGradient(Mat image, int x, int y)
{
    return image.at<uchar>(y-1, x-1) +
            2*image.at<uchar>(y-1, x) +
             image.at<uchar>(y-1, x+1) -
              image.at<uchar>(y+1, x-1) -
               2*image.at<uchar>(y+1, x) -
                image.at<uchar>(y+1, x+1);
}
int main()

 {

  Mat src, grey, dst;
 double start, end;
     start = omp_get_wtime();
  int gx, gy, sum;
src= imread("E:/image.jpg");  
cvtColor(src,grey,CV_BGR2GRAY);
  dst = grey.clone();
  if( !grey.data )
  { return -1; }
 #pragma omp parallel for  
  for(int y = 0; y < grey.rows; y++)
        for(int x = 0; x < grey.cols; x++)
            dst.at<uchar>(y,x) = 0;  
 #pragma omp parallel for
   for(int y = 1; y < grey.rows - 1; y++){
        for(int x = 1; x < grey.cols - 1; x++){
            gx = xGradient(grey, x, y);
            gy = yGradient(grey, x, y);
            sum = abs(gx) + abs(gy);
            sum = sum > 255 ? 255:sum;
            sum = sum < 0 ? 0 : sum;
            dst.at<uchar>(y,x) = sum;

                }
    }
    namedWindow("sobel");
    imshow("sobel", dst);
    namedWindow("grayscale");
    imshow("grayscale", grey);
    namedWindow("Original");
    imshow("Original", src);
    end = omp_get_wtime();
    cout<<"time is: "<<(end-start)<< " seconds" <<endl;
waitKey();
    return 0;
}

Thank you for any help and answers

도움이 되었습니까?

해결책

There is a race condition in gx, gy, and sum. They are all shared and should be made private. Just define them when you use them inside the parallel loop and it should fix your problem. Like this

   #pragma omp parallel for
   for(int y = 1; y < grey.rows - 1; y++){
        for(int x = 1; x < grey.cols - 1; x++){
            int gx = xGradient(grey, x, y);
            int gy = yGradient(grey, x, y);
            int sum = abs(gx) + abs(gy);
            sum = sum > 255 ? 255:sum;
            sum = sum < 0 ? 0 : sum;
            dst.at<uchar>(y,x) = sum;

다른 팁

i hope this can help u :)

 #pragma omp parallel for private (gx, gy, sum) //num_threads (2)
    for(int y = 1; y < grey.rows - 1; y++){
        for(int x = 1; x < grey.cols - 1; x++){     
            gx = xGradient(grey, x, y);
            gy = yGradient(grey, x, y);
            sum = abs(gx) + abs(gy);
            sum = sum > 255 ? 255:sum;
            sum = sum < 0 ? 0 : sum;
            dst.at<uchar>(y,x) = sum;
        }
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top