How to avoid overlap two objects in image

1 visualización (últimos 30 días)
Hafiz Muhammad Tayyab Abbas
Hafiz Muhammad Tayyab Abbas el 29 de Sept. de 2021
Editada: DGM el 30 de Sept. de 2021
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);

Respuestas (1)

DGM
DGM el 29 de Sept. de 2021
Editada: DGM el 29 de Sept. de 2021
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 comentarios
yanqi liu
yanqi liu el 30 de Sept. de 2021
sir,may be save the information on the filename
fileName = sprintf('%d_%.2f_%.2f_%.2f.jpg',i,x,y,angle);
DGM
DGM el 30 de Sept. de 2021
Editada: DGM el 30 de Sept. de 2021
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)

Iniciar sesión para comentar.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by