In: Computer Science
Using Matlab
1. Create a color image of size 200 × 200. The image should have a blue background.
2. Create 100 yellow regions within the image that have a variable size. More specifically, their width should be an odd number and vary between 3 and 7 and their height should also be an odd number and vary between 3 and 7. For example, you may get rectangular regions of size 3 × 3, 3 × 5, 5 × 3, 5 × 5, 7 × 5, etc. These 100 yellow regions should be placed at random locations within the 200 × 200 image, but we should make sure that they do not overlap with each other. Important: These regions should be created by modifying the image pixels, and not as plots on top of the image. In other words, these regions should be part of the image.
clear;
clc;
% create a blue 200 x 200 image
BImage=zeros(200,200,3);
BImage(:,:,1) = 0;
BImage(:,:,2) = 0;
BImage(:,:,3) = 1;
% show image
figure, imshow(BImage);
% generate random odd size of 100 rectangular regions
% initialize yellow_block verctor
% 1st row is for row size of block
% 2nd row is for col size of block
% 3rd row is for block starting x co-ordinate
% 4th row is for block starting y co-ordinate
yellow_block = zeros(4, 100);
% initialize %
i = 1;
% random block size of m x n order where m, n belongs to [3, 5, 7]
total = size(yellow_block, 1)* size(yellow_block, 2);
while i <= total
% loop until odd number is generated
while 1
r = randi([3, 7], 1);
if mod(r, 2) == 1
break;
end
end
if mod(i, 2) == 1
yellow_block(1, ceil(i/2))= r;
else
yellow_block(2, ceil(i/2))= r;
end
i = i+1;
end
% declare an array of 200 x 200 which tracks the
% reservation of pixel location by yellow blocks
% initialize
reserve = zeros(200);
% initialize %
i = 1;
% now reserve the location for each of yellow block
while i <= 100
while 1
% generate random numbers for
% co-ordinate of a yellow block
x = randi([1, 200], 1);
y = randi([1, 200], 1);
% now find if the yellow block
% covering a already reserved pixel
if (x+yellow_block(1, i)-1) <= 200 && (y+yellow_block(2, i)-1) <= 200
s = sum(sum(reserve(x:(x + yellow_block(1, i)-1), y:(y + yellow_block(2, i)-1))));
if s == 0
% make the location reserved
reserve(x : (x+yellow_block(1, i)-1), y : (y+yellow_block(2, i)-1)) = 1;
% set origin pixel of yellow block
yellow_block(3, i) = x;
yellow_block(4, i) = y;
break;
end
end
end
i = i+1;
end
%
i = 1;
% make the blocks yelllow color
while i <= 100
BImage(yellow_block(3, i):(yellow_block(3, i) + yellow_block(1, i)-1), ...
yellow_block(4, i):(yellow_block(4, i) + yellow_block(2, i)-1) ,3)=0;
BImage(yellow_block(3, i):(yellow_block(3, i) + yellow_block(1, i)-1), ...
yellow_block(4, i):(yellow_block(4, i) + yellow_block(2, i)-1) ,1)=1;
BImage(yellow_block(3, i):(yellow_block(3, i) + yellow_block(1, i)-1), ...
yellow_block(4, i):(yellow_block(4, i) + yellow_block(2, i)-1) ,2)=1;
i = i + 1;
end
% display image
figure, imshow(BImage);
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Note: If you have queries or confusion regarding this question, please leave a comment. I would be happy to help you. If you find it to be useful, please upvote.