How to avoid overlap two objects in image
Show older comments
I have the sample image I used this template to create a dataset at a different angle and position. it works well for generating one object but when I want to create a dataset for two objects as shown in the output image, the second object hide behind the first object How can I process to create two objects on the image?

the output image overlappng

function img_generate(img_width,img_hight,patch1,patch2,n)
a= img_width;
b=img_hight;
for i=1:n
p =randi(300);
q = randi(300);
sample = patch1;
x= randi(a-p)
y = randi(b-q)
%% Angle
angle = randi(360);
sample = imrotate(sample,angle);
sample = imresize(sample,[p q]);
img = uint8(zeros(a,b,1));
img(x:x+p-1,y:y+q-1,:) = sample;
p =randi(500);
q = randi(500);
sample = patch2;
x= randi(a-p)
y = randi(b-q)
%% Angle
angle = randi(360);
sample = imrotate(sample,angle);
sample = imresize(sample,[p q]);
img(x:x+p-1,y:y+q-1,:) = sample;
%%
pathName = "D:\Hammad\New folder\datset";
fileName = fullfile(pathName,sprintf('%d.jpg',i));
imwrite(img,fileName);
end
clc; clear all; close all;
%% Image Size
img_width = 800;
img_hight = 800;
patch = imread("ImageResult.png");
patch = rgb2gray(patch);
n_images = 500;
img_generate_n(img_width,img_hight,patch,patch,n_images);
Answers (1)
You're going to have to blend the sub-images as you combine them together. This is a simplified example of overlaying two copies of that image. Here are three simple blending methods that may be relevant.
% read image with alpha
[A,~,Aa] = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/753289/image.png');
A = rgb2gray(im2double(A)).*im2double(Aa); % apply alpha
B = flip(A,2);
out_lighten = max(A,B);
imshow(out_lighten)
out_screen = 1-((1-A).*(1-B));
imshow(out_screen)
out_add = A+B;
imshow(out_add)
5 Comments
Hafiz Muhammad Tayyab Abbas
on 29 Sep 2021
Edited: Hafiz Muhammad Tayyab Abbas
on 29 Sep 2021
Actually, I wouldn't worry about dealing with the alpha, at least for this image. In this particular case, it doesn't matter since the transparent regions are already black. When I posted the above, I didn't really bother to check.
The reason it wasn't working was because I was assuming a floating-point workflow, but you're explicitly allocating the output image to be uint8. That's fine, but the image classes need to be handled appropriately when they're combined.
% i didn't change anything here other than the filename
% Image Size
img_width = 800;
img_hight = 800;
patch = imread('comet.png');
patch = rgb2gray(patch);
n_images = 500;
img_generate(img_width,img_hight,patch,patch,n_images);
I added the relevant lines. You'll have to decide which to use (if any).
function img_generate(img_width,img_hight,patch1,patch2,n)
a = img_width;
b = img_hight;
for i=1:n
p =randi(300);
q = randi(300);
sample = patch1;
x = randi(a-p);
y = randi(b-q);
% Angle
angle = randi(360);
sample = imrotate(sample,angle);
sample = imresize(sample,[p q]);
img = uint8(zeros(a,b,1));
img(x:x+p-1,y:y+q-1,:) = sample;
p =randi(500);
q = randi(500);
sample = patch2;
x = randi(a-p);
y = randi(b-q);
% Angle
angle = randi(360);
sample = imrotate(sample,angle);
sample = imresize(sample,[p q]);
BG = img(x:x+p-1,y:y+q-1,:); % copy of any existing image content here
%img(x:x+p-1,y:y+q-1,:) = max(sample,BG); % lighten blend (for example)
img(x:x+p-1,y:y+q-1,:) = im2uint8(1-((1-im2double(sample)).*(1-im2double(BG)))); % screen blend (for example)
%img(x:x+p-1,y:y+q-1,:) = sample + BG; % addition blend (for example)
% i'm just dumping everything in my sandbox directory
fileName = sprintf('%d.jpg',i);
imwrite(img,fileName);
end
end
Hafiz Muhammad Tayyab Abbas
on 29 Sep 2021
yanqi liu
on 30 Sep 2021
sir,may be save the information on the filename
fileName = sprintf('%d_%.2f_%.2f_%.2f.jpg',i,x,y,angle);
There are a number of ways this could be done, but here's something basic:
function img_generate(img_width,img_hight,patch1,patch2,n)
a = img_width;
b = img_hight;
for i=1:n
p =randi(300);
q = randi(300);
sample = patch1;
x = randi(a-p);
y = randi(b-q);
% Angle
angle = randi(360);
sample = imrotate(sample,angle);
sample = imresize(sample,[p q]);
posdata = sprintf('%d,%d,%d\n',x,y,angle); % <--- store data
img = uint8(zeros(a,b,1));
img(x:x+p-1,y:y+q-1,:) = sample;
p =randi(500);
q = randi(500);
sample = patch2;
x = randi(a-p);
y = randi(b-q);
% Angle
angle = randi(360);
sample = imrotate(sample,angle);
sample = imresize(sample,[p q]);
posdata = [posdata sprintf('%d,%d,%d',x,y,angle)]; % <--- add another line
BG = img(x:x+p-1,y:y+q-1,:); % copy of any existing image content here
%img(x:x+p-1,y:y+q-1,:) = max(sample,BG); % lighten blend (for example)
img(x:x+p-1,y:y+q-1,:) = im2uint8(1-((1-im2double(sample)).*(1-im2double(BG)))); % screen blend (for example)
%img(x:x+p-1,y:y+q-1,:) = sample + BG; % addition blend (for example)
% i'm just dumping everything in my sandbox directory
fileName = sprintf('%0.3d.jpg',i); % consider using leading zeros to ease sorting issues
imwrite(img,fileName);
% write the stored data to disk
fileName = sprintf('%0.3d.txt',i);
fid = fopen(fileName,'w');
fprintf(fid,posdata);
fclose(fid);
end
end
You'll have to decide exactly what data you want to store (x&y offsets, height & width, centers, angle, etc), and how you want them formatted (headers, delimiters, etc)
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

