Changes

Jump to: navigation, search

GPU610/TeamEh

245 bytes added, 16:30, 5 December 2014
Assignment 3
This removed the edge checks from the convolution kernel allowing for a dramatic increase in Gaussian.
<pre>
//old
for (int i = -kXRad; i <= kXRad; i++){
for (int j = -kYRad; j <= kYRad; j++){
//wrap image
for ( int i xCorr = -kXRad; (x + i <= kXRad; 0) ? width + i: x ++i; xCorr = (xCorr >= width)? xCorr - width : xCorr;
{ int yCorr = (y + j < 0) ? height + j : y + j; yCorr = (yCorr >= height) ? yCorr - height : yCorr;
for (int j = -kYRad; j <= kYRad; j++) { //wrap image int xCorr = (x + i < 0) ? width + i : x + i; xCorr = (xCorr >= width) ? xCorr - width : xCorr; int yCorr = (y + j < 0) ? height + j : y + j; yCorr = (yCorr >= height) ? yCorr - height : yCorr;  const float kernelElement = kernel[kernelIndex(i, j, kernelXSize, kernelYSize)];  const GPU_RGBApixel& imageElement = img[imageIndex(xCorr, yCorr, height)]; outPixel.Red += kernelElement * imageElement.Red; outPixel.Green += kernelElement * imageElement.Green; outPixel.Blue += kernelElement * imageElement.Blue; }
outPixel.Red += kernelElement * imageElement.Red;
outPixel.Green += kernelElement * imageElement.Green;
outPixel.Blue += kernelElement * imageElement.Blue;
}
}
</pre>
<pre>
//new
for (int i = -kXRad; i <= kXRad; i++) {  for (int j = -kYRad; j <= kYRad; j++) { const float kernelElement = convolutionKernel[kernelIndex(i, j, kernelXSize, kernelYSize)]; const GPU_RGBApixel imageElement = img[imageIndex(xCorr + i, yCorr + j, height + kernelYSize - 1)]; outPixel.Red += kernelElement * imageElement.Red; outPixel.Green += kernelElement * imageElement.Green; outPixel.Blue += kernelElement * imageElement.Blue; }
const float kernelElement = convolutionKernel[kernelIndex(i, j, kernelXSize, kernelYSize)];
const GPU_RGBApixel imageElement = img[imageIndex(xCorr + i, yCorr + j, height + kernelYSize - 1)];
outPixel.Red += kernelElement * imageElement.Red;
outPixel.Green += kernelElement * imageElement.Green;
outPixel.Blue += kernelElement * imageElement.Blue;
}
}
</pre>
<b><font style="font-size:140%"> Canny </font></b>
notMaxSuppression used to have a large if block in the middle of the kernel that could potentially split into 4 threads. It was rewritten using a pre-computed table of outcomes and a little “mathemagic” to eliminate the if statements. It has reduced it reduces it from four paths to two.
<pre>
//changed the angle finding logic
 
//old
int index = imageIndex(x, y, height);
 
const float& gM = gradMagnitude[index];
 
result[index] = gM;
//don't touch the edges
if (x < 1 || y < 1 || x >= width - 1 || y >= height - 1) {  return; 
}
const float& gA = gradAngle[index];
 
int i;
 
int j;
result[index] = gradMagnitude[index];
if (((gA >= -M_PId8) && (gA <= M_PId8)) || (gA >= 7 * M_PId8) || (gA <= -7 * M_PId8)) {  i = 0;  j = -1; } else if (((gA <= 3 * M_PId8) && (gA > M_PId8)) || ((gA <= -5 * M_PId8) && (gA > -7 * M_PId8))){ i = -1; j = 1;}else if (((gA >= 5 * M_PId8) && (gA < 7 * M_PId8)) || ((gA < -M_PId8) && (gA >= -3 * M_PId8))){ i = -1;i j = -1;}else{j i = -1; j = 0;
}
else if (!((gA >gM <= 5 * M_PId8) && gradMagnitude[imageIndex(gA < 7 * M_PId8x + i, y + j, height)]) || (gM <= gradMagnitude[imageIndex(gA < x - 1, y -M_PId8j, height) && (gA >= -3 * M_PId8]))) { i = -1; j result[index] = -1gM
}
else</pre>
{ i = -1; j = 0; } if (!((gM <= gradMagnitude[imageIndex(x + i, y + j, height)]) || (gM <= gradMagnitude[imageIndex(x - 1, y - j, height)]))) { result[index] = gM; }pre>
//new
int xCorr = x + 1;
 
int yCorr = y + 1;
 
int heightCorr = height + 2;
 
int index = imageIndex(xCorr, yCorr, heightCorr);
const float gM = gradMagnitude[index];
 
const int sectorConvertX[9] = { 0, -1, -1, -1, -1, -1, -1, 0 , 0 };
 
const int sectorConvertY[9] = { -1, 1, 1, -1, -1, 0, 0, -1 , -1 };
 
const float gA = fabsf(gradAngle[imageIndex(x, y, height)]);
 
const int sector = int(gA / M_PI) * 8;
int i = sectorConvertX[sector];
 
int j = sectorConvertY[sector];
if (!((gM <= gradMagnitude[imageIndex(xCorr + i, yCorr + j, heightCorr)]) || (gM <= gradMagnitude[imageIndex(xCorr - 1, yCorr - j, heightCorr)]))){ result[index] = gM;}else result[index] = gM0.0
}
else</pre>
{hysterisisSecondPass was padded and had its inner double loop unrolled and the inner loop was replaced with divisions to eliminate branching.
result[index] = 0.0;
}
 
hysterisisSecondPass was padded and had its inner double loop unrolled and the inner loop was replaced with divisions to eliminate branching.
</pre>
//old
if (pixel >= lowerThreshold && pixel < upperThreshold) {  int iLower = (x == 0) ? 0 : -1; int iUpper = (x == width - 1) ? 0 : 1; int jLower = (height == 0) ? 0 : -1; int jUpper = (x == width - 1) ? 0 : 1;
int iUpper = (x == width - 1) ? 0 : 1; int jLower = (height == 0) ? 0 : -1; int jUpper = (x == width - 1) ? 0 : 1;  ret = 0.0; for (int i = iLower; i <= iUpper; i++) { for (int j = jLower; j <= jUpper; j++) { if (image[imageIndex(x + i, y + j, height)]) { ret = upperThreshold + 1; }
for (int i = iLower; i <= iUpper; i++){
for (int j = jLower; j <= jUpper; j++){
if (image[imageIndex(x + i, y + j, height)]){
ret = upperThreshold + 1;
}
}
}
}
}</pre>
}<pre>
//new
if (level >= lowerThreshold && level < upperThreshold){
{ level = 0.0; level += image[imageIndex(xCorr + 1, yCorr + 1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + 0, yCorr + 1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + -1, yCorr + 1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + 1, yCorr + 0, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + -1, yCorr + 0, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + 1, yCorr + -1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + 0, yCorr + -1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + -1, yCorr + -1, heightCorr)] / upperThreshold; level = level == 0 ? 0 : 255;}
level = 0.0; level += image[imageIndex(xCorr + 1, yCorr + 1, heightCorr)] </ upperThreshold; level += image[imageIndex(xCorr + 0, yCorr + 1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + -1, yCorr + 1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + 1, yCorr + 0, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + -1, yCorr + 0, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + 1, yCorr + -1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + 0, yCorr + -1, heightCorr)] / upperThreshold; level += image[imageIndex(xCorr + -1, yCorr + -1, heightCorr)] / upperThreshold; level = level == 0 ? 0 : 255; //level = (level) * (upperThreshold + 1); }pre>
<b><font style="font-size:140%"> Grey world </font></b>

Navigation menu