calculating euclidean distance for Luv values between 2 pixels - c++

In opencv c++ I'm trying to figure out how to calculate the euclidean distance between point i,j and all points within a 3x3 kernel. This is to create a contrast map of saliency from the Luv color space. I've also tried the norm function to no avail. I'm very confused as to how to solve this problem and would appreciate some feedback.
Mat tmp1 = MeanShift_Luv.clone();
int big_theta = 3; // kernel size / neighborhood to perform convolution on
Mat gradient_1 = Mat::zeros(tmp1.rows, tmp1.cols, CV_64FC3);
for (int i = 0; i < tmp1.rows; i++){
for (int j = 0; j < tmp1.cols; j++){
double dist = 0;
for (int m = -big_theta / 2; m < big_theta / 2; m++){
for(int n = -big_theta /2; n < big_theta / 2; n++){
if (m == 0 || n == 0) continue;
if (i + m < 0 || i + m >= tmp1.rows) continue;
if (j + n < 0 || j + n >= tmp1.cols) continue;
/* unsure what to do at this part
Point a(i,j);
Point b(i+m, j+n);
*/
}
}
gradient_1.at<Vec3d>(i,j) = dist;
}

Related

Convolution algorithm for image processing

I've come up with this code for applying a 3x3 kernel to my image:
double sum;
for(int i = 1; i < src.rows - 1; i++){
for(int j = 1; j < src.cols - 1; j++)
for (int k = 0; k < 3; k ++) {
sum = 0.0;
dst.at<cv::Vec3b>(i,j)[k] = 0.0;
for(int x = -1; x <= 1; x++){
for(int y = -1; y <=1; y++){
sum += (Kernel_Matrix[y+1][x+1]*src.at<cv::Vec3b>(i - x, j - y)[k]);
}
}
dst.at<cv::Vec3b>(i,j)[k] = cv::saturate_cast<uchar>(sum);
}
}
Now I got 2 questions:
By reading https://en.wikipedia.org/wiki/Kernel_(image_processing), there's various matrix for various filter, let's say I want my Blur filter to increase intensity, via a gui Slider that gives a value from x to whatever, what kind of operation should I make to my Blur Matrix(make a sum, a multiplication...)?
(I wold like to do the same with sharpness)
is there a specific matrix for Noise Reduction?
If you also have any mods to suggest me on my algorithm please let me know!
thanks!

Alpha-trimmed filter troubles

I am trying to make an alphatrimmed filter in openCV library. My code is not working properly and the resultant image is not looking as image after filtering.
The filter should work in the following way.
Chossing some (array) of pixels in my example it is 9 pixels '3x3' window.
Ordering them in increasing way.
Cutting our 'array' both sides for alpha-2.
calculating arithmetic mean of remaining pixels and inserting them in proper place.
int alphatrimmed(Mat img, int alpha)
{
Mat img9 = img.clone();
const int start = alpha/2 ;
const int end = 9 - (alpha/2);
//going through whole image
for (int i = 1; i < img.rows - 1; i++)
{
for (int j = 1; j < img.cols - 1; j++)
{
uchar element[9];
Vec3b element3[9];
int k = 0;
int a = 0;
//selecting elements for window 3x3
for (int m = i -1; m < i + 2; m++)
{
for (int n = j - 1; n < j + 2; n++)
{
element3[a] = img.at<Vec3b>(m*img.cols + n);
a++;
for (int c = 0; c < img.channels(); c++)
{
element[k] += img.at<Vec3b>(m*img.cols + n)[c];
}
k++;
}
}
//comparing and sorting elements in window (uchar element [9])
for (int b = 0; b < end; b++)
{
int min = b;
for (int d = b + 1; d < 9; d++)
{
if (element[d] < element[min])
{
min = d;
const uchar temp = element[b];
element[b] = element[min];
element[min] = temp;
const Vec3b temporary = element3[b];
element3[b] = element3[min];
element3[min] = temporary;
}
}
}
// index in resultant image( after alpha-trimmed filter)
int result = (i - 1) * (img.cols - 2) + j - 1;
for (int l = start ; l < end; l++)
img9.at<Vec3b>(result) += element3[l];
img9.at<Vec3b>(result) /= (9 - alpha);
}
}
namedWindow("AlphaTrimmed Filter", WINDOW_AUTOSIZE);
imshow("AlphaTrimmed Filter", img9);
return 0;
}
Without actual data, it's somewhat of a guess, but an uchar can't hold the sum of 3 channels. It works modulo 256 (at least on any platform OpenCV supports).
The proper solution is std::sort with a proper comparator for your Vec3b :
void L1(Vec3b a, Vec3b b) { return a[0]+a[1]+a[2] < b[0]+b[1]+b[2]; }

2D convolution - wrong results compared to opencv's output

I'm trying to implement a simple 2D convolution (mean filter in this case). But when I compare my results with an image generated by opencv's filter2D function I see a lot of differences. My current code is:
cv::Mat filter2D(cv::Mat& image, uint32_t kernelSize = 3)
{
float divider = kernelSize*kernelSize;
cv::Mat kernel = cv::Mat::ones(kernelSize,kernelSize,CV_32F) / divider;
int kHalf = kernelSize/2.f;
cv::Mat smoothedImage = cv::Mat::ones(image.rows,image.cols,image.type());
for (int32_t y = 0; y<image.rows; ++y) {
for (int32_t x = 0; x<image.cols; ++x) {
uint8_t sum = 0;
for (int m = -kHalf; m <= kHalf; ++m) {
for (int n = -kHalf; n <= kHalf; ++n) {
if (x+n >= 0 || x+n <= image.cols || y+m >= 0 || y <= image.rows) {
sum += kernel.at<float>(m+kHalf, n+kHalf)*image.at<uint8_t>(y-m+1, x-n+1);
} else {
// Zero padding - nothing to do
}
}
}
smoothedImage.at<uint8_t>(y,x) = sum;
}
}
return smoothedImage;
}
The results for a kernel size of five are (1. opencv, 2. my implementation):
I would appreciate if someone can explain me what I'm doing wrong.
For starter, your condition to account for edges should use && instead of || like so:
if (x+n >= 0 && x+n <= image.cols && y+m >= 0 && y <= image.rows)
This should help a little to remove artefacts around the edge.
Then, for the artefacts on the inner region, you should make sure the sum stays within the 0-255 range, and try to avoid loosing resolution every time you cast the partial result back to uint8_t as you assign to sum:
float sum = 0;
for (int m = -kHalf; m <= kHalf; ++m) {
for (int n = -kHalf; n <= kHalf; ++n) {
if (x+n >= 0 && x+n <= image.cols && y+m >= 0 && y <= image.rows) {
sum += kernel.at<float>(m+kHalf, n+kHalf)*image.at<uint8_t>(y-m+1, x-n+1);
} else {
// Zero padding - nothing to do
}
}
}
smoothedImage.at<uint8_t>(y,x) = std::min(std::max(0.0f, sum), 255.0f);

optimizing for loops in c++ code

Integer Range = 1;
for(Integer k = -Range; k <= Range; ++k)
{
for(Integer j = -Range; j <= Range; ++j)
{
for(Integer i = -Range; i <= Range; ++i)
{
Integer MCID = GetCellID(&CONSTANT_BOUNDINGBOX,CIDX +i, CIDY + j,CIDZ
+ k);
if(MCID < 0 || MCID >= c_CellNum)
{
continue;
}
unsigned int TriangleNum = c_daCell[MCID].m_TriangleNum;
for(unsigned int l = 0; l < TriangleNum; ++l)
{
TriangleID=c_daCell[MCID].m_TriangleID[l];
if( TriangleID >= 0 && TriangleID < c_TriangleNum && TriangleID
!= NearestID)// No need to calculate again for the same triangle
{
CDistance Distance ;
Distance.Magnitude = CalcDistance(&c_daTriangles[TriangleID], &TargetPosition,
&Distance.Direction);
if(Distance.Magnitude < NearestDistance.Magnitude)
{
NearestDistance = Distance;
NearestID = TriangleID;
}
}
}
}
}
}
}
c_daSTLDistance[ID] = NearestDistance;
c_daSTLID[ID] = NearestID;
GetCellID is the function to return the cellid in the variable CID with CIDX,CIDY,CIDZ with its position in the 3 axes
here the above code is a function to calculate the distance ,actually STL distance between a point and the triangles of the stl. This code runs fine however the problem is it is too slow as it has large number of loops within the code. Now my concern is to optimize the loop. Is there any technique of optimizing the loops within the code?

