Problem setting pixel value

I know this is a well known topic but for all the solutions I tried (major of them from stack overflow Q&A), I cannot set a pixel value as I want.

Given this code:

Mat sgWs = Mat(frameFixedSize.height * frameFixedSize.width, nColumns, CV_8UC1);
for(uint32_t x = 0; x < (uint32_t) frameFixedSize.height; x++)
{
for(uint32_t y = 0; y < (uint32_t) frameFixedSize.width; y++)
{
if (!y)
sgWs.at<uchar>(x, y) = 1;
}
}


the pixel values are not set to one. The istruction:

if (!y)
sgWs.at<cv::Vec3b>(y,x)[0] = 1;


neither works. So how do I have to change my code to make it works ?

edit retag close merge delete

you simply should never iterate over pixels at all.

what is your situation, and what are you trying to achieve ?

what is nColumns, and why is your Mat a large vertical strip ?

( 2017-01-09 07:15:59 -0500 )edit

Regarding the 1st question: So nColumns is of course the width size. Regarding the 2 last questions: I am trying to translate this line of matlab code: ws = [ones(VideoData.Height*VideoData.Width, 1), zeros(VideoData.Height*VideoData.Width, K-1)]; where K is an integer. That's why I want to set pixels of the 1st column to 1.

( 2017-01-09 08:01:48 -0500 )edit

Sort by ยป oldest newest most voted

you can easily pick any ROI of your Mat, and set that to a certain value.

example (let's set the 1st column to 1):

Mat M = Mat::zeros(5,6,CV_8U);

M(Rect(0,0,1,M.rows)) = 1;

cerr << M;

[  1,   0,   0,   0,   0,   0;
1,   0,   0,   0,   0,   0;
1,   0,   0,   0,   0,   0;
1,   0,   0,   0,   0,   0;
1,   0,   0,   0,   0,   0]

more

This is fine. But how do I modify pixel directly ? @berak, you wrote that the program should never iterate over pixels. So how do I do it, if I need it ? Sometimes the situation could be different than this.

( 2017-01-09 16:26:30 -0500 )edit
1

your original example gets wrong, that rows is actually frameFixedSize.height * frameFixedSize.width and cols is nColumns, and it also misnames x and y, so:

for(uint32_t x = 0; x < (uint32_t) nColumns; x++)
{
for(uint32_t y = 0; y < (uint32_t) frameFixedSize.width *  frameFixedSize.height; y++)
{
if (x==0)
sgWs.at<uchar>(y, x) = 1;
}
}

( 2017-01-09 17:27:49 -0500 )edit