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

조회 수: 5 (최근 30일)
I have the following code:
clear all
clc
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?

채택된 답변

Dave B
Dave B 2020년 11월 4일
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:
I(rows,cols,1)=255;
I(rows,cols,2)=0;
I(rows,cols,3)=0;
figure, imshow(I);
  댓글 수: 3
Dave B
Dave B 2020년 11월 5일
To set each pixel within the bounding box to a random value, you could do:
I(rows,cols,:)=rand(numel(rows),numel(cols),3)*255;
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Feature Detection and Extraction에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by