Laplacian does not give desired output

Here is my code:
int Factor=3,offset=0,k,l,p,q;
IplImage * image = cvCreateImage(cvSize(img->width, img->height),img->depth, img->nChannels);
cvCopy (img, image, 0);
long double mean=0,nTemp=0,c,sum=0,n=0,s=0,d=0;
int i=0,j=0,krow,kcol;
kernel[0][0]=kernel[0][2]=kernel[2][0]=kernel[2][2]=0;
kernel[0][1]=kernel[1][0]=kernel[1][2]=kernel[2][1]=1;
kernel[1][1]=-4;
uchar* temp_ptr=0 ;
int rows=image->height,cols=image->width,row,col;
//calculate the mean of image and deviation
for ( row = 1; row < rows - 2; row++ )
{
for ( col = 1; col < cols - 2; col++ )
{
nTemp = 0.0;
for (p=0, krow = -1 ; p < 3; krow++,p++)
{
for (q=0, kcol = -1; q < 3; kcol++,q++)
{
temp_ptr = &((uchar*)(image->imageData + (image->widthStep*(row+krow))))[(col+kcol)*3];
for(int k=0; k < 3; k++)
Pixel[p][q].val[k]=temp_ptr[k];
}
}
for (i=0 ; i < 3; i++)
{
for (j=0 ; j < 3; j++)
{
c = (Pixel[i][j].val[0]+Pixel[i][j].val[1]+Pixel[i][j].val[2])/Factor ;
nTemp += (double)c * kernel[i][j];
}
}
sum += nTemp;
n++;
}
}
mean = ((double)sum / n);
for ( row = 1; row < rows - 2; row++ )
{
for ( col = 1; col < cols - 2; col++ )
{
nTemp = 0.0;
for (p=0, krow = -1 ; p < 3; krow++,p++)
{
for (q=0, kcol = -1; q < 3; kcol++,q++)
{
temp_ptr = &((uchar*)(image->imageData + (image->widthStep*(row+krow))))[(col+kcol)*3];
for(int k=0; k < 3; k++)
Pixel[p][q].val[k]=temp_ptr[k];
}
}
for (i=0 ; i < 3; i++)
{
for (j=0 ; j < 3; j++)
{
c = (Pixel[i][j].val[0]+Pixel[i][j].val[1]+Pixel[i][j].val[2])/Factor ;
nTemp += (double)c * kernel[i][j];
}
}
s = (mean - nTemp);
d += (s * s);
}
}
d = d / (n - 1);
d = (sqrt(d));
d=d* 2;
// Write to image
for ( row = 1; row < rows - 2; row++ )
{
for ( col = 1; col < cols - 2; col++ )
{
nTemp = 0.0;
for (p=0, krow = -1 ; p < 3; krow++,p++)
{
for (q=0, kcol = -1; q < 3; kcol++,q++)
{
temp_ptr = &((uchar*)(image->imageData + (image->widthStep*(row+krow))))[(col+kcol)*3];
for(int k=0; k < 3; k++)
Pixel[p][q].val[k]=temp_ptr[k];
}
}
for (i=0 ; i < 3; i++)
{
for (j=0 ; j < 3; j++)
{
c = (Pixel[i][j].val[0]+Pixel[i][j].val[1]+Pixel[i][j].val[2])/Factor ;
nTemp += (double)c * kernel[i][j];
}
}
temp_ptr = &((uchar*)(image->imageData + (image->widthStep*row)))[col*3];
if (nTemp > d)
temp_ptr[0]=temp_ptr[1]=temp_ptr[2]=255;
else
temp_ptr[0]=temp_ptr[1]=temp_ptr[2]=0;
}
}
Where am i going wrong? I have implemented Gaussian Filtering in a similar manner, is there anything wrong in my algorithm?
It seems that your code (labeled "Write to image") overwrites the input image during the calculations. This is not good. Create a copy of the image, calculate its pixels, then delete the original image.
I noticed is that your code is needlessly complex and inefficient. You don't need to convolve the image before calculating its mean — the convolution just multiplies the mean by the sum of the kernel entries.
Also, since your convolution kernel sums to zero, the mean you get after convolving with it will also (almost) be zero. The only non-zero contributions will come from the edges of the image. I rather doubt that's actually what you want to calculate (and if it is, you could save a lot of time by summing only over the edges in the first place).
Third, as pointed out in another answer (I missed this one myself), you're not averaging over all the color channels, you're averaging over the red channel three times. (Besides, you should probably use a weighted average anyway, after applying gamma correction.)
And finally, as anatolyg said, you're overwriting the image data before you're done reading it. There are several ways to fix that, but the easiest is to write your output to a separate buffer.