MATLAB Answers

How can we replacing the pixel values of RGB image under bounding box with some random pixel values?

8 views (last 30 days)
sami ullah
sami ullah on 4 Nov 2020
Commented: sami ullah on 6 Nov 2020
I have the following code:
clear all
faceDetector = vision.CascadeObjectDetector;
I = imread('visionteam.jpg');
figure, imshow(I), title('Input Image');
bboxes = step(faceDetector, I);
IFaces = insertObjectAnnotation(I, 'Rectangle', bboxes, 'Face');
figure, imshow(IFaces), title('Detected faces');
I want to replace the pixel values of RGB image under bounding box with some random pixel values?
After replacing the pixel values under bounding box, how can i show the complete image with replaced pixel values under bounding box?


Sign in to comment.

Accepted Answer

Dave B
Dave B on 4 Nov 2020
Hi Sami:
It sounds like you want to set the RGB pixel value for the values in the bounding boxes. You can extract the rows and columns from the 'bboxes' variable, and then set those on the image matrix (or take a copy of the matrix to avoid altering the original). Below I show an example where I make all of the pixels in the first bounding box red. I then show the altered image using the imshow function.
% Each row of bboxes has a X, Y, width, height
cols=bboxes(1,1):sum(bboxes(1,[1 3]));
rows=bboxes(1,2):sum(bboxes(1,[2 4]));
% Make bbox 1 red:
figure, imshow(I);


sami ullah
sami ullah on 5 Nov 2020
instead of setting bbox 1 to Red. Can i set it to random values or pass to some other function for performing XOR? How can I do?
Dave B
Dave B on 5 Nov 2020
To set each pixel within the bounding box to a random value, you could do:
This sets the RGB values for pixels in the bounding box to a set of random uniform numbers. See rand for more details. Note that the arguments I passed into rand specifiy the number of rows, number of columns, and '3' - for the three (red, green, blue) channels.
To set all of the pixels within the bounding box to one, random, color:
clr=rand(1,1,3)*255; % Pick 3 random numbers, shaped like an RGB, multiply by 255 to make them RGB like
I(rows,cols,:)=repmat(clr,numel(rows),numel(cols)); % Repeat these numbers to fill the block
See repmat for more details on repeating an array.
You can also pass the bounding box coordinates to another function, just call that function with the variable 'bboxes'. If you want to call the functions with the contents of the bounding box, you might pass in I(rows,cols,:) - a matrix with rows and columns corresponding to the bounding box, and three 'slices' corresponding to red, green, and blue channels.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!