draw a minion carrying a ginger in matlab

Here's an example code to draw a minion carrying a ginger on MATLAB:

main.m
% Read image of minion
minion = imread('minion.jpg');
% convert image to grayscale
bw_minion = rgb2gray(minion);
% make background pixels as white and minion pixels as black
bw_minion = imbinarize(bw_minion);
% invert image
bw_minion = ~bw_minion;

% the coordinates of the minion's arm
x1 = 240; y1 = 230;
x2 = 380; y2 = 190;

% create a line representing the arm of the minion that will carry the ginger
bw_minion = insertShape(bw_minion, 'Line', [x1,y1,x2,y2], 'LineWidth', 2, 'Color', 'black');

% read image of ginger
ginger = imread('ginger.jpg');

% resize ginger image to fit in minion's arm
ginger = imresize(ginger, [150,150]);
% convert image to grayscale
bw_ginger = rgb2gray(ginger);
% make background pixels as white and ginger pixels as black
bw_ginger = imbinarize(bw_ginger);
% invert image
bw_ginger = ~bw_ginger;

% calculate angle of the minion's arm
theta = atan2(y2-y1, x2-x1)*180/pi;

% rotate ginger image with the calculated angle
bw_ginger = imrotate(bw_ginger, theta, 'crop');

% get the size of the ginger image
[m, n] = size(bw_ginger);

% calculate coordinates where ginger image will be inserted
x = round(x2 - m/2); y = round(y2 - n/2);

% insert ginger image to the minion's arm
bw_minion(x:x+m-1, y:y+n-1) = bw_ginger;
% show final image
imshow(bw_minion);
1291 chars
45 lines

This code reads an image of a minion, converts it to grayscale, converts it to binary to separate the background pixels and the minion pixels. Then, it creates a line which represents the minion's arm that will carry the ginger by using insertShape() function. After that, the code reads an image of a ginger and resizes it to fit the minion's arm. The ginger image is also converted to grayscale and binary to separate the background pixels and ginger pixels. Then, the ginger image is rotated with the calculated angle, and the coordinates where the ginger image will be inserted are calculated. Finally, the ginger image is inserted into the minion's arm by using indexing, and the final image is displayed.

Note: The minion.jpg and ginger.jpg files are the image files you want to use for this code snippet. They need to be saved on the same directory as the MATLAB file you run.

related categories

gistlibby LogSnag