ppm图像模糊滤镜创建8个单独的图像

时间:2017-01-10 17:35:08

标签: c++ filter blur ppm

我对c ++有点陌生,我的课程分配是读取P6 ppm图像并在其上使用一些过滤器。我卡在模糊的一个上,我无法在我的代码上找到我的逻辑错误。 我的问题是输出图像模糊,但就像有4个模糊相同的图像

编辑 - 这里是输出图像的链接(TESTFILTER.ppm)输入图像(原始)和一个简单的查看器,以查看我的请求者是我们的(CPIViewer)

http://www.filedropper.com/blurfilterquestion

这是模糊过滤器代码。

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;

这是getPixel方法

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;
}

这是我的main()方法

#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";
    }
}

0 个答案:

没有答案