Skip to content
Snippets Groups Projects
Commit 19e864d6 authored by ylvse560's avatar ylvse560
Browse files

Lab 3 average and gaussian done

parent 36952f9d
No related branches found
No related tags found
No related merge requests found
......@@ -26,25 +26,47 @@ unsigned char average_kernel(skepu::Region2D<unsigned char> m, size_t elemPerPx)
return res * scaling;
}
unsigned char average_kernel_1d(skepu::Region1D<unsigned char> m, size_t elemPerPx)
unsigned char average_kernel_1d_col(skepu::Region1D<unsigned char> m, size_t elemPerPx)
{
float scaling = 1.0 / (m.oi*2+1);
float res = 0;
for (int y = -m.oi; y <= m.oi; ++y)
for (int y = -m.oi; y <= m.oi; y ++)
res += m(y);
return res * scaling;
}
unsigned char average_kernel_1d_row(skepu::Region1D<unsigned char> m, size_t elemPerPx)
{
float scaling = 1.0 / (m.oi/elemPerPx*2+1);
float res = 0;
for (int y = -m.oi; y <= m.oi; y += elemPerPx)
res += m(y);
return res * scaling;
}
unsigned char gaussian_kernel(skepu::Region1D<unsigned char> m, const skepu::Vec<float> stencil, size_t elemPerPx)
{
// your code here
return m(0);
}
int stepsize = 1;
int stencilstep = 0;
// If the kernel is row wise
if(m.oi*2+1 != (int)stencil.size) {
printf("%i\n", (int)stencil.size);
stepsize = (int)elemPerPx;
}
else {
printf("%s\n", "col");
}
float res = 0;
for (int y = -m.oi; y <= m.oi; y += stepsize) {
res += stencil(stencilstep)*m(y);
//printf("%f\n", stencil(stencilstep));
stencilstep++;
}
return res;
}
int main(int argc, char* argv[])
{
......@@ -71,6 +93,7 @@ int main(int argc, char* argv[])
ImageInfo imageInfo;
skepu::Matrix<unsigned char> inputMatrixPad = ReadAndPadPngFileToMatrix(inputFileName, radius, colorType, imageInfo);
skepu::Matrix<unsigned char> inputMatrix = ReadPngFileToMatrix(inputFileName, colorType, imageInfo);
skepu::Matrix<unsigned char> outputMatrixFirstPass(imageInfo.height, imageInfo.width * imageInfo.elementsPerPixel, 120);
skepu::Matrix<unsigned char> outputMatrix(imageInfo.height, imageInfo.width * imageInfo.elementsPerPixel, 120);
// more containers...?
......@@ -93,20 +116,17 @@ int main(int argc, char* argv[])
// use conv.setOverlapMode(skepu::Overlap::[ColWise RowWise]);
// and conv.setOverlap(<integer>)
{
auto conv1 = skepu::MapOverlap(average_kernel_1d);
//auto conv2 = skepu::MapOverlap(average_kernel_1d);
conv1.setOverlapMode(skepu::Overlap::ColWise);
conv1.setOverlap(radius*imageInfo.elementsPerPixel);
//conv2.setOverlapMode(skepu::Overlap::RowWise);
//conv2.setOverlap(radius*imageInfo.elementsPerPixel);
auto conv_row = skepu::MapOverlap(average_kernel_1d_row);
conv_row.setOverlapMode(skepu::Overlap::RowWise);
conv_row.setOverlap(radius*imageInfo.elementsPerPixel);
auto conv_col = skepu::MapOverlap(average_kernel_1d_col);
conv_col.setOverlapMode(skepu::Overlap::ColWise);
conv_col.setOverlap(radius);
auto timeTaken = skepu::benchmark::measureExecTime([&]
{
// your code here
conv1(outputMatrix, inputMatrixPad, imageInfo.elementsPerPixel);
//conv2(outputMatrix, inputMatrixPad, imageInfo.elementsPerPixel);
conv_row(outputMatrixFirstPass, inputMatrix, imageInfo.elementsPerPixel);
conv_col(outputMatrix, outputMatrixFirstPass, imageInfo.elementsPerPixel);
});
WritePngFileMatrix(outputMatrix, outputFile + "-separable.png", colorType, imageInfo);
......@@ -117,15 +137,19 @@ int main(int argc, char* argv[])
// Separable gaussian
{
skepu::Vector<float> stencil = sampleGaussian(radius);
// skeleton instance, etc here (remember to set backend)
auto conv = skepu::MapOverlap(gaussian_kernel);
conv.setOverlapMode(skepu::Overlap::ColWise);
conv.setOverlap(radius);
auto timeTaken = skepu::benchmark::measureExecTime([&]
{
// your code here
conv(outputMatrixFirstPass, inputMatrix, stencil, imageInfo.elementsPerPixel);
conv.setOverlapMode(skepu::Overlap::RowWise);
conv.setOverlap(radius*imageInfo.elementsPerPixel);
conv(outputMatrix, outputMatrixFirstPass, stencil, imageInfo.elementsPerPixel);
});
// WritePngFileMatrix(outputMatrix, outputFile + "-gaussian.png", colorType, imageInfo);
WritePngFileMatrix(outputMatrix, outputFile + "-gaussian.png", colorType, imageInfo);
std::cout << "Time for gaussian: " << (timeTaken.count() / 10E6) << "\n";
}
......
......@@ -19,56 +19,106 @@
unsigned char median_kernel(skepu::Region2D<unsigned char> image, size_t elemPerPx)
{
// your code here
int row_size = image.oi*2+1;
int col_size = image.oj*2+1;
int n = row_size*col_size;
int i, j, x, y, x_next, y_next;
unsigned char temp;
for (i = 0; i < n-1; i++){
// Last i elements are already in place
for (j = 0; j < n-i-1; j += elemPerPx) {
x = j%row_size;
y = (j-x)/row_size;
x_next = (j+elemPerPx)%row_size;
y_next = (j+elemPerPx-x_next)/row_size;
if (image(x,y) > image(x_next, y_next)) {
temp = image(x,y);
printf("%uc\n", temp);
//image.data[x,y] = image(x_next, y_next);
//image.data[x_next, y_next] = temp;
}
}
}
return image(0,0);
}
/*
unsigned char median_kernel_vector(skepu::Region2D<unsigned char> image, skepu::Vector<unsigned char> sortingVec, size_t elemPerPx)
{
int row_size = image.oi*2+1;
int col_size = image.oj*2+1;
int n = row_size*col_size;
int i, j, x, y, x_next, y_next;
int k = 0;
unsigned char temp;
for(i = -image.oj; i <= image.oi; i++)
for(j = -image.oj; j <= image.oj; j++)
sortingVec(k) = image(i,j);
for (i = 0; i < n-1; i++){
// Last i elements are already in place
for (j = 0; j < n-i-1; j += elemPerPx) {
if (sortingVec(j) > sortingVec(j+1)) {
temp = sortingVec(j);
sortingVec(j) = sortingVec(j+1);
sortingVec(j+1) = temp;
}
}
}
return image(0,0);
}
*/
int main(int argc, char* argv[])
{
LodePNGColorType colorType = LCT_RGB;
if (argc < 5)
{
std::cout << "Usage: " << argv[0] << " input output radius [backend]\n";
exit(1);
}
std::string inputFileName = argv[1];
std::string outputFileName = argv[2];
const int radius = atoi(argv[3]);
auto spec = skepu::BackendSpec{argv[4]};
skepu::setGlobalBackendSpec(spec);
// Create the full path for writing the image.
std::stringstream ss;
ss << (2 * radius + 1) << "x" << (2 * radius + 1);
std::string outputFileNamePad = outputFileName + ss.str() + "-median.png";
// Read the padded image into a matrix. Create the output matrix without padding.
ImageInfo imageInfo;
skepu::Matrix<unsigned char> inputMatrix = ReadAndPadPngFileToMatrix(inputFileName, radius, colorType, imageInfo);
std::cout << imageInfo.height << ", " << imageInfo.width << std::endl;
std::cout << inputMatrix.total_cols() << ", " << inputMatrix.total_rows() << std::endl;
skepu::Matrix<unsigned char> outputMatrix(imageInfo.height, imageInfo.width * imageInfo.elementsPerPixel, 120);
std::cout << outputMatrix.total_cols() << ", " << outputMatrix.total_rows() << std::endl;
// Skeleton instance
auto calculateMedian = skepu::MapOverlap(median_kernel);
calculateMedian.setOverlap(radius, radius * imageInfo.elementsPerPixel);
skepu::Vector<unsigned char> sortingVec((radius*2+1)*(radius*2+1)*imageInfo.elementsPerPixel);
auto timeTaken = skepu::benchmark::measureExecTime([&]
{
calculateMedian(outputMatrix, inputMatrix, imageInfo.elementsPerPixel);
});
WritePngFileMatrix(outputMatrix, outputFileNamePad, colorType, imageInfo);
std::cout << "Time: " << (timeTaken.count() / 10E6) << "\n";
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment