Ask Your Question

Pixels in the region of interest (ROI)

asked 2013-08-19 13:58:27 -0500

deiaalencar gravatar image

Hello, could someone help me, I'm new to OpenCV and would like to know how to change a pixel of an roi to another. My program opens an image, divides the image into rectangles with the rectangle function, and would like to know how to change a few pixels from the edges of rectangle. From already, thank you.

edit retag flag offensive close merge delete


so, you've drawn a grid onto your image. and now ? it's a bit unclear, what you want to do from here on.

could you edit , and be a bit more specific ?

berak gravatar imageberak ( 2013-08-19 15:56:43 -0500 )edit

Then I drew a grid on my image and now I want to change a few pixels that are on the edge of the rectangles, rectangle to another, for example, that the pixel at position (0.159) belongs to ROI1 and I want to put it in ROI2 ...

deiaalencar gravatar imagedeiaalencar ( 2013-08-28 17:02:04 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2013-08-19 22:06:02 -0500

To change/access pixel of ROI, it's the same as access/change pixel in the Mat. Indeed, the ROI is just an header for the underling data, therefore:

Mat img = imread( "myimage.jpg" );
Rect my_roi( x, y, w, h );
Mat ROI = img( my_roi );
Vec3b pixel =< Vec3b >( pixel_x, pixel_y );
// Swap color for the first pixel (BGR => RGB).
char tmp = pixel[0];
pixel[0] = pixel[2]; // set blue to red value
pixel[1] = pixel[1]; // do nothing on green
pixel[2] = tmp; // set red to blue

I use vector (Vec3b) in that sample, but you could use char or Ptr aceess on anything you want. Look at this page for the performance evaluation of accessors.

edit flag offensive delete link more


But in this case I'll be changing the value of the pixel and not the position, in fact I need to get the pixel is on the edge of ROI1 and put it in ROI2, so that by the time I go to print the image on the screen no longer appears she divided into rectangles, but a little deformed.

deiaalencar gravatar imagedeiaalencar ( 2013-09-02 08:15:23 -0500 )edit

Question Tools


Asked: 2013-08-19 13:58:27 -0500

Seen: 3,952 times

Last updated: Aug 28 '13