Ask Your Question
0

The complexity of the pixel-access in Mat.

asked 2018-09-07 15:09:33 -0600

Na_Osi gravatar image

Hello!

I am new in openCV and try to change colour of some pixels my matrix. if i work with new "Mat::zeros" matrix created by me, my code works (type of elements is CV_8UC1). image description

If i work with some image, code does not work correct. image description

But why? is it specific trait of image's depth or channels? can t get some properties of image like depth and number of channels.

The original image is here.

Please help me to make it clear.

The code for changing colour is:

for (int i = 0; i < img.rows; i++)
        for (int j = 0; j < img.cols; j++)
            if ((i % 20 == 10 && j % 2 == 1) ||
                (j % 50 == 25 && i % 2 == 1))
            {
                img.at<uchar>(i, j) = 255;
            }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2018-09-08 00:02:42 -0600

berak gravatar image

updated 2018-09-08 00:06:50 -0600

please do not abuse Mat::at to write whole images, it is a random access operator.

you also have to use the correct type, if your image has 3 channels, it needs to be: img.at<Vec3b> (NOT uchar)

better idea here, if you wanted to draw a grid, use builtin drawing functions like line():

for (int i = 0; i < img.rows; i+=20)
    line(img, Point(0,i), Point(img.cols-1, i), Scalar(255,0,0), 1);

for (int j = 0; j < img.cols; j+=20)
    line(img, Point(j,0), Point(j, img.rows-1), Scalar(255,0,0), 1);
edit flag offensive delete link more

Comments

2

Thank you for answer!

Really, my image has 3 channels. but its colour is brown.I thought that it means the image had 1 channel. after check with IplImage structure i saw depth = 8 and nChannels = 3

Na_Osi gravatar imageNa_Osi ( 2018-09-08 01:05:58 -0600 )edit
1

you can check mat.type() , mat.channels() , and mat.depth()

berak gravatar imageberak ( 2018-09-08 02:25:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-09-07 15:09:33 -0600

Seen: 400 times

Last updated: Sep 08 '18