1

I'm kinda new to c++ and I have an assignment for my course to read a P6 ppm image and use some filters on it.Im stuck on the blurring one and I cannot find my logical error on my code. My problem is that the output image is blurred but it is like there are 4 blurred same images there

EDIT- HERE IS THE LINK TO OUTPUT IMAGE(TESTFILTER.ppm) THE INPUT IMAGE (Original) AND A SIMPLE VIEWER TO VIEW THEM MY PROFFESSOR GAVE US (CPIViewer)

http://www.filedropper.com/blurfilterquestion

Here is the blur filter code.

typedef Vec3<float> buffer;

static void blur(Image*   pic) { //BLUR FILTER
            int h = pic->getHeight();
            int w = pic->getWidth();

            int count = 0;
            buffer* temp = new buffer[h*w];
            for (int x = 0; x < w; x++){
                for (int y = 0; y < h; y++){

                    if (x == 0 && y == 0) {
                        count++;
                        temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x + 1, y) + pic->getPixel(x, y + 1) + pic->getPixel(x + 1, y + 1)) / 4;
                    }
                    else if (x == 0 && y == h - 1) {
                        count++;
                        temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x + 1, y) + pic->getPixel(x, y - 1) + pic->getPixel(x + 1, y - 1)) / 4;
                    }
                    else if (x == w - 1 && y == 0){
                        count++;
                        temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x - 1, y) + pic->getPixel(x, y + 1) + pic->getPixel(x - 1, y + 1)) / 4;
                    }
                    else if (x == w - 1 && y == h - 1){
                        count++;
                        temp[y*w + x] = (pic->getPixel(x, y) + pic->getPixel(x - 1, y) + pic->getPixel(x, y - 1) + pic->getPixel(x - 1, y - 1)) / 4;
                    }
                    else if (x == 0){
                        count++;
                        temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x + 1, y - 1)) + (pic->getPixel(x + 1, y)) + (pic->getPixel(x + 1, y + 1))) / 6;
                    }
                    else if (x == w - 1){
                        count++;
                        temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x - 1, y + 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x - 1, y - 1))) / 6;
                    }
                    else if (y == 0){
                        count++;
                        temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x, y + 1)) + (pic->getPixel(x - 1, y + 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x + 1, y + 1))) / 6;
                    }
                    else if (y == h - 1){
                        count++;
                        temp[y*w + x] = ((pic->getPixel(x, y)) + (pic->getPixel(x, y - 1)) + (pic->getPixel(x - 1, y)) + (pic->getPixel(x + 1, y - 1)) + (pic->getPixel(x + 1, y)) + (pic->getPixel(x - 1, y - 1))) / 6;
                    }
                    else {
                        count++;
                        temp[y*w + x].r = ((pic->getPixel(x, y).r) + (pic->getPixel(x + 1, y).r) + (pic->getPixel(x, y + 1).r) + (pic->getPixel(x, y - 1).r) + (pic->getPixel(x - 1, y).r) + (pic->getPixel(x - 1, y - 1).r) + (pic->getPixel(x + 1, y + 1).r) + (pic->getPixel(x + 1, y - 1).r) + (pic->getPixel(x - 1, y + 1).r)) / 9;
                        temp[y*w + x].g = ((pic->getPixel(x, y).g) + (pic->getPixel(x + 1, y).g) + (pic->getPixel(x, y + 1).g) + (pic->getPixel(x, y - 1).g) + (pic->getPixel(x - 1, y).g) + (pic->getPixel(x - 1, y - 1).g) + (pic->getPixel(x + 1, y + 1).g) + (pic->getPixel(x + 1, y - 1).g) + (pic->getPixel(x - 1, y + 1).g)) / 9;
                        temp[y*w + x].b = ((pic->getPixel(x, y).b) + (pic->getPixel(x + 1, y).b) + (pic->getPixel(x, y + 1).b) + (pic->getPixel(x, y - 1).b) + (pic->getPixel(x - 1, y).b) + (pic->getPixel(x - 1, y - 1).b) + (pic->getPixel(x + 1, y + 1).b) + (pic->getPixel(x + 1, y - 1).b) + (pic->getPixel(x - 1, y + 1).b)) / 9;

                    }
                }
            }
            const buffer* constTemp;
            constTemp = temp;
            pic->setData(constTemp);
            std::cout << "TIMES: " << count << std::endl;

