Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version
  • image.at<Vec3b>(j,i)= intensity; <-- i,j , not j, (you're probably out of bounds this way)
  • please use the c++ headers: #include <opencv2/opencv.hpp> not the deprecated c ones(cv.h, highgui.h)
  • in general, - try to avoid per-pixel loops.
  • image.at<Vec3b>(j,i)= intensity; <-- i,j , not j, j,i (you're probably out of bounds this way)
  • please use the c++ headers: #include <opencv2/opencv.hpp> not the deprecated c ones(cv.h, highgui.h)
  • in general, - try to avoid per-pixel loops.
  • image.at<Vec3b>(j,i)= intensity; <-- i,j , not j,i (you're probably out of bounds this way)
  • please use the c++ headers: #include <opencv2/opencv.hpp> not the deprecated c ones(cv.h, highgui.h)
  • in general, - try to avoid per-pixel loops.
  • [edit] pixel order is BGR in opencv, not RGB
  • image.at<Vec3b>(j,i)= intensity; <-- i,j , not j,i (you're probably out of bounds this way)
  • please use the c++ headers: #include <opencv2/opencv.hpp> not the deprecated c ones(cv.h, highgui.h)
  • in general, - try to avoid per-pixel loops.
  • [edit] pixel order is BGR in opencv, not RGB

[edit]

imho, you have to do this in float space, not in 8u. while i'm at it, one could get rid of the loops, too.

void filter2 (const Mat &image, Mat &out)
{
    // convert and split
    Mat imgf;
    image.convertTo(imgf, CV_32F, 1.0/255.0);
    Mat chn[3];
    split(imgf, chn);
    // (R-G)^2
    Mat rg = chn[2] - chn[1];
    multiply(rg,rg,rg);
    // (R-B)^2
    Mat rb = chn[2] - chn[0];
    multiply(rb,rb,rb);
    // (G-B)^2
    Mat gb = chn[1] - chn[0];
    multiply(gb,gb,gb);
    // divide by sum
    Mat sm = (rg + rb + gb);
    divide(rg, sm, rg);
    divide(rb, sm, rb);
    divide(gb, sm, gb);
    // merge and concvert
    Mat ch2[] = {rg, rb, gb};
    merge(ch2, 3, imgf);
    imgf.convertTo(out, CV_8U, 255.0);
}

int main(int agra, char** argv){
    String fn="phase1.png";
    if (agra>1) fn = argv[1];
    Mat image = imread(fn);
    if (image.empty())
    {
        cerr << "U$)TZ(T§$TI§H";
        return 1;
    }
    Mat result;
    filter2(image, result);
    imshow("org", image);
    imshow("res", result);
    waitKey(0);
    return 0;
}