Ask Your Question
0

Problem setting pixel value

asked 2017-01-09 07:11:04 -0600

simozz gravatar image

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 flag offensive close merge delete

Comments

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 ?

berak gravatar imageberak ( 2017-01-09 07:15:59 -0600 )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.

simozz gravatar imagesimozz ( 2017-01-09 08:01:48 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-01-09 08:11:07 -0600

berak gravatar image

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]
edit flag offensive delete link more

Comments

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.

simozz gravatar imagesimozz ( 2017-01-09 16:26:30 -0600 )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;
    }
}
berak gravatar imageberak ( 2017-01-09 17:27:49 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-09 07:11:04 -0600

Seen: 535 times

Last updated: Jan 09 '17