here is the getPixel method

typedef Vec3<float> color;
enter code here
color  Image::getPixel(unsigned int x, unsigned int y) const   {
    if (x > getWidth() || y > getHeight()) { color col(0, 0, 0); return col; }
    color col = buffer[y*width + x];

    return  col;
}

and here is my main() method

#include "Image.h"
#include "ppm_format.h"
#include "Filters.h"
#include <iostream>




using namespace imaging;
using namespace std;
using namespace filtering;


int main(int argc, char* argv[]) {

Image* image = ReadPPM(argv[argc - 1]); // Creating Image


float r, g, b;
bool filterFlag = false; //Flag used to check if -f command is given 
string check; 

Vec3<float> temp;


for (int i = 1; i < argc - 2; i++) {
    string check(argv[i]);
    if (!check.compare("-f")) { filterFlag = true;  } // check.compare("-f") returns 0 if check = -f
    if (filterFlag) {
    i++;
    check = argv[i];
        if (!check.compare("gray")) {
            filterFlag = false; //filter flag falsified to show that a filter is read
            gray(image);    
        }
        else if (!check.compare("color")) {
            filterFlag = false;

            if (argc-2 <= i + 3) {
                cout << "INVALID COLOR FILTER CALL.EXITING...";
                system("pause");
                exit(0);
            }
            r = i + 1;
            g = i + 2;
            b = i + 3;
            i += 4;

            color(image, r, g, b);
        }
        else if (!check.compare("blur")) {
            filterFlag = false;

            blur(image);

        }
        else if (!check.compare("median")){
            filterFlag = false;

            medianORdiff(image,"median");
        }
        else if (!check.compare("diff")){
            filterFlag = false;

            medianORdiff(image,"diff");
        }
        else {
            cout << "Invalid filter name or no filter name at all.\nFilter names are gray , color r g b , blur , median , diff";
            system("pause");
            exit(0);
        }
        *image >> "C://Users/M1TR0S0UL4S/Desktop/TESTFILTER.ppm";
    }
}
Dimitris
  • 23
  • 6
  • What do you mean by 4 images? Can you show the result? Are they shown side-by-side, or are there just 4 images in total? – KjMag Jan 10 '17 at 19:18
  • actually they are 8 and they are blended together in the same frame it's like only the 1/4 of the pic is rendered and next to it the same part of the picture starts again i don't know how to post the picture – Dimitris Jan 10 '17 at 21:43
  • Edit your answer - there's a button for adding an image. It's going to be much easier. Also please write how do you run this program, i.e. what the input parameters are (I guess it is a command line program?). – KjMag Jan 11 '17 at 11:55
  • max size for image is 1mb.Because those are ppm images they are about 2.5mb so I cannot upload them. The command is -f blur Image01.ppm Those are the arguments and I believe my main works well because the previous two filters I've done work well. – Dimitris Jan 11 '17 at 12:02
  • edit I uploaded the images along with a viewer my professor gave us in a free uploading site don't know how long they will keep it there though – Dimitris Jan 11 '17 at 12:14
  • Sorry mate, I don't download programs or archives whose origins I don't know. You could upload smaller version of the image or upload the full size to external service such as imgur. – KjMag Jan 11 '17 at 13:13
  • tried some services like imagebucket but none would let me upload ppm type – Dimitris Jan 11 '17 at 14:24
  • Then convert the image to some other lossless format. – KjMag Jan 11 '17 at 15:28

0 Answers0