I am new to Image Processing please help

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
Locked
tidusleonart
Posts: 1
Joined: 2015-02-03T10:09:51-07:00
Authentication code: 6789

I am new to Image Processing please help

Post by tidusleonart »

I am studying Image Processing with Convolution methods. I was supposed to write the method in C++. i wrote in normal method (using 4 for loops) but the teacher said it was not corrected. And i need to write the way he was explaining in his slides as Convolution by Copying, Multiplying, and Shifting the Image. But the code i wrote turn out not gave me the same result as the filter2D methods and the original convolution (4 for loops). Can someone please help me with this matter. i dont know what i done wrong in the method convolutionFast(). Thank you

Code: Select all

//For 4 loops
void convolutionColor(Mat src, Mat dst, Mat kernel)
{
   float sum1, sum2, sum3;
   int x = kernel.rows/2, y = kernel.cols/2;
   for(int i = x; i < src.rows-x; i++)
   {
      for(int j = y; j < src.cols-y; j++)
      {
         sum1 = 0, sum2 = 0, sum3 = 0;
         for(int k = -x; k <= x; k++)
         {
            for(int l = -y; l <= y; l++)
            {
               sum1 += kernel.at<float>(k+x,l+y) * src.at<Vec3b>(i-k, j-l)[0];
               sum2 += kernel.at<float>(k+x,l+y) * src.at<Vec3b>(i-k, j-l)[1];
               sum3 += kernel.at<float>(k+x,l+y) * src.at<Vec3b>(i-k, j-l)[2];
            }
         }
         dst.at<Vec3b>(i,j)[0] = fabs(sum1);
         dst.at<Vec3b>(i,j)[1] = fabs(sum2);
         dst.at<Vec3b>(i,j)[2] = fabs(sum3);
      }
   }
}

//Convolution Other methods
void convolutionFast(Mat src, Mat dst, Mat kernel)
{
   int x = kernel.rows/2;
   float sum = 0;
   Mat padded;
   copyMakeBorder(src, padded, x,x,x,x,BORDER_CONSTANT, 0);
   //imshow("Padded", padded);
   for(int i = 0; i < kernel.rows; i++)
   {
      for(int j = 0; j < kernel.cols; j++)
      {
         sum += kernel.at<float>(i,j);
         Mat roi(padded, Rect(i, j, src.rows, src.cols));
         Mat roi_copy = roi.clone();
         roi_copy = roi_copy.mul((double)kernel.at<float>(i,j));
         dst += roi_copy;
      }
   }
   if(sum)
      dst *= 1/sum;
   //dst *= 1.0/(kernel.cols * kernel.rows);
}
int main()
{
	double t = (double)getTickCount();

	Mat image = imread("lena.jpg", 1);
	Mat conv(image.rows, image.cols, CV_8U, Scalar(0));
	Mat conv1(image.rows, image.cols, CV_8UC3, Scalar(0,0,0));
	Mat conv2(image.rows, image.cols, CV_8UC3, Scalar(0,0,0));
	Mat conv3(image.rows, image.cols, CV_8UC3, Scalar(0,0,0));

	Mat kernel(5,5, CV_32F, 1/25.0);
	Mat kernel2 = (Mat_<float>(3,3) << 0,-1,0,
										-1,4,-1,
										0,-1,0);

	//convolutionColor(image, conv, kernel);
	imshow("Original", image);
	convolutionFast(image, conv1, kernel);
	convolutionColor(image, conv2, kernel);
	filter2D(image, conv3,-1, kernel);
	imshow("opencv", conv3);
	imshow("ConvoFast", conv1);
	imshow("ConvoNorm", conv2);
	t = ((double)getTickCount() - t) / getTickFrequency();
	cout << "Time: " << t << endl;
	waitKey();
	return 0;
}

Locked