Mike Miller
Mike Miller

Reputation: 263

Writing pixel value to blank matrix

having a bit of trouble with this. For a simple example, say I wanted to look up the intensity values of a grey-scale image and write them onto a new array, but say change the intensity by a certain amount. How would I go about doing this with opencv? I've been trying something along the lines of this to try and practice:

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

    Mat img = imread(argv[1], CV_LOAD_IMAGE_UNCHANGED), img_grey, dst;

    if (img.empty())
    {
        return -1;
    }

    cvtColor(img, img_grey, CV_BGR2GRAY);

    dst = Mat::zeros(img_grey.size(), img_grey.type());

    for
        (int x = 1; x < dst.rows - 1; x++)
    {
        for (int y = 1; y < dst.cols - 1; y++)
        {

            dst.at<uchar>(y, x) = x;

        }
    }

    namedWindow("New", CV_WINDOW_AUTOSIZE);
    namedWindow("Original", CV_WINDOW_AUTOSIZE);

    imshow("New", dst);
    imshow("Original", img);



    waitKey(0);
    return 0;

}

Upvotes: 0

Views: 100

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50667

Given the way that you access the image, you should change

dst.at<uchar>(y, x) = x;

to

dst.at<uchar>(x, y) = x;

Check out Mat::at() for more info.


Edit: As @Micka pointed out, you really should rename your x and y since x is normally assumed to be the horizontal axis, which corresponds to the "column-direction". Normal use case will be like:

for(int y = 0; y < mat.rows; y++)
    for(int x = 0; x < mat.cols; x++)
        mat.at<uchar>(y,x) = ...;

Upvotes: 1

Related Questions