Ask Your Question

Is there a way to easily plot pixels?

asked 2015-03-10 16:19:30 -0600

TrustMeImAnEngineer gravatar image

Let's say I have a Mat M which contains a grayscale image. Now I apply a colormap to make a new Mat M_color and show the image. The colormap is fine, but at specific points (where I already know the indices), I want to make the pixels a specific color (let's say black).

Example: I have a 25x25 grayscale image shown with a rainbow colormap. At pixels (2,7), (4,12), (16,9), and (14,23) I want them to be black.

Is there an easy way to plot the black pixels over-top the rainbow image? Or if it's easier, to just change the color at that specific instance to black.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-10 16:33:08 -0600

Potato gravatar image

updated 2015-03-10 16:47:09 -0600

Depending on your application, you could draw circles around the points instead of making the pixels specifically coloured. There are OpenCV functions to do this. But yes, you can change the color at that specific instance to black by doing something like this --><uchar>(Points(x, y)) = 0;

Edit: Here is the function for drawing a circle around a point circle(image, co-ordinates, radius, color) --> Where image is the source image, co-ordinates are the points where you want the center of the circle to be (Point(x,y)), radius is an integer value with the circle radius, and color is the color of the circle. (Scalar(0, 0, 0) for a black circle).

Additionally, you could also create a rectangle with your four points as the edges.

edit flag offensive delete link more


Thanks for the help. Since I want multiple circles, I have to apply the circle function multiple times? Also, doesn't this exceed the size of one pixel? What I mean by this is let's say the circle radius is one, this will technically circle around more than one pixel because the pixel value is just the center, correct?

Other question, let's say I use<uchar>(Point(x, y)) = 0; does uchar work for CV_8U datatype? Sorry, I know that question is really basic. And finally, let's say I want to make the color turquoise instead of black. Do I say:<uchar>(Point(x, y)) = Scalar(R,G,B); where R,G,B are the proper values for whatever color I want?

TrustMeImAnEngineer gravatar imageTrustMeImAnEngineer ( 2015-03-10 16:54:39 -0600 )edit

just as a sidenote, if that operation is after applying a colorMap, it should be:<Vec3b>(y,x) = Scalar(b,g,r);

also, please, Point, not Points

berak gravatar imageberak ( 2015-03-10 16:55:59 -0600 )edit

Thanks berak. This might be a dumb question, but why is it (y, x) instead of (x, y)? Is that just the convention when you do <Vec3b>? Or is it because you are using

EDIT: Nevermind, I looked at the documentation.

TrustMeImAnEngineer gravatar imageTrustMeImAnEngineer ( 2015-03-10 17:02:53 -0600 )edit

Point goes x,y - while cv::Mat is at<type>(row,col); so either:<type>(y,x); or<type>(Point(x,y));

berak gravatar imageberak ( 2015-03-10 17:17:26 -0600 )edit

Yes @TrustMeImAnEngineer, you would need to draw the circle function 4 times. 1 for each point. And @berak was right, since the color map has been applied, you would need to use<Vec3b>(y,x) = Scalar(b,g,r); where b g r are the proper values for the colour turquoise.

Potato gravatar imagePotato ( 2015-03-11 09:16:22 -0600 )edit

In a sense it is a convention, but a historical one. Data storage on a computer is always 1D, so if you look at C-code and you have anarray[y][x], where 'x' is the inner array and 'y' is the outer one. So the convention is to scan in rows like an old TV will do.

There now a huge speed difference between

for(int y=0; y < Ymax; y++) {
    for(int x=0; x < Xmax; x++) {
        int something = array[y][x];


for(int y=0; y < Ymax; y++) {
    for(int x=0; x < Xmax; x++) {
        int something = array[x][y];

where the first example reads the data continous and the second one does not

matman gravatar imagematman ( 2015-03-11 15:12:56 -0600 )edit

Question Tools

1 follower


Asked: 2015-03-10 16:19:30 -0600

Seen: 1,426 times

Last updated: Mar 10 '15