Ask Your Question
0

write pixel to mat

asked 2016-04-30 16:43:06 -0600

I want to manually set every pixel's R,G, and B floating point values to 0.4. When I run the following code I get an error. What am I doing wrong?

The error at run-time is:

* Error in `./program': free(): invalid next size (fast): 0x00000000019f9080 * Aborted

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
    Mat imageF(36,36, CV_32F);
    int i = 0;
    int j = 0;
    float blue = 0.0;
    float green= 0.0;
    float red= 0.0;
    Vec3f intensity;

    for(j=0; j<imageF.cols; j++)
        {
            for(i=0; i<imageF.rows; i++)
                {
                    intensity = imageF.at<Vec3f>(j, i);
                    //blue = intensity.val[0];
                    //green = intensity.val[1];
                    //red = intensity.val[2];

                    blue = red = green = 0.4;

                    intensity.val[0] = blue;
                    intensity.val[1] = green;
                    intensity.val[2] = red;

                    imageF.at<Vec3f>(j, i) = intensity;
                }
        }
    imshow("Output", imageF);

    waitKey(0);
    return 0;
}
edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2016-05-12 11:23:49 -0600

matman gravatar image

updated 2016-05-12 11:32:01 -0600

i: OpenCV is row major.

ii: ImageF.at<Vec3f>(y, x) == ImageF.at<Vec3f>(Point(x, y));

iii: You allocate an 1 channel image with CV_32F. Use CV_32FC3

iv: Do not use per Pixel manipulation. For constant values of the whole image use

cv::Mat imageF = cv::Mat(36, 36, CV_32FC3, cv::Scalar(blue, green, red));

or

imageF.setTo(cv::Scalar(blue, green, red));

This should work

#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
    Mat imageF(36,36, CV_32FC3);
    int i = 0;
    int j = 0;
    float blue = 0.0;
    float green= 0.0;
    float red= 0.0;
    Vec3f intensity;

    for(y=0; y<imageF.rows; y++)
        {
            for(x=0; x<imageF.cols; x++)
            {
                // The next two rows do the same
                //intensity = imageF.at<Vec3f>(y, x);
                intensity = imageF.at<Vec3f>(Point(x, y));

                //blue = intensity.val[0];
                //green = intensity.val[1];
                //red = intensity.val[2];

                blue = red = green = 0.4;
                intensity.val[0] = blue;
                intensity.val[1] = green;
                intensity.val[2] = red;

                // The next two rows do the same
                //imageF.at<Vec3f>(y, x) = intensity;
                imageF.at<Vec3f>(Point(x, y)) = intensity;
            }
        }
    imshow("Output", imageF);

    waitKey(0);
    return 0;
}
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-04-30 16:43:06 -0600

Seen: 3,274 times

Last updated: May 12 '16